|
| 1 | +module App.Widgets.PlaceOrder (placeOrder) where |
| 2 | + |
| 3 | +import qualified App.Jsm as Jsm |
| 4 | +import App.Types |
| 5 | +import qualified App.Xlsx as Xlsx |
| 6 | +import qualified Data.ByteString.Lazy as BL |
| 7 | +import qualified Functora.Miso.Jsm as Jsm |
| 8 | +import Functora.Miso.Prelude |
| 9 | +import qualified Functora.Miso.Widgets.Dialog as Dialog |
| 10 | +import qualified Functora.Miso.Widgets.Icon as Icon |
| 11 | +import qualified Text.URI as URI |
| 12 | + |
| 13 | +placeOrder :: Model -> [View Action] |
| 14 | +placeOrder st = |
| 15 | + if null $ st ^. #modelState . #stAssets |
| 16 | + then mempty |
| 17 | + else |
| 18 | + button_ |
| 19 | + [ onClick |
| 20 | + . PushUpdate |
| 21 | + . Instant |
| 22 | + . PureUpdate |
| 23 | + $ #modelPlaceOrder |
| 24 | + .~ Opened |
| 25 | + ] |
| 26 | + [ icon Icon.IconShopping, |
| 27 | + text " Place Order" |
| 28 | + ] |
| 29 | + : Dialog.dialog |
| 30 | + Dialog.defOpts |
| 31 | + { Dialog.optsTitle = Just ("Place order" :: Unicode), |
| 32 | + Dialog.optsFlexCol = False, |
| 33 | + Dialog.optsTitleIcon = Just Icon.IconShopping |
| 34 | + } |
| 35 | + Dialog.Args |
| 36 | + { Dialog.argsModel = st, |
| 37 | + Dialog.argsOptic = #modelPlaceOrder, |
| 38 | + Dialog.argsAction = PushUpdate . Instant, |
| 39 | + Dialog.argsContent = |
| 40 | + [ button_ |
| 41 | + [ onClick |
| 42 | + . either impureThrow openBrowser |
| 43 | + $ teleUri st |
| 44 | + ] |
| 45 | + [ icon Icon.IconTelegram, |
| 46 | + text " Telegram" |
| 47 | + ], |
| 48 | + button_ |
| 49 | + [ onClick |
| 50 | + . either impureThrow openBrowser |
| 51 | + $ whatsUri st |
| 52 | + ] |
| 53 | + [ icon Icon.IconWhatsApp, |
| 54 | + text " WhatsApp" |
| 55 | + ], |
| 56 | + button_ |
| 57 | + [ onClick |
| 58 | + . either impureThrow openBrowser |
| 59 | + $ emailUri st |
| 60 | + ] |
| 61 | + [ icon Icon.IconEmail, |
| 62 | + text " Email" |
| 63 | + ], |
| 64 | + button_ |
| 65 | + [ onClick . PushUpdate . Instant . EffectUpdate $ do |
| 66 | + let doc = st ^. #modelState |
| 67 | + imgs <- Jsm.fetchBlobUris doc |
| 68 | + file <- Xlsx.xlsxFile |
| 69 | + Jsm.saveFileShare file Xlsx.xlsxMime |
| 70 | + . from @BL.ByteString @ByteString |
| 71 | + $ Xlsx.newXlsx doc imgs |
| 72 | + ] |
| 73 | + [ icon Icon.IconDownload, |
| 74 | + text " Share Excel file" |
| 75 | + ] |
| 76 | + ] |
| 77 | + } |
| 78 | + |
| 79 | +openBrowser :: URI -> Action |
| 80 | +openBrowser = |
| 81 | + PushUpdate |
| 82 | + . Instant |
| 83 | + . EffectUpdate |
| 84 | + . Jsm.openBrowserPage |
| 85 | + |
| 86 | +teleUri :: (MonadThrow m) => Model -> m URI |
| 87 | +teleUri st = do |
| 88 | + base <- URI.mkURI "https://t.me" |
| 89 | + user <- |
| 90 | + URI.mkPathPiece |
| 91 | + . from @Unicode @Text |
| 92 | + $ st |
| 93 | + ^. #modelState |
| 94 | + . #stMerchantTele |
| 95 | + . #fieldOutput |
| 96 | + key <- URI.mkQueryKey "text" |
| 97 | + val <- URI.mkQueryValue placeOrderMessage |
| 98 | + pure |
| 99 | + $ base |
| 100 | + { URI.uriPath = Just (False, [user]), |
| 101 | + URI.uriQuery = [URI.QueryParam key val] |
| 102 | + } |
| 103 | + |
| 104 | +whatsUri :: (MonadThrow m) => Model -> m URI |
| 105 | +whatsUri st = do |
| 106 | + base <- URI.mkURI "https://wa.me" |
| 107 | + user <- |
| 108 | + URI.mkPathPiece |
| 109 | + . from @Unicode @Text |
| 110 | + $ st |
| 111 | + ^. #modelState |
| 112 | + . #stMerchantWhats |
| 113 | + . #fieldOutput |
| 114 | + key <- URI.mkQueryKey "text" |
| 115 | + val <- URI.mkQueryValue placeOrderMessage |
| 116 | + pure |
| 117 | + $ base |
| 118 | + { URI.uriPath = Just (False, [user]), |
| 119 | + URI.uriQuery = [URI.QueryParam key val] |
| 120 | + } |
| 121 | + |
| 122 | +emailUri :: (MonadThrow m) => Model -> m URI |
| 123 | +emailUri st = do |
| 124 | + user <- |
| 125 | + URI.mkPathPiece |
| 126 | + . from @Unicode @Text |
| 127 | + $ st |
| 128 | + ^. #modelState |
| 129 | + . #stMerchantEmail |
| 130 | + . #fieldOutput |
| 131 | + base <- URI.mkURI $ "mailto:" <> URI.unRText user |
| 132 | + subjKey <- URI.mkQueryKey "subject" |
| 133 | + subjVal <- URI.mkQueryValue "Delivery Calculator" |
| 134 | + bodyKey <- URI.mkQueryKey "body" |
| 135 | + bodyVal <- URI.mkQueryValue placeOrderMessage |
| 136 | + pure |
| 137 | + $ base |
| 138 | + { URI.uriQuery = |
| 139 | + [ URI.QueryParam subjKey subjVal, |
| 140 | + URI.QueryParam bodyKey bodyVal |
| 141 | + ] |
| 142 | + } |
| 143 | + |
| 144 | +placeOrderMessage :: Text |
| 145 | +placeOrderMessage = |
| 146 | + "Hello, I have a question about delivery. I will share the Excel file in the next message." |
0 commit comments