Commit 9db9c662 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

tree: preliminary file contents box added & some code simplification

Removed unnecessary record entries in function definitions.
parent 56c7def5
...@@ -48,6 +48,8 @@ filterNTree p (NTree x ary) = ...@@ -48,6 +48,8 @@ filterNTree p (NTree x ary) =
NTree x $ map (filterNTree p) $ filter (\(NTree a _) -> p a) ary NTree x $ map (filterNTree p) $ filter (\(NTree a _) -> p a) ary
type FTree = NTree LNode type FTree = NTree LNode
data FileType = CSV | PresseRIS
type UploadFileContents = String
type FileHash = String type FileHash = String
data Action = ShowPopOver ID data Action = ShowPopOver ID
...@@ -62,7 +64,8 @@ data Action = ShowPopOver ID ...@@ -62,7 +64,8 @@ data Action = ShowPopOver ID
| ShowRenameBox ID | ShowRenameBox ID
| CancelRename ID | CancelRename ID
| CurrentNode ID | CurrentNode ID
| UploadFile ID String | PrepareUploadFile ID UploadFileContents
| UploadFile ID FileType UploadFileContents
type State = { state :: FTree type State = { state :: FTree
...@@ -78,6 +81,8 @@ initialNode :: { createNode :: Boolean ...@@ -78,6 +81,8 @@ initialNode :: { createNode :: Boolean
, open :: Boolean , open :: Boolean
, popOver :: Boolean , popOver :: Boolean
, renameNodeValue :: String , renameNodeValue :: String
, droppedFileContents :: Maybe UploadFileContents
, showFileTypeBox :: Boolean
, showRenameBox :: Boolean , showRenameBox :: Boolean
} }
initialNode = { id : 3 initialNode = { id : 3
...@@ -88,6 +93,8 @@ initialNode = { id : 3 ...@@ -88,6 +93,8 @@ initialNode = { id : 3
, renameNodeValue : "" , renameNodeValue : ""
, createNode : false , createNode : false
, nodeValue : "InitialNode" , nodeValue : "InitialNode"
, droppedFileContents : Nothing
, showFileTypeBox : false
, showRenameBox : false} , showRenameBox : false}
initialState :: State initialState :: State
...@@ -141,8 +148,11 @@ performAction (SetNodeValue v nid) _ _ = ...@@ -141,8 +148,11 @@ performAction (SetNodeValue v nid) _ _ =
performAction (CurrentNode nid) _ _ = performAction (CurrentNode nid) _ _ =
modifyState_ $ \{state: s} -> {state: s, currentNode : Just nid} modifyState_ $ \{state: s} -> {state: s, currentNode : Just nid}
performAction (UploadFile nid contents) _ _ = do performAction (PrepareUploadFile nid contents) _ _ = do
hashes <- lift $ uploadFile nid contents modifyState_ $ mapFTree $ map $ toggleFileTypeBox nid contents
performAction (UploadFile nid fileType contents) _ _ = do
hashes <- lift $ uploadFile nid fileType contents
liftEffect $ log2 "uploaded:" hashes liftEffect $ log2 "uploaded:" hashes
...@@ -168,10 +178,14 @@ showPopOverNode :: ID -> LNode -> LNode ...@@ -168,10 +178,14 @@ showPopOverNode :: ID -> LNode -> LNode
showPopOverNode sid (LNode node) = showPopOverNode sid (LNode node) =
LNode $ node {showRenameBox = toggleIf (sid == node.id) node.showRenameBox} LNode $ node {showRenameBox = toggleIf (sid == node.id) node.showRenameBox}
toggleFileTypeBox :: ID -> UploadFileContents -> LNode -> LNode
toggleFileTypeBox sid contents (LNode node@{droppedFileContents: _, showFileTypeBox: true}) = LNode $ node {showFileTypeBox = true, droppedFileContents = Just contents}
toggleFileTypeBox sid _ (LNode node) = LNode $ node {showFileTypeBox = false, droppedFileContents = Nothing}
-- TODO: DRY, NTree.map -- TODO: DRY, NTree.map
showCreateNode :: ID -> NTree LNode -> NTree LNode showCreateNode :: ID -> NTree LNode -> NTree LNode
showCreateNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) ary) = showCreateNode sid (NTree (LNode node@{id, createNode}) ary) =
NTree (LNode {id,name, nodeType, open , popOver, renameNodeValue, createNode : createNode', nodeValue, showRenameBox}) $ map (showCreateNode sid) ary NTree (LNode $ node {createNode = createNode'}) $ map (showCreateNode sid) ary
where where
createNode' = if sid == id then not createNode else createNode createNode' = if sid == id then not createNode else createNode
...@@ -186,22 +200,22 @@ showCreateNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeV ...@@ -186,22 +200,22 @@ showCreateNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeV
-- TODO: DRY, NTree.map -- TODO: DRY, NTree.map
rename :: ID -> String -> NTree LNode -> NTree LNode rename :: ID -> String -> NTree LNode -> NTree LNode
rename sid v (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) ary) = rename sid v (NTree (LNode node@{id}) ary) =
NTree (LNode {id,name, nodeType, open , popOver , renameNodeValue : rvalue, createNode, nodeValue, showRenameBox}) $ map (rename sid v) ary NTree (LNode $ node {renameNodeValue = rvalue}) $ map (rename sid v) ary
where where
rvalue = if sid == id then v else "" rvalue = if sid == id then v else ""
-- TODO: DRY, NTree.map -- TODO: DRY, NTree.map
setNodeValue :: ID -> String -> NTree LNode -> NTree LNode setNodeValue :: ID -> String -> NTree LNode -> NTree LNode
setNodeValue sid v (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) ary) = setNodeValue sid v (NTree (LNode node@{id}) ary) =
NTree (LNode {id,name, nodeType, open , popOver , renameNodeValue , createNode, nodeValue : nvalue, showRenameBox}) $ map (setNodeValue sid v) ary NTree (LNode $ node {nodeValue = nvalue}) $ map (setNodeValue sid v) ary
where where
nvalue = if sid == id then v else "" nvalue = if sid == id then v else ""
-- TODO: DRY, NTree.map -- TODO: DRY, NTree.map
toggleNode :: ID -> NTree LNode -> NTree LNode toggleNode :: ID -> NTree LNode -> NTree LNode
toggleNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) ary) = toggleNode sid (NTree (LNode node@{id, open}) ary) =
NTree (LNode {id,name, nodeType, open : nopen, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) $ map (toggleNode sid) ary NTree (LNode $ node {open = nopen}) $ map (toggleNode sid) ary
where where
nopen = if sid == id then not open else open nopen = if sid == id then not open else open
...@@ -211,7 +225,17 @@ toggleNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue ...@@ -211,7 +225,17 @@ toggleNode sid (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue
-- Realistic Tree for the UI -- Realistic Tree for the UI
exampleTree :: NTree LNode exampleTree :: NTree LNode
exampleTree = NTree (LNode {id : 1, name : "", nodeType : Node, open : false, popOver : false, renameNodeValue : "", createNode : false, nodeValue : "", showRenameBox : false}) [] exampleTree = NTree (LNode { id : 1
, name : ""
, nodeType : Node
, open : false
, popOver : false
, renameNodeValue : ""
, createNode : false
, nodeValue : ""
, droppedFileContents: Nothing
, showFileTypeBox: false
, showRenameBox : false}) []
-- exampleTree :: NTree LNode -- exampleTree :: NTree LNode
-- exampleTree = -- exampleTree =
...@@ -293,7 +317,7 @@ treeview = simpleSpec defaultPerformAction render ...@@ -293,7 +317,7 @@ treeview = simpleSpec defaultPerformAction render
} ] } ]
renameTreeView :: (Action -> Effect Unit) -> FTree -> ID -> ReactElement renameTreeView :: (Action -> Effect Unit) -> FTree -> ID -> ReactElement
renameTreeView d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, showRenameBox }) ary) nid = renameTreeView d s@(NTree (LNode {id, name, renameNodeValue, showRenameBox }) ary) nid =
div [ className "" div [ className ""
, _id "rename-tooltip" , _id "rename-tooltip"
, _data {toggle: "tooltip", placement: "right"} , _data {toggle: "tooltip", placement: "right"}
...@@ -384,7 +408,7 @@ renameTreeView d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeV ...@@ -384,7 +408,7 @@ renameTreeView d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeV
createNodeView :: (Action -> Effect Unit) -> FTree -> ID -> ReactElement createNodeView :: (Action -> Effect Unit) -> FTree -> ID -> ReactElement
createNodeView d s@(NTree (LNode {id, name, nodeType, open, popOver, nodeValue }) ary) nid = createNodeView d s@(NTree (LNode { nodeValue }) ary) nid =
div [ className "" div [ className ""
, _id "create-node-tooltip" , _id "create-node-tooltip"
, _data {toggle: "tooltip", placement: "right"} , _data {toggle: "tooltip", placement: "right"}
...@@ -426,14 +450,14 @@ renameTreeViewDummy :: (Action -> Effect Unit) -> FTree -> ReactElement ...@@ -426,14 +450,14 @@ renameTreeViewDummy :: (Action -> Effect Unit) -> FTree -> ReactElement
renameTreeViewDummy d s = div [] [] renameTreeViewDummy d s = div [] []
popOverValue :: FTree -> Boolean popOverValue :: FTree -> Boolean
popOverValue (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, showRenameBox }) ary) = popOver popOverValue (NTree (LNode {popOver}) ary) = popOver
getCreateNodeValue :: FTree -> String getCreateNodeValue :: FTree -> String
getCreateNodeValue (NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, nodeValue, showRenameBox}) ary) = nodeValue getCreateNodeValue (NTree (LNode {nodeValue}) ary) = nodeValue
toHtml :: (Action -> Effect Unit) -> FTree -> Maybe ID -> ReactElement toHtml :: (Action -> Effect Unit) -> FTree -> Maybe ID -> ReactElement
toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode,nodeValue, showRenameBox }) []) n = toHtml d s@(NTree (LNode {id, name, nodeType, popOver, createNode}) []) n =
ul [] ul []
[ [
li [] $ [span [] li [] $ [span []
...@@ -447,7 +471,7 @@ toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, cr ...@@ -447,7 +471,7 @@ toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, cr
] ]
]] ]]
--- need to add renameTreeview value to this function --- need to add renameTreeview value to this function
toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, createNode, nodeValue, showRenameBox}) ary) n = toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, createNode}) ary) n =
ul [] ul []
[ li [] $ [ li [] $
( [span [onDrop dropHandler, onDragOver onDragOverHandler] [ ( [span [onDrop dropHandler, onDragOver onDragOverHandler] [
...@@ -480,7 +504,7 @@ toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, cr ...@@ -480,7 +504,7 @@ toHtml d s@(NTree (LNode {id, name, nodeType, open, popOver, renameNodeValue, cr
let blob = toBlob $ ff let blob = toBlob $ ff
void $ runAff (\_ -> pure unit) do void $ runAff (\_ -> pure unit) do
contents <- readAsText blob contents <- readAsText blob
liftEffect $ d $ UploadFile id contents liftEffect $ d $ PrepareUploadFile id contents
onDragOverHandler = \e -> do onDragOverHandler = \e -> do
-- prevent redirection when file is dropped -- prevent redirection when file is dropped
-- https://stackoverflow.com/a/6756680/941471 -- https://stackoverflow.com/a/6756680/941471
...@@ -492,7 +516,17 @@ fldr :: Boolean -> DOM.Props ...@@ -492,7 +516,17 @@ fldr :: Boolean -> DOM.Props
fldr open = if open then className "fas fa-folder-open" else className "fas fa-folder" fldr open = if open then className "fas fa-folder-open" else className "fas fa-folder"
newtype LNode = LNode {id :: ID, name :: String, nodeType :: NodeType, open :: Boolean, popOver :: Boolean, renameNodeValue :: String, nodeValue :: String, createNode :: Boolean, showRenameBox :: Boolean} newtype LNode = LNode { id :: ID
, name :: String
, nodeType :: NodeType
, open :: Boolean
, popOver :: Boolean
, renameNodeValue :: String
, nodeValue :: String
, createNode :: Boolean
, droppedFileContents :: Maybe UploadFileContents
, showFileTypeBox :: Boolean
, showRenameBox :: Boolean}
derive instance newtypeLNode :: Newtype LNode _ derive instance newtypeLNode :: Newtype LNode _
...@@ -502,7 +536,17 @@ instance decodeJsonLNode :: DecodeJson LNode where ...@@ -502,7 +536,17 @@ instance decodeJsonLNode :: DecodeJson LNode where
id_ <- obj .: "id" id_ <- obj .: "id"
name <- obj .: "name" name <- obj .: "name"
nodeType <- obj .: "type" nodeType <- obj .: "type"
pure $ LNode {id : id_, name, nodeType, open : true, popOver : false, renameNodeValue : "", createNode : false, nodeValue : "", showRenameBox : false} pure $ LNode { id : id_
, name
, nodeType
, open : true
, popOver : false
, renameNodeValue : ""
, createNode : false
, nodeValue : ""
, droppedFileContents: Nothing
, showFileTypeBox: false
, showRenameBox : false}
instance decodeJsonFTree :: DecodeJson (NTree LNode) where instance decodeJsonFTree :: DecodeJson (NTree LNode) where
decodeJson json = do decodeJson json = do
...@@ -540,8 +584,6 @@ instance encodeJsonCreateValue :: EncodeJson CreateValue where ...@@ -540,8 +584,6 @@ instance encodeJsonCreateValue :: EncodeJson CreateValue where
~> "files_id" := ([] :: Array String) ~> "files_id" := ([] :: Array String)
~> jsonEmptyObject ~> jsonEmptyObject
type UploadFileContents = String
createNode :: CreateValue -> Aff ID createNode :: CreateValue -> Aff ID
createNode = post $ urlPlease Back $ "new" createNode = post $ urlPlease Back $ "new"
...@@ -551,9 +593,11 @@ renameNode renameNodeId = put $ toUrl Back Node (Just renameNodeId) <> "/rename" ...@@ -551,9 +593,11 @@ renameNode renameNodeId = put $ toUrl Back Node (Just renameNodeId) <> "/rename"
deleteNode :: ID -> Aff ID deleteNode :: ID -> Aff ID
deleteNode = delete <<< toUrl Back Node <<< Just deleteNode = delete <<< toUrl Back Node <<< Just
uploadFile :: ID -> UploadFileContents -> Aff (Array FileHash) -- TODO: fileType
uploadFile id = postWwwUrlencoded $ toUrl Back Node (Just id) <> "/upload" uploadFile :: ID -> FileType -> UploadFileContents -> Aff (Array FileHash)
--uploadFile = postWwwUrlencoded $ urlPlease Back $ "upload" uploadFile id fileType fileContents = postWwwUrlencoded url fileContents
where
url = toUrl Back Node (Just id) <> "/upload"
-- UNUSED -- UNUSED
-- deleteNodes :: TODO -> Aff ID -- deleteNodes :: TODO -> Aff ID
......
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