aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFélix Sipma <felix.sipma@no-log.org>2018-02-16 15:59:15 +0100
committerFélix Sipma <felix.sipma@no-log.org>2018-02-16 15:59:15 +0100
commitc6cc9eceafbe0676dba03bb857b20ecd263c0d92 (patch)
tree2daa841396cf9dea0b81b992531abd60a5dacb05
parent00e54758461459dc82f7674f08ee600e4e7863c7 (diff)
manually define EncodeJson/DecodeJson instancesdeploy/shop
-rw-r--r--shop-client/psc-package.json4
-rw-r--r--shop-client/src/Shop.purs1
-rw-r--r--shop-client/src/Types.purs97
3 files changed, 55 insertions, 47 deletions
diff --git a/shop-client/psc-package.json b/shop-client/psc-package.json
index 67b5dfb..0f9b18d 100644
--- a/shop-client/psc-package.json
+++ b/shop-client/psc-package.json
@@ -5,10 +5,8 @@
"depends": [
"formatters",
"now",
- "argonaut-generic",
"dom-classy",
"psci-support",
- "css",
"halogen-css",
"foreign-generic",
"halogen",
@@ -22,4 +20,4 @@
"prelude",
"format"
]
-} \ No newline at end of file
+}
diff --git a/shop-client/src/Shop.purs b/shop-client/src/Shop.purs
index f9007ef..4c39a3f 100644
--- a/shop-client/src/Shop.purs
+++ b/shop-client/src/Shop.purs
@@ -4,7 +4,6 @@ import Prelude
import Control.Monad.Aff (Aff)
import Control.Monad.Eff (Eff, kind Effect)
import Control.Monad.Eff.Exception (EXCEPTION, throw)
-import CSS as CSS
import Data.Argonaut.Encode (encodeJson)
import Data.Argonaut.Decode (decodeJson)
import Data.Array (mapWithIndex, modifyAt)
diff --git a/shop-client/src/Types.purs b/shop-client/src/Types.purs
index 6832c81..e1fe3b1 100644
--- a/shop-client/src/Types.purs
+++ b/shop-client/src/Types.purs
@@ -1,11 +1,6 @@
module Types where
-import Prelude (class Show, class Eq, bind, pure, ($),
- (<=<), (<<<))
-import Control.Monad.Except (ExceptT(..))
-import Data.Bifunctor (lmap)
-import Data.Either (Either)
-import Data.Foreign (F, ForeignError(..))
+import Prelude (class Show, class Eq, bind, pure, ($))
import Data.Foreign.Class (class Decode, class Encode)
import Data.Foreign.Generic (defaultOptions, genericDecode, genericEncode)
import Data.Foreign.Generic.Types (Options)
@@ -13,14 +8,9 @@ import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
import Data.Newtype (class Newtype, unwrap)
-import Data.Argonaut.Core (Json, jsonEmptyObject)
-import Data.Argonaut.Decode (class DecodeJson, decodeJson, getField)
+import Data.Argonaut.Core (jsonEmptyObject)
+import Data.Argonaut.Decode (class DecodeJson, decodeJson, (.?))
import Data.Argonaut.Encode (class EncodeJson, (:=), (~>))
-import Data.Argonaut.Decode.Generic.Rep (genericDecodeJson)
-import Data.Argonaut.Encode.Generic.Rep (genericEncodeJson)
-import Data.Maybe (Maybe(..))
-import Data.Tuple (Tuple(..))
-import Data.MediaType.Common as MediaType
import Network.HTTP.Affjax.Response (class Respondable, ResponseType(..),
fromResponse)
@@ -45,21 +35,26 @@ newtype Product = Product
derive instance newtypeProduct :: Newtype Product _
instance showProduct :: Show Product where
- show = genericShow
+ show = genericShow
derive instance genericProduct :: Generic Product _
instance eqProduct :: Eq Product where
- eq x y = genericEq x y
+ eq x y = genericEq x y
instance decodeProduct :: Decode Product where
- decode x = genericDecode myOptions x
+ decode x = genericDecode myOptions x
instance encodeProduct :: Encode Product where
- encode x = genericEncode myOptions x
+ encode x = genericEncode myOptions x
instance encodeJsonProduct :: EncodeJson Product where
- encodeJson x = genericEncodeJson x
+ encodeJson (Product p)
+ = "nom" := p.nom
+ ~> "description" := p.description
+ ~> "poids" := p.poids
+ ~> "prix" := p.prix
+ ~> jsonEmptyObject
-- Shop
newtype Shop = Shop
@@ -69,18 +64,18 @@ newtype Shop = Shop
derive instance newtypeShop :: Newtype Shop _
instance showShop :: Show Shop where
- show = genericShow
+ show = genericShow
derive instance genericShop :: Generic Shop _
instance eqShop :: Eq Shop where
- eq x y = genericEq x y
+ eq x y = genericEq x y
instance decodeShop :: Decode Shop where
- decode x = genericDecode myOptions x
+ decode x = genericDecode myOptions x
instance encodeShop :: Encode Shop where
- encode x = genericEncode myOptions x
+ encode x = genericEncode myOptions x
-- Article
newtype Article = Article
@@ -91,21 +86,24 @@ newtype Article = Article
derive instance newtypeArticle :: Newtype Article _
instance showArticle :: Show Article where
- show = genericShow
+ show = genericShow
derive instance genericArticle :: Generic Article _
instance eqArticle :: Eq Article where
- eq x y = genericEq x y
+ eq x y = genericEq x y
instance decodeArticle :: Decode Article where
- decode x = genericDecode myOptions x
+ decode x = genericDecode myOptions x
instance encodeArticle :: Encode Article where
- encode x = genericEncode myOptions x
+ encode x = genericEncode myOptions x
instance encodeJsonArticle :: EncodeJson Article where
- encodeJson x = genericEncodeJson x
+ encodeJson (Article p)
+ = "product" := p.product
+ ~> "quantity" := p.quantity
+ ~> jsonEmptyObject
-- ChargeForm
newtype ChargeForm = ChargeForm
@@ -120,48 +118,61 @@ newtype ChargeForm = ChargeForm
derive instance newtypeChargeForm :: Newtype ChargeForm _
instance showChargeForm :: Show ChargeForm where
- show = genericShow
+ show = genericShow
derive instance genericChargeForm :: Generic ChargeForm _
instance eqChargeForm :: Eq ChargeForm where
- eq x y = genericEq x y
+ eq x y = genericEq x y
instance decodeChargeForm :: Decode ChargeForm where
- decode x = genericDecode myOptions x
+ decode x = genericDecode myOptions x
instance encodeChargeForm :: Encode ChargeForm where
- encode x = genericEncode myOptions x
+ encode x = genericEncode myOptions x
instance encodeJsonChargeForm :: EncodeJson ChargeForm where
- encodeJson x = genericEncodeJson x
+ encodeJson (ChargeForm p)
+ = "name" := p.name
+ ~> "email" := p.email
+ ~> "phone" := p.phone
+ ~> "date" := p.date
+ ~> "articles" := p.articles
+ ~> jsonEmptyObject
-- ChargeResponse
newtype ChargeResponse = ChargeResponse
- { chargeid :: String
- , chargeamount :: String
- }
+ { chargeid :: String
+ , chargeamount :: String
+ }
derive instance newtypeChargeResponse :: Newtype ChargeResponse _
instance decodeChargeResponse :: Decode ChargeResponse where
- decode x = genericDecode myOptions x
+ decode x = genericDecode myOptions x
instance encodeChargeResponse :: Encode ChargeResponse where
- encode x = genericEncode myOptions x
+ encode x = genericEncode myOptions x
derive instance genericChargeResponse :: Generic ChargeResponse _
instance showChargeResponse :: Show ChargeResponse where
- show = genericShow
+ show = genericShow
instance eqChargeResponse :: Eq ChargeResponse where
- eq x y = genericEq x y
+ eq x y = genericEq x y
instance decodeJsonChargeResponse :: DecodeJson ChargeResponse where
- decodeJson x = genericDecodeJson x
+ decodeJson x = do
+ obj <- decodeJson x
+ chargeid <- obj .? "chargeid"
+ chargeamount <- obj .? "chargeamount"
+ pure $ ChargeResponse
+ { chargeid : chargeid
+ , chargeamount : chargeamount
+ }
data ChargeResponseType
- = NoResponse
- | ResponseDecodeError String
- | ResponseSuccess ChargeResponse
+ = NoResponse
+ | ResponseDecodeError String
+ | ResponseSuccess ChargeResponse