Skip to content

Instantly share code, notes, and snippets.

@codedmart
Last active December 4, 2015 13:51
Show Gist options
  • Select an option

  • Save codedmart/0edc033b0549660f8822 to your computer and use it in GitHub Desktop.

Select an option

Save codedmart/0edc033b0549660f8822 to your computer and use it in GitHub Desktop.
module Stores.App where
import Prelude
import Data.Generic
import Data.Maybe
-- import Control.Monad.Eff
-- import Control.Monad.Eff.Console (log)
import Data.Maybe.Unsafe (fromJust)
import Signal (Signal(), foldp, dropRepeats, mergeMany, constant)
import Data.Argonaut (Json(), (~>), (:=), (.?), jsonEmptyObject)
import Data.Argonaut.Encode (EncodeJson, gEncodeJson, encodeJson)
import Data.Argonaut.Decode (DecodeJson, gDecodeJson, decodeJson, decodeMaybe)
import Types.AspectRatio
foreign import data Blob :: *
type SvgData =
{ aspectRatio :: AspectRatio
, mimeType :: String
, name :: String
, size :: Int
, title :: String
, uploadType :: String
, userId :: Maybe String
}
newtype SvgDataNT = SvgDataNT SvgData
type AppUpload =
{ svg :: Maybe Blob
, svgData :: Maybe SvgData
, svgString :: Maybe String
}
type AppAlert =
{ message :: Maybe String
, type :: Maybe String
, wrapperClass :: Maybe String
, link :: Maybe String
}
newtype AppAlertNT = AppAlertNT
{ message :: Maybe String
, type :: Maybe String
, wrapperClass :: Maybe String
, link :: Maybe String
}
derive instance genericAppAlert :: Generic AppAlertNT
instance decodeJsonAppAlertNT :: DecodeJson AppAlertNT where
-- decodeJson = gDecodeJson
decodeJson json = do
obj <- decodeJson json
message <- obj .? "message"
tType <- obj .? "type"
wrapperClass <- obj .? "wrapperClass"
link <- obj .? "link"
pure $ AppAlertNT
{ message: message
, type: tType
, wrapperClass: wrapperClass
, link: link
}
instance encodeJsonAppAlertNT :: EncodeJson AppAlertNT where
-- encodeJson = gEncodeJson
encodeJson (AppAlertNT a)
= "message" := a.message
~> "type" := a.type
~> "wrapperClass" := a.wrapperClass
~> "link" := a.link
~> jsonEmptyObject
instance showAppAlertNT :: Show AppAlertNT where
show = gShow
appAlertNT :: AppAlertNT
appAlertNT = AppAlertNT
{ message: Nothing
, type: Nothing
, wrapperClass: Just "string"
, link: Nothing
}
encodedAppAlertNT :: Json
encodedAppAlertNT = encodeJson appAlertNT
decodedAppAlertNT :: Maybe AppAlertNT
-- decodedAppAlertNT = decodeJson "{\"message\": null, \"type\": null, \"wrapperClass\": \"string\", \"link\": null}"
decodedAppAlertNT = decodeMaybe encodedAppAlertNT
createGraph :: { fromJs :: Json } -> { fromJs :: Signal Json }
createGraph init = { fromJs: fromJs } where
fromJs :: Signal Json
fromJs = constant init.fromJs
marshalled :: Signal (Maybe AppAlert)
marshalled = map (decodeMaybe >>> map runAppAlert) fromJs
runAppAlert :: AppAlertNT -> AppAlert
runAppAlert (AppAlertNT a) = a
type AppState =
{ upload :: AppUpload
, alert :: AppAlert
, loginModalIsOpen :: Boolean
, cartModalIsOpen :: Boolean
, modalImageWarningIsOpen :: Boolean
, modalVectorWarningIsOpen :: Boolean
, openModals :: Array String
, localStorageAvailable :: Boolean
}
data Action =
NoOp
| Update AppState
| Alert AppAlert
| Upload AppUpload
| ToggleLoginModal Boolean
initialUpload :: AppUpload
initialUpload =
{ svg: Nothing
, svgData: Nothing
, svgString: Nothing
}
initialAlert :: AppAlert
initialAlert =
{ message: Nothing
, type: Nothing
, wrapperClass: Nothing
, link: Nothing
}
initialState :: AppState
initialState =
{ upload: initialUpload
, alert: initialAlert
, loginModalIsOpen: false
, cartModalIsOpen: false
, modalImageWarningIsOpen: false
, modalVectorWarningIsOpen: false
, openModals: []
, localStorageAvailable: true
}
update :: Action -> AppState -> AppState
update action state = case action of
Update st -> st
Alert alrt -> state { alert = alrt }
Upload upld -> state
ToggleLoginModal bl -> state { loginModalIsOpen = bl }
NoOp -> state
actions :: Maybe (Signal Action)
actions =
mergeMany
[ map ToggleLoginModal toggleLoginModal
, map Alert alert
, map Upload upload
]
alert :: Signal AppAlert
alert = constant initialAlert
upload :: Signal AppUpload
upload = constant initialUpload
toggleLoginModal :: Signal Boolean
toggleLoginModal = constant false
changes :: Signal AppState
changes = foldp update initialState $ fromJust actions
alertChanges :: Signal Json
alertChanges = map (\m -> encodeJson $ AppAlertNT m.alert) changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment