Commit ee0a7e98 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[annuaire] implementation of file upload for annuaire

parent 6eb24204
...@@ -169,7 +169,7 @@ performAction session (NTree (LNode {id}) _) (_ /\ setReload) (_ /\ setOpenNodes ...@@ -169,7 +169,7 @@ performAction session (NTree (LNode {id}) _) (_ /\ setReload) (_ /\ setOpenNodes
setOpenNodes (Set.insert (mkNodeId session id)) setOpenNodes (Set.insert (mkNodeId session id))
setReload (_ + 1) setReload (_ + 1)
performAction session (NTree (LNode {id}) _) _ _ (_ /\ setAsyncTasks) (UploadFile fileType contents) = do performAction session (NTree (LNode {id}) _) _ _ (_ /\ setAsyncTasks) (UploadFile nodeType fileType contents) = do
task <- uploadFile session id fileType contents task <- uploadFile session nodeType id fileType contents
liftEffect $ setAsyncTasks $ A.cons task liftEffect $ setAsyncTasks $ A.cons task
liftEffect $ log2 "uploaded, task:" task liftEffect $ log2 "uploaded, task:" task
...@@ -205,7 +205,8 @@ settingsBox Annuaire = SettingsBox { ...@@ -205,7 +205,8 @@ settingsBox Annuaire = SettingsBox {
show: true show: true
, edit : false , edit : false
, doc : Documentation Annuaire , doc : Documentation Annuaire
, buttons : [Delete] , buttons : [ Upload
, Delete ]
} }
settingsBox _ = SettingsBox { settingsBox _ = SettingsBox {
......
...@@ -18,7 +18,7 @@ data Action = CreateSubmit String GT.NodeType ...@@ -18,7 +18,7 @@ data Action = CreateSubmit String GT.NodeType
| DeleteNode | DeleteNode
| SearchQuery GT.AsyncTaskWithType | SearchQuery GT.AsyncTaskWithType
| Submit String | Submit String
| UploadFile FileType UploadFileContents | UploadFile GT.NodeType FileType UploadFileContents
----------------------------------------------------- -----------------------------------------------------
-- UploadFile Action -- UploadFile Action
......
...@@ -24,6 +24,7 @@ import Gargantext.Utils.Reactix as R2 ...@@ -24,6 +24,7 @@ import Gargantext.Utils.Reactix as R2
type Props = type Props =
( id :: Int ( id :: Int
, nodeType :: GT.NodeType
, session :: Session , session :: Session
) )
...@@ -34,7 +35,7 @@ uploadFileView d props = R.createElement (uploadFileViewCpt d) props [] ...@@ -34,7 +35,7 @@ uploadFileView d props = R.createElement (uploadFileViewCpt d) props []
uploadFileViewCpt :: (Action -> Aff Unit) -> R.Component Props uploadFileViewCpt :: (Action -> Aff Unit) -> R.Component Props
uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt
where where
cpt {id} _ = do cpt {id, nodeType} _ = do
mContents :: R.State (Maybe UploadFileContents) <- R.useState' Nothing mContents :: R.State (Maybe UploadFileContents) <- R.useState' Nothing
fileType :: R.State FileType <- R.useState' CSV fileType :: R.State FileType <- R.useState' CSV
lang :: R.State (Maybe Lang) <- R.useState' (Just EN) lang :: R.State (Maybe Lang) <- R.useState' (Just EN)
...@@ -65,7 +66,7 @@ uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt ...@@ -65,7 +66,7 @@ uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt
} (map renderOptionLang [EN, FR]) } (map renderOptionLang [EN, FR])
] ]
, H.div {} [ uploadButton d id mContents fileType lang ] , H.div {} [ uploadButton {action: d, fileType, lang, id, mContents, nodeType } ]
] ]
renderOptionFT :: FileType -> R.Element renderOptionFT :: FileType -> R.Element
...@@ -97,42 +98,54 @@ uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt ...@@ -97,42 +98,54 @@ uploadFileViewCpt d = R.hooksComponent "UploadFileView" cpt
$ R2.unsafeEventValue e $ R2.unsafeEventValue e
uploadButton :: (Action -> Aff Unit) type UploadButtonProps =
-> Int (
-> R.State (Maybe UploadFileContents) action :: Action -> Aff Unit
-> R.State FileType , fileType :: R.State FileType
-> R.State (Maybe Lang) , id :: Int
-> R.Element , lang :: R.State (Maybe Lang)
uploadButton d id (mContents /\ setMContents) (fileType /\ setFileType) (lang /\ setLang) = , mContents :: R.State (Maybe UploadFileContents)
H.button {className: "btn btn-primary", disabled, on: {click: onClick}} [ H.text "Upload" ] , nodeType :: GT.NodeType
where )
disabled = case mContents of
Nothing -> "1"
Just _ -> ""
onClick e = do uploadButton :: Record UploadButtonProps -> R.Element
let contents = unsafePartial $ fromJust mContents uploadButton props = R.createElement uploadButtonCpt props []
void $ launchAff do
_ <- d $ UploadFile fileType contents uploadButtonCpt :: R.Component UploadButtonProps
liftEffect $ do uploadButtonCpt = R.hooksComponent "G.C.F.T.N.A.U.uploadButton" cpt
setMContents $ const $ Nothing where
setFileType $ const $ CSV cpt {action, fileType: (fileType /\ setFileType), id, lang: (lang /\ setLang), mContents: (mContents /\ setMContents), nodeType} _ = do
setLang $ const $ Just EN pure $ H.button {className: "btn btn-primary", disabled, on: {click: onClick}} [ H.text "Upload" ]
where
disabled = case mContents of
Nothing -> "1"
Just _ -> ""
onClick e = do
let contents = unsafePartial $ fromJust mContents
void $ launchAff do
_ <- action $ UploadFile nodeType fileType contents
liftEffect $ do
setMContents $ const $ Nothing
setFileType $ const $ CSV
setLang $ const $ Just EN
-- START File Type View -- START File Type View
type FileTypeProps = type FileTypeProps =
( id :: ID ( action :: Action -> Aff Unit
, nodeType :: GT.NodeType) , droppedFile :: R.State (Maybe DroppedFile)
, id :: ID
fileTypeView :: (Action -> Aff Unit) , isDragOver :: R.State Boolean
-> Record FileTypeProps , nodeType :: GT.NodeType
-> R.State (Maybe DroppedFile) )
-> R.State Boolean
-> R.Element fileTypeView :: Record FileTypeProps -> R.Element
fileTypeView d p (Just (DroppedFile {contents, fileType}) /\ setDroppedFile) (_ /\ setIsDragOver) = R.createElement el p [] fileTypeView p = R.createElement fileTypeViewCpt p []
fileTypeViewCpt :: R.Component FileTypeProps
fileTypeViewCpt = R.hooksComponent "G.C.F.T.N.A.U.fileTypeView" cpt
where where
el = R.hooksComponent "FileTypeView" cpt cpt {action, droppedFile: (Just (DroppedFile {contents, fileType}) /\ setDroppedFile), isDragOver: (_ /\ setIsDragOver), nodeType} _ = do
cpt {id} _ = do
pure $ H.div tooltipProps $ pure $ H.div tooltipProps $
[ H.div {className: "panel panel-default"} [ H.div {className: "panel panel-default"}
[ panelHeading [ panelHeading
...@@ -184,7 +197,7 @@ fileTypeView d p (Just (DroppedFile {contents, fileType}) /\ setDroppedFile) (_ ...@@ -184,7 +197,7 @@ fileTypeView d p (Just (DroppedFile {contents, fileType}) /\ setDroppedFile) (_
, type: "button" , type: "button"
, on: {click: \_ -> do , on: {click: \_ -> do
setDroppedFile $ const Nothing setDroppedFile $ const Nothing
launchAff $ d $ UploadFile ft contents launchAff $ action $ UploadFile nodeType ft contents
} }
} [H.text "Upload"] } [H.text "Upload"]
Nothing -> Nothing ->
...@@ -192,10 +205,10 @@ fileTypeView d p (Just (DroppedFile {contents, fileType}) /\ setDroppedFile) (_ ...@@ -192,10 +205,10 @@ fileTypeView d p (Just (DroppedFile {contents, fileType}) /\ setDroppedFile) (_
, type: "button" , type: "button"
} [H.text "Upload"] } [H.text "Upload"]
] ]
fileTypeView _ _ (Nothing /\ _) _ = R.createElement el {} []
where cpt {droppedFile: (Nothing /\ _)} _ = do
el = R.hooksComponent "FileTypeView" cpt pure $ H.div {} []
cpt props _ = pure $ H.div {} []
newtype FileUploadQuery = FileUploadQuery { newtype FileUploadQuery = FileUploadQuery {
fileType :: FileType fileType :: FileType
...@@ -208,15 +221,15 @@ instance fileUploadQueryToQuery :: GT.ToQuery FileUploadQuery where ...@@ -208,15 +221,15 @@ instance fileUploadQueryToQuery :: GT.ToQuery FileUploadQuery where
where pair :: forall a. Show a => String -> a -> Array (Tuple QP.Key (Maybe QP.Value)) where pair :: forall a. Show a => String -> a -> Array (Tuple QP.Key (Maybe QP.Value))
pair k v = [ QP.keyFromString k /\ (Just $ QP.valueFromString $ show v) ] pair k v = [ QP.keyFromString k /\ (Just $ QP.valueFromString $ show v) ]
uploadFile :: Session -> ID -> FileType -> UploadFileContents -> Aff GT.AsyncTaskWithType uploadFile :: Session -> GT.NodeType -> ID -> FileType -> UploadFileContents -> Aff GT.AsyncTaskWithType
uploadFile session id fileType (UploadFileContents fileContents) = do uploadFile session nodeType id fileType (UploadFileContents fileContents) = do
task <- postWwwUrlencoded session p bodyParams task <- postWwwUrlencoded session p bodyParams
pure $ GT.AsyncTaskWithType {task, typ: GT.Form} pure $ GT.AsyncTaskWithType {task, typ: GT.Form}
--postMultipartFormData session p fileContents --postMultipartFormData session p fileContents
where where
q = FileUploadQuery { fileType: fileType } q = FileUploadQuery { fileType: fileType }
--p = NodeAPI GT.Corpus (Just id) $ "add/file/async/nobody" <> Q.print (toQuery q) --p = NodeAPI GT.Corpus (Just id) $ "add/file/async/nobody" <> Q.print (toQuery q)
p = GR.NodeAPI GT.Corpus (Just id) $ GT.asyncTaskTypePath GT.Form p = GR.NodeAPI nodeType (Just id) $ GT.asyncTaskTypePath GT.Form
bodyParams = [ bodyParams = [
Tuple "_wf_data" (Just fileContents) Tuple "_wf_data" (Just fileContents)
, Tuple "_wf_filetype" (Just $ show fileType) , Tuple "_wf_filetype" (Just $ show fileType)
......
...@@ -71,7 +71,7 @@ nodeMainSpan d p folderOpen session frontends = R.createElement el p [] ...@@ -71,7 +71,7 @@ nodeMainSpan d p folderOpen session frontends = R.createElement el p []
, name: name' props} ] , name: name' props} ]
, popOverIcon showBox popupOpen popupPosition , popOverIcon showBox popupOpen popupPosition
, mNodePopupView props showBox popupOpen popupPosition , mNodePopupView props showBox popupOpen popupPosition
, fileTypeView d {id, nodeType} droppedFile isDragOver , fileTypeView {action: d, droppedFile, id, isDragOver, nodeType}
, H.div {} (map (\t -> asyncProgressBar { asyncTask: t , H.div {} (map (\t -> asyncProgressBar { asyncTask: t
, corpusId: id , corpusId: id
, onFinish: \_ -> onAsyncTaskFinish t , onFinish: \_ -> onAsyncTaskFinish t
...@@ -374,7 +374,7 @@ panelAction d {id, name, nodeType, action, session, search} p = case action of ...@@ -374,7 +374,7 @@ panelAction d {id, name, nodeType, action, session, search} p = case action of
(Just (Documentation x)) -> fragmentPT $ "More information on" <> show nodeType (Just (Documentation x)) -> fragmentPT $ "More information on" <> show nodeType
(Just (Link _)) -> fragmentPT "Soon, you will be able to link the corpus with your Annuaire (and reciprocally)." (Just (Link _)) -> fragmentPT "Soon, you will be able to link the corpus with your Annuaire (and reciprocally)."
(Just Upload) -> uploadFileView d {session, id} (Just Upload) -> uploadFileView d {id, nodeType, session}
(Just Download) -> fragmentPT "Soon, you will be able to dowload your file here" (Just Download) -> fragmentPT "Soon, you will be able to dowload your file here"
(Just SearchBox) -> R.fragment [ H.p {"style": {"margin" :"10px"}} [ H.text $ "Search and create a private corpus with the search query as corpus name." ] (Just SearchBox) -> R.fragment [ H.p {"style": {"margin" :"10px"}} [ H.text $ "Search and create a private corpus with the search query as corpus name." ]
......
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