DocumentFormCreation.purs 7.09 KB
Newer Older
arturo's avatar
arturo committed
1 2 3
module Gargantext.Components.DocsTable.DocumentFormCreation
  ( documentFormCreation
  , FormData
arturo's avatar
arturo committed
4
  , create, createProgress
arturo's avatar
arturo committed
5 6 7 8 9 10 11
  ) where

import Gargantext.Prelude

import DOM.Simple.Console (log3)
import Data.Either (Either(..))
import Data.Foldable (foldl, intercalate)
arturo's avatar
arturo committed
12
import Data.Maybe (Maybe(..))
arturo's avatar
arturo committed
13 14
import Effect (Effect)
import Gargantext.Components.Bootstrap as B
15
import Gargantext.Components.Bootstrap.Types (ButtonVariant(..), ComponentStatus(..), Variant(..))
16
import Gargantext.Config.REST (AffRESTError)
arturo's avatar
arturo committed
17 18 19
import Gargantext.Hooks.FormValidation (VForm, useFormValidation)
import Gargantext.Hooks.FormValidation.Unboxed as FV
import Gargantext.Hooks.StateRecord (useStateRecord)
arturo's avatar
arturo committed
20 21 22
import Gargantext.Routes as GR
import Gargantext.Sessions (Session, post, get)
import Gargantext.Types as GT
arturo's avatar
arturo committed
23 24 25 26
import Gargantext.Utils (nbsp, (?))
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
arturo's avatar
arturo committed
27
import Record as Record
arturo's avatar
arturo committed
28
import Record.Extra (pick)
arturo's avatar
arturo committed
29
import Type.Proxy (Proxy(..))
arturo's avatar
arturo committed
30 31 32 33 34 35 36 37 38 39

type Props =
  ( callback  :: Record FormData -> Effect Unit
  , status    :: ComponentStatus
  | Options
  )

type Options = ( | FormData )

options :: Record Options
arturo's avatar
arturo committed
40
options = Record.merge {} defaultData
arturo's avatar
arturo committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

documentFormCreation :: forall r. R2.OptLeaf Options Props r
documentFormCreation = R2.optLeaf component options

component :: R.Component Props
component = R.hooksComponent "documentFormCreation" cpt where
  cpt props _ = do
    -- Hooks
    { state, bindStateKey } <- useStateRecord (pick props :: Record FormData)
    fv <- useFormValidation

    -- @onSubmit: exec whole form validation and execute callback
    onSubmit <- pure $ do

      result <- fv.try (\_ -> documentFormValidation state)

      case result of
arturo's avatar
arturo committed
58
        Left err -> log3 "documentFormCreation validation error" state err
arturo's avatar
arturo committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
        Right _  -> props.callback state

    -- Render
    pure $

      H.form
      { className: "document-form-creation" }
      [
        -- Title
        H.div
        { className: intercalate " "
            [ "form-group"
            , (fv.hasError' "title") ?
                "form-group--error" $
                mempty
            ]
        }
        [
          H.div { className: "form-group__label" }
          [
            H.label {} [ H.text "Title" ]
          ]
        ,
          H.div { className: "form-group__field" }
          [
            B.formInput $
              bindStateKey "title"
          ,
arturo's avatar
arturo committed
87
            R2.when (fv.hasError' "title") $
arturo's avatar
arturo committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
              H.div { className: "form-group__error" }
              [ H.text "Please enter a title" ]
          ]
        ]
      ,
        -- Source
        H.div
        { className: intercalate " "
            [ "form-group"
            , (fv.hasError' "source") ?
                "form-group--error" $
                mempty
            ]
        }
        [
          H.div { className: "form-group__label" }
          [
            H.label {} [ H.text "Source" ]
          ]
        ,
          H.div { className: "form-group__field" }
          [
            B.formInput $
              bindStateKey "source"
          ,
arturo's avatar
arturo committed
113
            R2.when (fv.hasError' "source") $
arturo's avatar
arturo committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
              H.div { className: "form-group__error" }
              [ H.text "Please enter a source" ]
          ]
        ]
      ,
        -- Authors
        H.div
        { className: intercalate " "
            [ "form-group"
            , (fv.hasError' "authors") ?
                "form-group--error" $
                mempty
            ]
        }
        [
          H.div { className: "form-group__label" }
          [
            H.label {} [ H.text "Authors" ]
          ]
        ,
          H.div { className: "form-group__field" }
          [
            B.formInput $
            { placeholder: "ex: author1, author2, …"
arturo's avatar
arturo committed
138
            } `Record.merge` bindStateKey "authors"
arturo's avatar
arturo committed
139
          ,
arturo's avatar
arturo committed
140
            R2.when (fv.hasError' "authors") $
arturo's avatar
arturo committed
141 142 143 144
              H.div { className: "form-group__error" }
              [ H.text "Please enter at least one author" ]
          ]
        ]
arturo's avatar
arturo committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
      ,
        -- Date
        H.div
        { className: intercalate " "
            [ "form-group"
            , (fv.hasError' "date") ?
                "form-group--error" $
                mempty
            ]
        }
        [
          H.div
          { className: "form-group__label" }
          [
            H.label {} [ H.text $ "Date" ]
          ]
        ,
          H.div
          { className: "form-group__field" }
          [
            B.formInput $
            { type: "date"
            } `Record.merge` bindStateKey "date"
          ,
arturo's avatar
arturo committed
169
            R2.when (fv.hasError' "date") $
arturo's avatar
arturo committed
170 171 172 173
              H.div { className: "form-group__error" }
              [ H.text "Please enter a valid date" ]
          ]
        ]
arturo's avatar
arturo committed
174 175 176 177 178 179 180 181
      ,
        -- Abstract
        H.div
        { className: intercalate " "
            [ "form-group"
            ]
        }
        [
arturo's avatar
arturo committed
182 183
          H.div
          { className: "form-group__label" }
arturo's avatar
arturo committed
184 185 186 187 188 189 190 191
          [
            H.label {} [ H.text $ "Abstract" <> nbsp 1 ]
          ,
            H.span
            { className: "form-group__label--sub" }
            [ H.text "optional" ]
          ]
        ,
arturo's avatar
arturo committed
192 193
          H.div
          { className: "form-group__field" }
arturo's avatar
arturo committed
194 195 196
          [
            B.formTextarea $
            { rows: 5
arturo's avatar
arturo committed
197
            } `Record.merge` bindStateKey "abstract"
arturo's avatar
arturo committed
198 199 200 201 202 203 204 205 206
          ]
        ]
      ,
        -- Submit
        H.div { className: "document-form-creation__submit" }
        [
          B.button
          { callback: \_ -> onSubmit
          , status: props.status == Deferred ? Deferred $ Enabled
207
          , variant: ButtonVariant Primary
arturo's avatar
arturo committed
208 209 210 211 212 213 214 215 216 217 218
          , type: "submit"
          , block: true
          }
          [ H.text "Add" ]
        ]
      ]

type FormData =
  ( title     :: String
  , source    :: String
  , authors   :: String
arturo's avatar
arturo committed
219
  , date      :: String
arturo's avatar
arturo committed
220 221 222 223 224 225 226 227
  , abstract  :: String
  )

defaultData :: Record FormData
defaultData =
  { title     : ""
  , source    : ""
  , authors   : ""
arturo's avatar
arturo committed
228
  , date      : ""
arturo's avatar
arturo committed
229 230 231 232 233 234 235 236 237 238
  , abstract  : ""
  }

documentFormValidation :: Record FormData -> Effect VForm
documentFormValidation r = foldl append mempty rules
  where
    rules =
      [ FV.nonEmpty "title" r.title
      , FV.nonEmpty "source" r.source
      , FV.nonEmpty "authors" r.authors
arturo's avatar
arturo committed
239
      , FV.date "date" r.date
arturo's avatar
arturo committed
240
      ]
arturo's avatar
arturo committed
241 242 243 244 245 246 247 248


---------------------------------------------------

create ::
     Session
  -> GT.ID
  -> Record FormData
249
  -> AffRESTError GT.AsyncTaskWithType
arturo's avatar
arturo committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
create session nodeId =
      rename
  >>> post session request
  >=> case _ of
    Left err   -> pure $ Left err
    Right task -> pure $ Right $ GT.AsyncTaskWithType
      { task
      , typ: GT.NodeDocument
      }

  where

    request = GR.NodeAPI GT.Node (Just nodeId)
      (GT.asyncTaskTypePath GT.NodeDocument)

    rename = Record.rename
      (Proxy :: Proxy "source")
      (Proxy :: Proxy "sources")

createProgress ::
    Session
  -> GT.ID
  -> GT.AsyncTaskWithType
273
  -> AffRESTError GT.AsyncProgress
arturo's avatar
arturo committed
274 275 276 277 278 279 280 281 282 283 284 285
createProgress
  session
  nodeId
  (GT.AsyncTaskWithType { task: GT.AsyncTask { id } })
  =
    get session request

  where

    request = GR.NodeAPI GT.Node (Just nodeId)
      (GT.asyncTaskTypePath GT.NodeDocument <> pollParams)

arturo's avatar
arturo committed
286
    pollParams = "/" <> id <> "/poll?limit1"