SyncResetButton.purs 2.29 KB
Newer Older
1 2
module Gargantext.Components.NgramsTable.SyncResetButton where

3 4
import Gargantext.Prelude

5 6 7
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import FFI.Simple.Functions (delay)
8 9
import Gargantext.Components.Bootstrap as B
import Gargantext.Components.Bootstrap.Types (ButtonVariant(..), ComponentStatus(..), Variant(..))
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import Gargantext.Core.NgramsTable.Types (CoreAction(..), CoreDispatch, NgramsTablePatch)
import Gargantext.Utils.Reactix as R2
import Reactix as R
import Reactix.DOM.HTML as H
import Toestand as T

here :: R2.Here
here = R2.here "Gargantext.Components.NgramsTable.SyncResetButton"


-- | Reset Button
type SyncResetButtonsProps =
  ( afterSync        :: Unit -> Aff Unit
  , ngramsLocalPatch :: NgramsTablePatch
  , performAction    :: CoreDispatch
  )

syncResetButtons :: Record SyncResetButtonsProps -> R.Element
syncResetButtons p = R.createElement syncResetButtonsCpt p []
syncResetButtonsCpt :: R.Component SyncResetButtonsProps
syncResetButtonsCpt = here.component "syncResetButtons" cpt
  where
    cpt { afterSync, ngramsLocalPatch, performAction } _ = do
      synchronizing <- T.useBox false
      synchronizing' <- T.useLive T.unequal synchronizing

      let
        hasChanges = ngramsLocalPatch /= mempty

39 40 41 42 43 44 45
        statusReset _     true = Disabled
        statusReset false _ = Disabled
        statusReset _     _ = Enabled

        statusSync _     true = Deferred
        statusSync false _    = Disabled
        statusSync _     _    = Enabled
46 47 48 49 50 51 52 53 54 55 56 57

        resetClick _ = do
          performAction ResetPatches

        synchronizeClick _ = delay unit $ \_ -> do
          T.write_ true synchronizing
          performAction $ Synchronize { afterSync: newAfterSync }

        newAfterSync x = do
          afterSync x
          liftEffect $ T.write_ false synchronizing

58
      pure $
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        B.wad
        [ "d-flex" ]
        [
          B.button
          { variant: ButtonVariant Light
          , callback: resetClick
          , status: statusReset hasChanges synchronizing'
          }
          [ H.text "Reset" ]
        ,
          B.wad_ [ "mr-1", "d-inline-block" ]
        ,
          B.button
          { variant: ButtonVariant Primary
          , callback: synchronizeClick
          , status: statusSync hasChanges synchronizing'
          }
          [ H.text "Sync" ]
        ]