Node.purs 1.69 KB
Newer Older
1
module Gargantext.Components.Node
2 3
  where

4 5
import Gargantext.Prelude

6
import Data.Eq.Generic (genericEq)
7 8
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..))
9 10
import Data.Newtype (class Newtype)
import Simple.JSON as JSON
11

12 13 14 15 16 17 18
type NodePolyCommon a =
  ( id :: Int
  , typename :: Int
  , name :: String
  , date :: String
  , hyperdata :: a )

19
newtype NodePoly a =
20
  NodePoly { userId   :: Int
21
           , parentId  :: Maybe Int
22
           | NodePolyCommon a
23
           }
24
derive instance Generic (NodePoly a) _
25 26 27 28
derive instance Newtype (NodePoly a) _
instance Eq a => Eq (NodePoly a) where eq = genericEq
instance JSON.ReadForeign a => JSON.ReadForeign (NodePoly a) where
  readImpl f = do
29
    inst :: { user_id :: Int, parent_id :: Maybe Int | NodePolyCommon a } <- JSON.readImpl f
30 31 32 33 34 35 36
    pure $ NodePoly { id: inst.id
                    , typename: inst.typename
                    , userId: inst.user_id
                    , parentId: inst.parent_id
                    , name: inst.name
                    , date: inst.date
                    , hyperdata: inst.hyperdata }
37 38 39 40 41 42 43 44 45
instance JSON.WriteForeign a => JSON.WriteForeign (NodePoly a) where
  writeImpl (NodePoly np) = do
    JSON.writeImpl { user_id   : np.userId
                   , parent_id : np.parentId
                   , id        : np.id
                   , typename  : np.typename
                   , name      : np.name
                   , date      : np.date
                   , hyperdata : np.hyperdata}
46

47
newtype HyperdataList = HyperdataList { preferences :: Maybe String }
48 49
derive instance Generic HyperdataList _
derive instance Newtype HyperdataList _
50
derive instance Eq HyperdataList
51
derive newtype instance JSON.ReadForeign HyperdataList