Commit 5bac90ab authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[toestand] refactor renameable component

parent c27949bd
...@@ -5,6 +5,7 @@ import Data.Tuple.Nested ((/\)) ...@@ -5,6 +5,7 @@ import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
import Reactix as R import Reactix as R
import Reactix.DOM.HTML as H import Reactix.DOM.HTML as H
import Toestand as T
import Gargantext.Prelude import Gargantext.Prelude
...@@ -12,8 +13,8 @@ import Gargantext.Components.InputWithEnter (inputWithEnter) ...@@ -12,8 +13,8 @@ import Gargantext.Components.InputWithEnter (inputWithEnter)
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
thisModule :: String here :: R2.Here
thisModule = "Gargantext.Components.Renameable" here = R2.here "Gargantext.Components.Renameable"
type RenameableProps = type RenameableProps =
( (
...@@ -21,15 +22,15 @@ type RenameableProps = ...@@ -21,15 +22,15 @@ type RenameableProps =
, text :: String , text :: String
) )
renameable :: Record RenameableProps -> R.Element renameable :: R2.Component RenameableProps
renameable props = R.createElement renameableCpt props [] renameable = R.createElement renameableCpt
renameableCpt :: R.Component RenameableProps renameableCpt :: R.Component RenameableProps
renameableCpt = R.hooksComponentWithModule thisModule "renameableCpt" cpt renameableCpt = here.component "renameableCpt" cpt
where where
cpt {onRename, text} _ = do cpt { onRename, text } _ = do
isEditing <- R.useState' false isEditing <- T.useBox false
state <- R.useState' text state <- T.useBox text
textRef <- R.useRef text textRef <- R.useRef text
-- handle props change of text -- handle props change of text
...@@ -38,45 +39,72 @@ renameableCpt = R.hooksComponentWithModule thisModule "renameableCpt" cpt ...@@ -38,45 +39,72 @@ renameableCpt = R.hooksComponentWithModule thisModule "renameableCpt" cpt
pure unit pure unit
else do else do
R.setRef textRef text R.setRef textRef text
snd state $ const text T.write_ text state
pure $ H.div { className: "renameable" } [ pure $ H.div { className: "renameable" } [
renameableText { isEditing, onRename, state } renameableText { isEditing, onRename, state } []
] ]
type RenameableTextProps = type RenameableTextProps =
( (
isEditing :: R.State Boolean isEditing :: T.Box Boolean
, onRename :: String -> Effect Unit , onRename :: String -> Effect Unit
, state :: R.State String , state :: T.Box String
) )
renameableText :: Record RenameableTextProps -> R.Element renameableText :: R2.Component RenameableTextProps
renameableText props = R.createElement renameableTextCpt props [] renameableText = R.createElement renameableTextCpt
renameableTextCpt :: R.Component RenameableTextProps renameableTextCpt :: R.Component RenameableTextProps
renameableTextCpt = R.hooksComponentWithModule thisModule "renameableTextCpt" cpt renameableTextCpt = here.component "renameableText" cpt
where where
cpt {isEditing: (false /\ setIsEditing), state: (text /\ _)} _ = do cpt props@{ isEditing, state } _ = do
isEditing' <- T.useLive T.unequal isEditing
pure $ if isEditing' then
notEditing props []
else
editing props []
notEditing :: R2.Component RenameableTextProps
notEditing = R.createElement notEditingCpt
notEditingCpt :: R.Component RenameableTextProps
notEditingCpt = here.component "notEditing" cpt
where
cpt props@{ isEditing, state } _ = do
state' <- T.useLive T.unequal state
pure $ H.div { className: "input-group" } pure $ H.div { className: "input-group" }
[ H.input { className: "form-control" [ H.input { className: "form-control"
, defaultValue: text , defaultValue: state'
, disabled: 1 , disabled: 1
, type: "text" } , type: "text" }
, H.div { className: "btn input-group-append" , H.div { className: "btn input-group-append"
, on: { click: \_ -> setIsEditing $ const true } } , on: { click: \_ -> T.write_ true isEditing } }
[ H.span { className: "fa fa-pencil" } [] [ H.span { className: "fa fa-pencil" } []
] ]
] ]
cpt {isEditing: (true /\ setIsEditing), onRename, state: (text /\ setText)} _ = do
editing :: R2.Component RenameableTextProps
editing = R.createElement editingCpt
editingCpt :: R.Component RenameableTextProps
editingCpt = here.component "editing" cpt
where
cpt props@{ isEditing, onRename, state } _ = do
state' <- T.useLive T.unequal state
pure $ H.div { className: "input-group" } pure $ H.div { className: "input-group" }
[ inputWithEnter { [ inputWithEnter {
autoFocus: false autoFocus: false
, className: "form-control text" , className: "form-control text"
, defaultValue: text , defaultValue: state'
, onBlur: setText <<< const , onBlur: \s -> T.write_ s state
, onEnter: submit , onEnter: submit state'
, onValueChanged: setText <<< const , onValueChanged: \s -> T.write_ s state
, placeholder: "" , placeholder: ""
, type: "text" , type: "text"
} }
...@@ -86,6 +114,6 @@ renameableTextCpt = R.hooksComponentWithModule thisModule "renameableTextCpt" cp ...@@ -86,6 +114,6 @@ renameableTextCpt = R.hooksComponentWithModule thisModule "renameableTextCpt" cp
] ]
] ]
where where
submit _ = do submit text _ = do
setIsEditing $ const false T.write_ false isEditing
onRename text onRename text
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment