KET5QGQPM5STWGRDL72HTZ5T57QRKQQ3L564PST2PNG4YJHKATSAC
3PFXXJTLLGDWIFVI32VDUSVGGQL73F6KBACLD2GGJO2AAIS4VPJAC
ANDJ6GEY2IRDNKPVXESYEZKU24BAXFB5PPSZFIJRMBGL57A622FQC
27H4DECZW4CEDSV5XYJQA5HOMUW73K5G2DBQNLQB7AFZXXVXCFCAC
YBLHJFCNW52TJ37UIHPZ6UD22SQVGG27SP5UQR7YAIJ7F7SYJZSAC
T2DN23M7W53UMRV46SKDP6UDMCZB7VG2J772LXKMAJNL6NA62MKAC
VTZT2ILU7VWP5EY4526HU72Z5HZB6VRVQIVJJTB6Q5NL2AUFZRSAC
3HTCTHHULQUAHAQFUKDIFO3S7FVVFXMAQLLS3T44MLHGDIT5DZGAC
NAFJ6RB3KYDBSTSNB3WQSVUQEPUGG2RZCBWRF4XNT2UKSOXDNMDQC
4GOBY5NQYPISPYKVN74SM7JYWV7PALUDWWGVXWRHW2J2CPPMC42QC
N6FG4EW6QU7V6QV7UHHYRA3EDKPGVCAEAT7IS3QI45N3GRRV2V7AC
DJATFGIC75CQDWMFHIWOKFXF26GKPINREMP6FNNTLF75JZZ3EQEQC
RFYEVKZQLOOQP536GRZOROSQW2O7TEHJ2HZDRVVUSBKLY5FBEO3QC
import Data.Tuple (Tuple)
-- import Data.Traversable (class Traversable, traverse)
import Data.Tuple (Tuple(..))
import Data.Traversable (traverse)
-- parseBillableJSON :: Object Json -> Either JsonDecodeError (Tuple BillableId Billable)
-- parseBillableJSON obj = do
-- billableId <- parseBillableIdJSON =<< obj .: "billableId"
-- bobj <- obj .: "billable"
-- amount <-
-- Zatoshi <$> (note (TypeMismatch "Failed to decode as Zatoshi") <<< BigInt.fromNumber)
-- =<< (_ .: "zatoshi")
-- =<< (bobj .: "amount")
-- gracePeriod <- Hours <$> bobj .: "gracePeriod"
-- recurrence <- parseRecurrence =<< bobj .: "recurrence"
parseRecurrence :: Json -> Either JsonDecodeError Recurrence
parseRecurrence json = do
obj <- decodeJson json
let annually = traverse (map \(_ :: Unit) -> Annually) (obj .:? "annually")
monthly = traverse (map Monthly) (obj .:? "monthly")
weekly = traverse (map Weekly) (obj .:? "weekly")
onetime = traverse (map \(_ :: Unit) -> OneTime) (obj .:? "onetime")
join $ note (UnexpectedValue json) (annually <|> monthly <|> weekly <|> onetime)
parseBillableJSON :: Object Json -> Either JsonDecodeError (Tuple BillableId Billable)
parseBillableJSON obj = do
billableId <- parseBillableIdJSON =<< obj .: "billableId"
bobj <- obj .: "billable"
name :: String <- bobj .: "name"
description :: String <- bobj .: "description"
let message = ""
recurrence :: Recurrence <- parseRecurrence =<< bobj .: "recurrence"
amount <-
map Zatoshi
$ (note (TypeMismatch "Failed to decode as Zatoshi") <<< BigInt.fromNumber)
=<< (_ .: "zatoshi")
=<< (bobj .: "amount")
gracePeriod <- Days <$> bobj .: "gracePeriod"
expiryPeriod <- Hours <$> bobj .: "gracePeriod"
pure $ Tuple billableId {name, description, message, recurrence, amount, gracePeriod, expiryPeriod }
runExceptT $ case response of
Left err -> throwError $ Error { status: Nothing, message: printError err }
Right r -> case r.status of
StatusCode 403 -> throwError $ Forbidden
StatusCode 200 -> withExceptT ParseFailure <<< except $ decodeJson r.body
other -> throwError $ Error { status: Just other, message: r.statusText }
parseResponse decodeJson response
parseResponse ::
forall a.
Decode a ->
Either AJAX.Error (Response Json) ->
Aff (Either APIError a)
parseResponse decode response =
runExceptT $ case response of
Left err -> throwError $ Error { status: Nothing, message: printError err }
Right r -> case r.status of
StatusCode 403 -> throwError $ Forbidden
StatusCode 200 -> withExceptT ParseFailure <<< except $ decode r.body
other -> throwError $ Error { status: Just other, message: r.statusText }
renderBillableList :: Array (Tuple BillableId Billable) -> H.ComponentHTML BillingAction Slots m
renderBillableList billables =
HH.div
[ P.classes (ClassName <$> [ "container-fluid" ]) ]
[ HH.section
[ P.id_ "projectOverview", P.classes (ClassName <$> [ "pt-3" ]) ]
([ HH.div
-- header
[ P.classes (ClassName <$> [ "row", "pt-3", "font-weight-bold" ]) ]
[ colmd2 (Just "Billable Name")
, colmd2 (Just "Description")
, colmd2 (Just "Amount")
, colmd2 (Just "Recurrence")
, colmd2 Nothing
]
] <> (billableRow <$> billables))
]
where
billableRow (Tuple bid b) =
HH.div
[ P.classes (ClassName <$> [ "row", "pt-3" ]) ]
[ colmd2 (Just b.name)
, colmd2 (Just b.description)
, colmd2 (Just (zecString <<< toZEC $ b.amount))
]
colmd2 :: forall i w. Maybe String -> HH.HTML i w
colmd2 xs = HH.div [ P.classes (ClassName <$> [ "col-md-2" ]) ] (U.fromMaybe $ HH.text <$> xs)
billables <- lift $ traverse (caps.listProjectBillables <<< (_.projectId) <<< unwrap) currentProject
case billables of
Nothing -> pure unit
Just (Left err) -> lift $ system.error (show err)
Just (Right b) -> H.modify_ (_ { billables = b })
refreshBillables currentProject
BillableCreated _ ->
pure unit
BillableCreated _ -> do
currentProject <- H.gets (_.selectedProject)
refreshBillables currentProject
where
refreshBillables currentProject = do
billables <- lift $ traverse (caps.listProjectBillables <<< (_.projectId) <<< unwrap) currentProject
case billables of
Nothing -> pure unit
Just (Left err) -> lift $ system.error (show err)
Just (Right b) -> H.modify_ (_ { billables = b })