11module Main where
22
33import Prelude
4- import React.DOM as D
5- import React.DOM.Props as P
4+
65import Control.Monad.Eff (Eff )
76import Control.Monad.Eff.Alert (ALERT , alert )
87import Control.Monad.Eff.Console (CONSOLE , log )
@@ -18,18 +17,19 @@ import Data.AddressBook (Address(..), Person(..), PhoneNumber(..), PhoneType(..)
1817import Data.AddressBook.Validation (Errors , validatePerson' )
1918import Data.Array ((..), length , modifyAt , zipWith )
2019import Data.Either (Either (..))
21- import Data.Foldable (for_ )
22- import Data.Foreign (ForeignError , readString , toForeign )
23- import Data.Foreign.Class (class IsForeign , readJSON , read , readProp )
24- import Data.Foreign.Index ( prop )
25- import Data.Foreign.Null ( unNull )
26- import Data.JSON ( stringify )
20+ import Data.Foldable (foldMap , for_ )
21+ import Data.Foreign (ForeignError , readNullOrUndefined , readString , renderForeignError , toForeign )
22+ import Data.Foreign.Class (class Decode , class Encode )
23+ import Data.Foreign.Generic ( decodeJSON , defaultOptions , encodeJSON , genericDecode , genericEncode )
24+ import Data.Foreign.Index ( index )
25+ import Data.Generic.Rep ( class Generic )
2726import Data.List.NonEmpty (NonEmptyList )
2827import Data.Maybe (Maybe (..), fromJust , fromMaybe )
29- import Data.Nullable (toMaybe )
3028import Data.Traversable (traverse )
3129import Partial.Unsafe (unsafePartial )
3230import React (ReactClass , ReadWrite , ReactState , Event , ReactThis , createFactory , readState , spec , createClass , writeState )
31+ import React.DOM as D
32+ import React.DOM.Props as P
3333import ReactDOM (render )
3434
3535newtype AppState = AppState
@@ -65,24 +65,13 @@ newtype FormData = FormData
6565 , cellPhone :: String
6666 }
6767
68- instance formDataIsForeign :: IsForeign FormData where
69- read value = do
70- firstName <- readProp " firstName" value
71- lastName <- readProp " lastName" value
72- street <- readProp " street" value
73- city <- readProp " city" value
74- state <- readProp " state" value
75- homePhone <- readProp " homePhone" value
76- cellPhone <- readProp " cellPhone" value
77- pure $ FormData
78- { firstName
79- , lastName
80- , street
81- , city
82- , state
83- , homePhone
84- , cellPhone
85- }
68+ derive instance genericFormData :: Generic FormData _
69+
70+ instance decodeFormData :: Decode FormData where
71+ decode = genericDecode (defaultOptions { unwrapSingleConstructors = true })
72+
73+ instance encodeFormData :: Encode FormData where
74+ encode = genericEncode (defaultOptions { unwrapSingleConstructors = true })
8675
8776toFormData :: Partial => Person -> FormData
8877toFormData (Person p@{ homeAddress: Address a
@@ -113,12 +102,12 @@ loadSavedData = do
113102 let
114103 savedData :: Either (NonEmptyList ForeignError ) (Maybe FormData )
115104 savedData = runExcept do
116- jsonOrNull <- read item
117- traverse readJSON (unNull jsonOrNull)
105+ jsonOrNull <- traverse readString =<< readNullOrUndefined item
106+ traverse decodeJSON jsonOrNull
118107
119108 case savedData of
120109 Left err -> do
121- alert $ " Unable to read saved form data: " <> show err
110+ alert $ " Unable to read saved form data: " <> foldMap (( " \n " <> _) <<< renderForeignError) err
122111 pure Nothing
123112 Right mdata -> pure mdata
124113
@@ -136,13 +125,13 @@ validateAndSaveEntry person = do
136125 case validatePerson' person of
137126 Left errs -> alert $ " There are " <> show (length errs) <> " validation errors."
138127 Right result -> do
139- setItem " person" $ stringify $ toForeign $ unsafePartial toFormData result
128+ setItem " person" $ encodeJSON $ unsafePartial toFormData result
140129 alert " Saved"
141130
142131valueOf :: Event -> Either (NonEmptyList ForeignError ) String
143132valueOf e = runExcept do
144- target <- prop " target " (toForeign e)
145- value <- prop " value" target
133+ target <- index (toForeign e) " target "
134+ value <- index target " value"
146135 readString value
147136
148137updateAppState
@@ -249,4 +238,4 @@ main = void do
249238 let component = D .div [] [ createFactory (addressBook (initialState formData)) unit ]
250239 doc <- window >>= document
251240 ctr <- getElementById (ElementId " main" ) (documentToNonElementParentNode (htmlDocumentToDocument doc))
252- render component (unsafePartial fromJust (toMaybe ctr) )
241+ render component (unsafePartial fromJust ctr)
0 commit comments