|
| 1 | +module Statebox.Console where |
| 2 | + |
| 3 | +import Prelude |
| 4 | +import Data.Either (either) |
| 5 | +import Data.Lens |
| 6 | +import Data.Lens.Record (prop) |
| 7 | +import Data.Symbol (SProxy(..)) |
| 8 | +import Data.Foldable (fold, foldMap) |
| 9 | +import Data.Maybe (Maybe(..), maybe, fromMaybe) |
| 10 | +import Effect.Aff.Class (class MonadAff) |
| 11 | +import Effect.Console (log) |
| 12 | +import Halogen as H |
| 13 | +import Halogen (ComponentHTML) |
| 14 | +import Halogen.HTML (HTML, p, text, div, ul, li, h2, table, tr, th, td) |
| 15 | +import Halogen.Query.HalogenM (HalogenM) |
| 16 | + |
| 17 | +import Statebox.Console.DAO as DAO |
| 18 | + |
| 19 | +import Stripe as Stripe |
| 20 | + |
| 21 | +import Debug.Trace (spy) |
| 22 | + |
| 23 | +-------------------------------------------------------------------------------- |
| 24 | + |
| 25 | +type State = |
| 26 | + { customer :: Maybe Stripe.Customer |
| 27 | + , paymentMethods :: Array Stripe.PaymentMethod |
| 28 | + , accounts :: Array { invoices :: Array Stripe.Invoice |
| 29 | + } |
| 30 | + , status :: AppStatus |
| 31 | + } |
| 32 | + |
| 33 | +_accounts = prop (SProxy :: SProxy "accounts") |
| 34 | +_invoices = prop (SProxy :: SProxy "invoices") |
| 35 | + |
| 36 | +-------------------------------------------------------------------------------- |
| 37 | + |
| 38 | +data AppStatus = Ok | ErrorStatus String |
| 39 | + |
| 40 | +derive instance eqAppStatus :: Eq AppStatus |
| 41 | + |
| 42 | +instance showAppStatus :: Show AppStatus where |
| 43 | + show = case _ of |
| 44 | + Ok -> "Ok" |
| 45 | + ErrorStatus x -> "(ErrorStatus " <> x <> ")" |
| 46 | + |
| 47 | +type Input = State |
| 48 | + |
| 49 | +data Action = FetchStuff |
| 50 | + |
| 51 | +data Query a = DoAction Action a |
| 52 | + |
| 53 | +type ChildSlots = () |
| 54 | + |
| 55 | +ui :: ∀ m. MonadAff m => H.Component HTML Query Input Void m |
| 56 | +ui = |
| 57 | + H.mkComponent |
| 58 | + { initialState: mkInitialState |
| 59 | + , eval: H.mkEval $ H.defaultEval { handleAction = handleAction, handleQuery = handleQuery } |
| 60 | + , render: render |
| 61 | + } |
| 62 | + |
| 63 | +mkInitialState :: Input -> State |
| 64 | +mkInitialState input = input |
| 65 | + |
| 66 | +handleQuery = case _ of |
| 67 | + (DoAction x next) -> do |
| 68 | + handleAction x |
| 69 | + pure (Just next) |
| 70 | + |
| 71 | +handleAction :: ∀ m. MonadAff m => Action -> HalogenM State Action ChildSlots Void m Unit |
| 72 | +handleAction = case _ of |
| 73 | + FetchStuff -> do |
| 74 | + H.liftEffect $ log "handling action FetchStuff..." |
| 75 | + invoicesEE <- H.liftAff $ DAO.listInvoices |
| 76 | + invoicesEE # either (\e -> H.modify_ $ _ { status = ErrorStatus "Failed to fetch invoices." }) |
| 77 | + (either (\e -> H.modify_ $ _ { status = ErrorStatus "Decoding invoices failed."}) |
| 78 | + (\x -> H.modify_ $ _ { accounts = [ { invoices: x.data } ] })) |
| 79 | + spyM "invoicesEE" $ invoicesEE |
| 80 | + |
| 81 | + customerEE <- H.liftAff $ DAO.fetchCustomer |
| 82 | + customerEE # either (\e -> H.modify_ $ _ { customer = Nothing, status = ErrorStatus "Failed to fetch customer." }) |
| 83 | + (either (\e -> H.modify_ $ _ { customer = Nothing, status = ErrorStatus "Decoding customer failed."}) |
| 84 | + (\x -> H.modify_ $ _ { customer = Just x })) |
| 85 | + spyM "customerEE" $ customerEE |
| 86 | + |
| 87 | + paymentMethodsEE <- H.liftAff $ DAO.listPaymentMethods |
| 88 | + paymentMethodsEE # either (\e -> H.modify_ $ _ { status = ErrorStatus "Failed to fetch payment methods." }) |
| 89 | + (either (\e -> H.modify_ $ _ { status = ErrorStatus "Decoding payment methods failed."}) |
| 90 | + (\x -> H.modify_ $ _ { paymentMethods = x.data })) |
| 91 | + spyM "paymentMethodsEE" $ paymentMethodsEE |
| 92 | + |
| 93 | + H.liftEffect $ log "FetchStuff done." |
| 94 | + |
| 95 | +-------------------------------------------------------------------------------- |
| 96 | + |
| 97 | +render :: ∀ m. MonadAff m => State -> ComponentHTML Action ChildSlots m |
| 98 | +render state = |
| 99 | + div [] |
| 100 | + [ p [] [ text $ if state.status == Ok then "" else "status: " <> show state.status ] |
| 101 | + , h2 [] [ text "Customer" ] |
| 102 | + , div [] (maybe [] (pure <<< customerHtml) state.customer) |
| 103 | + , h2 [] [ text "Invoices" ] |
| 104 | + , div [] |
| 105 | + (state.accounts <#> \account -> table [] |
| 106 | + (account.invoices <#> invoiceSummaryLineHtml) |
| 107 | + ) |
| 108 | + ] |
| 109 | + |
| 110 | +invoiceSummaryLineHtml :: ∀ m. MonadAff m => Stripe.Invoice -> ComponentHTML Action ChildSlots m |
| 111 | +invoiceSummaryLineHtml i = |
| 112 | + tr [] [ td [] [ text $ i.customer_email ] |
| 113 | + , td [] [ text $ i.account_name ] |
| 114 | + , td [] [ text $ i.currency ] |
| 115 | + , td [] [ text $ show i.amount_due ] |
| 116 | + ] |
| 117 | + |
| 118 | +customerHtml :: ∀ m. MonadAff m => Stripe.Customer -> ComponentHTML Action ChildSlots m |
| 119 | +customerHtml c = |
| 120 | + table [] |
| 121 | + [ tr [] [ th [] [ text "name" ] |
| 122 | + , td [] [ text $ fold c.name ] |
| 123 | + ] |
| 124 | + , tr [] [ th [] [ text "email" ] |
| 125 | + , td [] [ text $ c.email ] |
| 126 | + ] |
| 127 | + , tr [] [ th [] [ text "phone" ] |
| 128 | + , td [] [ text $ fold c.phone ] |
| 129 | + ] |
| 130 | + , tr [] [ th [] [ text "description" ] |
| 131 | + , td [] [ text $ fold c.description ] |
| 132 | + ] |
| 133 | + , tr [] [ th [] [ text "balance" ] |
| 134 | + , td [] [ text $ c.currency <> " " <> show c.balance <> " cents" ] |
| 135 | + ] |
| 136 | + ] |
| 137 | + |
| 138 | +-------------------------------------------------------------------------------- |
| 139 | + |
| 140 | +spyM :: ∀ m a. Applicative m => String -> a -> m Unit |
| 141 | +spyM tag value = do |
| 142 | + let dummy1 = spy tag value |
| 143 | + pure unit |
0 commit comments