User.purs 5.64 KB
Newer Older
Alexandre Delanoë's avatar
Alexandre Delanoë committed
1 2 3 4 5 6
module Gargantext.Components.Nodes.Annuaire.User
  ( module Gargantext.Components.Nodes.Annuaire.User.Contacts.Types
  , userLayout
  )
  where

7

8 9
import Gargantext.Prelude

Alexandre Delanoë's avatar
Alexandre Delanoë committed
10 11
import Data.Maybe (Maybe(..), fromMaybe)
import Effect (Effect)
12
import Effect.Aff (launchAff_)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
13
import Effect.Class (liftEffect)
14
import Gargantext.Components.App.Data (Boxes)
15
import Gargantext.Components.GraphQL.User (UserInfo)
16
import Gargantext.Components.Nodes.Annuaire.Tabs as Tabs
17
import Gargantext.Components.Nodes.Annuaire.User.Contact (getUserInfoWithReload, saveUserInfo, contactInfos)
18
import Gargantext.Components.Nodes.Annuaire.User.Contacts.Types (Contact(..), ContactData, ContactTouch(..), ContactWhere(..), ContactWho(..), HyperdataContact(..), HyperdataUser(..), _city, _country, _firstName, _labTeamDeptsJoinComma, _lastName, _mail, _office, _organizationJoinComma, _ouFirst, _phone, _role, _shared, _touch, _who, defaultContactTouch, defaultContactWhere, defaultContactWho, defaultHyperdataContact, defaultHyperdataUser)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
19
import Gargantext.Components.Nodes.Lists.Types as LT
20
import Gargantext.Config.REST (logRESTError)
21
import Gargantext.Config.Utils (handleRESTError)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
22 23 24
import Gargantext.Ends (Frontends)
import Gargantext.Hooks.Loader (useLoader)
import Gargantext.Routes as Routes
25 26
import Gargantext.Sessions (WithSession, WithSessionContext, sessionId)
import Gargantext.Types (FrontendError)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
27
import Gargantext.Utils.Reactix as R2
28
import Gargantext.Utils.Toestand as T2
29 30
import Reactix as R
import Reactix.DOM.HTML as H
31
import Record as Record
32
import Toestand as T
Alexandre Delanoë's avatar
Alexandre Delanoë committed
33

34 35
here :: R2.Here
here = R2.here "Gargantext.Components.Nodes.Annuaire.User"
Alexandre Delanoë's avatar
Alexandre Delanoë committed
36

37
type DisplayProps = ( title :: String )
Alexandre Delanoë's avatar
Alexandre Delanoë committed
38 39 40 41

display :: R2.Component DisplayProps
display = R.createElement displayCpt
displayCpt :: R.Component DisplayProps
42
displayCpt = here.component "display" cpt
Alexandre Delanoë's avatar
Alexandre Delanoë committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
  where
    cpt { title } children = do
      pure $ H.div { className: "container-fluid" }
        [ H.div { className: "row", id: "contact-page-header" }
          [ H.div { className: "col-md-6"} [ H.h3 {} [ H.text title ] ]
          , H.div { className: "col-md-8"} []
          , H.div { className: "col-md-2"} [ H.span {} [ H.text "" ] ]
          ]
        , H.div { className: "row", id: "contact-page-info" }
          [ H.div { className: "col-md-12" }
            [ H.div { className: "row" }
              [ H.div { className: "col-md-2" } [ H.img { src: "/images/Gargantextuel-212x300.jpg"} ]
              , H.div { className: "col-md-1"} []
              , H.div { className: "col-md-8"} children
57
              ]
Alexandre Delanoë's avatar
Alexandre Delanoë committed
58 59
            ]
          ]
60
        ]
Alexandre Delanoë's avatar
Alexandre Delanoë committed
61

62
{-
Alexandre Delanoë's avatar
Alexandre Delanoë committed
63 64
listElement :: Array R.Element -> R.Element
listElement = H.li { className: "list-group-item justify-content-between" }
65
-}
Alexandre Delanoë's avatar
Alexandre Delanoë committed
66

67
type LayoutNoSessionProps =
68 69 70
  ( boxes     :: Boxes
  , frontends :: Frontends
  , nodeId    :: Int
Alexandre Delanoë's avatar
Alexandre Delanoë committed
71 72
  )

73 74 75 76
type LayoutProps = WithSession LayoutNoSessionProps

type LayoutSessionContextProps = WithSessionContext LayoutNoSessionProps

Alexandre Delanoë's avatar
Alexandre Delanoë committed
77 78 79 80 81
type KeyLayoutProps = (
    key :: String
  | LayoutProps
  )

82 83
userLayout :: R2.Component LayoutProps
userLayout = R.createElement userLayoutCpt
Alexandre Delanoë's avatar
Alexandre Delanoë committed
84
userLayoutCpt :: R.Component LayoutProps
85
userLayoutCpt = here.component "userLayout" cpt
Alexandre Delanoë's avatar
Alexandre Delanoë committed
86
  where
87 88
    cpt props@{ nodeId
              , session } _ = do
Alexandre Delanoë's avatar
Alexandre Delanoë committed
89 90
      let sid = sessionId session

91
      pure $ userLayoutWithKey $ Record.merge props { key: show sid <> "-" <> show nodeId }
Alexandre Delanoë's avatar
Alexandre Delanoë committed
92

93
userLayoutWithKey :: R2.Leaf KeyLayoutProps
94
userLayoutWithKey = R2.leafComponent userLayoutWithKeyCpt
Alexandre Delanoë's avatar
Alexandre Delanoë committed
95
userLayoutWithKeyCpt :: R.Component KeyLayoutProps
96
userLayoutWithKeyCpt = here.component "userLayoutWithKey" cpt where
97
  cpt { boxes: boxes@{ sidePanelTexts }
98 99
      , frontends
      , nodeId
100
      , session } _ = do
101 102 103 104 105 106
    reload <- T.useBox T2.newReload
    reload' <- T.useLive T.unequal reload

    cacheState <- T.useBox LT.CacheOn

    useLoader { errorHandler
107
              , loader: getUserInfoWithReload
108
              , path: { nodeId, reload: reload', session }
109
              , render: \userInfo@{ ui_username } ->
110
                  H.ul { className: "col-md-12 list-group" } [
111 112
                    display { title: fromMaybe "no name" (Just ui_username) }
                    (contactInfos userInfo (onUpdateUserInfo boxes.errors reload))
113
                    , Tabs.tabs {
114 115
                         boxes
                       , cacheState
116
                       , defaultListId: 424242
117 118 119
                       , frontends
                       , nodeId
                       , session
120
                       , sidePanel: sidePanelTexts
121
                       }
122
                    ]
123 124
              }
    where
125
      errorHandler = logRESTError here "[userLayoutWithKey]"
126 127
      onUpdateUserInfo :: T.Box (Array FrontendError) -> T2.ReloadS -> UserInfo -> Effect Unit
      onUpdateUserInfo errors reload ui = do
128
        launchAff_ $ do
129 130 131 132
          res <- saveUserInfo session nodeId ui
          handleRESTError errors res $ \_ ->
            liftEffect $ T2.reload reload

133
--saveContactHyperdata :: Session -> Int -> HyperdataUser -> AffRESTError Int
134
--saveContactHyperdata session id = put session (Routes.NodeAPI Node (Just id) "")
Alexandre Delanoë's avatar
Alexandre Delanoë committed
135 136

-- | toUrl to get data XXX
137
--getContact :: Session -> Int -> AffRESTError ContactData
138 139 140 141 142 143 144 145 146 147 148
--getContact session id = do
--  eContactNode <- get session $ Routes.NodeAPI Node (Just id) ""
--  -- TODO: we need a default list for the pairings
--  --defaultListIds <- get $ toUrl endConfigStateful Back (Children NodeList 0 1 Nothing) $ Just id
--  --case (head defaultListIds :: Maybe (NodePoly HyperdataList)) of
--  --  Just (NodePoly { id: defaultListId }) ->
--  --    pure {contactNode, defaultListId}
--  --  Nothing ->
--  --    throwError $ error "Missing default list"
--  pure $ (\contactNode -> { contactNode, defaultListId: 424242 }) <$> eContactNode
--