Commit 494295c0 authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[upload] upload JSON type works now

parent 2a668be5
...@@ -237,8 +237,8 @@ addNode' name nodeType p@{ forestOpen, tree: (NTree (LNode { id }) _) } = do ...@@ -237,8 +237,8 @@ addNode' name nodeType p@{ forestOpen, tree: (NTree (LNode { id }) _) } = do
liftEffect $ T.modify_ (openNodesInsert (mkNodeId p.session id)) forestOpen liftEffect $ T.modify_ (openNodesInsert (mkNodeId p.session id)) forestOpen
refreshTree p refreshTree p
uploadFile' nodeType fileType mName blob p@{ tasks, tree: (NTree (LNode { id }) _) } = do uploadFile' nodeType fileType mName contents p@{ tasks, tree: (NTree (LNode { id }) _) } = do
task <- uploadFile p.session nodeType id fileType {mName, blob} task <- uploadFile p.session nodeType id fileType {mName, contents}
liftEffect $ do liftEffect $ do
GAT.insert id task tasks GAT.insert id task tasks
log2 "[performAction] UploadFile, uploaded, task:" task log2 "[performAction] UploadFile, uploaded, task:" task
......
...@@ -20,16 +20,16 @@ import Reactix.DOM.HTML as H ...@@ -20,16 +20,16 @@ import Reactix.DOM.HTML as H
import Toestand as T import Toestand as T
import URI.Extra.QueryPairs as QP import URI.Extra.QueryPairs as QP
-- import Web.File.Blob (Blob) -- import Web.File.Blob (Blob)
import Web.File.FileReader.Aff (readAsDataURL, readAsText) import Web.File.FileReader.Aff (readAsDataURL)
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Components.Forest.Tree.Node.Action (Action(..), Props) import Gargantext.Components.Forest.Tree.Node.Action (Action(..), Props)
import Gargantext.Components.Forest.Tree.Node.Action.Upload.Types (FileType(..), UploadFileBlob(..)) import Gargantext.Components.Forest.Tree.Node.Action.Upload.Types (FileType(..), UploadFileBlob(..), readUFBAsText)
import Gargantext.Components.Forest.Tree.Node.Tools (fragmentPT, formChoiceSafe, panel) import Gargantext.Components.Forest.Tree.Node.Tools (fragmentPT, formChoiceSafe, panel)
import Gargantext.Components.Lang (Lang(..)) import Gargantext.Components.Lang (Lang(..))
import Gargantext.Routes as GR import Gargantext.Routes as GR
import Gargantext.Sessions (Session, postWwwUrlencoded) import Gargantext.Sessions (Session, postWwwUrlencoded, post)
import Gargantext.Types (NodeType(..), ID) import Gargantext.Types (NodeType(..), ID)
import Gargantext.Types as GT import Gargantext.Types as GT
import Gargantext.Utils.Reactix as R2 import Gargantext.Utils.Reactix as R2
...@@ -79,7 +79,6 @@ instance Eq DroppedFile where ...@@ -79,7 +79,6 @@ instance Eq DroppedFile where
type FileHash = String type FileHash = String
type UploadFile = type UploadFile =
{ blob :: UploadFileBlob { blob :: UploadFileBlob
, name :: String , name :: String
...@@ -203,7 +202,7 @@ uploadButtonCpt = here.component "uploadButton" cpt ...@@ -203,7 +202,7 @@ uploadButtonCpt = here.component "uploadButton" cpt
Arbitrary -> Arbitrary ->
dispatch $ UploadArbitraryFile (Just name) blob dispatch $ UploadArbitraryFile (Just name) blob
_ -> do _ -> do
contents <- readAsText blob contents <- readUFBAsText blob
dispatch $ UploadFile nodeType fileType' (Just name) contents dispatch $ UploadFile nodeType fileType' (Just name) contents
liftEffect $ do liftEffect $ do
T.write_ Nothing mFile T.write_ Nothing mFile
...@@ -289,7 +288,9 @@ fileTypeViewCpt = here.component "fileTypeView" cpt ...@@ -289,7 +288,9 @@ fileTypeViewCpt = here.component "fileTypeView" cpt
, type: "button" , type: "button"
, on: {click: \_ -> do , on: {click: \_ -> do
T.write_ Nothing droppedFile T.write_ Nothing droppedFile
launchAff $ dispatch $ UploadFile nodeType ft Nothing blob launchAff $ do
contents <- readUFBAsText blob
dispatch $ UploadFile nodeType ft Nothing contents
} }
} [H.text "Upload"] } [H.text "Upload"]
Nothing -> Nothing ->
...@@ -316,22 +317,26 @@ uploadFile :: Session ...@@ -316,22 +317,26 @@ uploadFile :: Session
-> FileType -> FileType
-> {contents :: String, mName :: Maybe String} -> {contents :: String, mName :: Maybe String}
-> Aff GT.AsyncTaskWithType -> Aff GT.AsyncTaskWithType
uploadFile session NodeList id fileType { mName, contents } = do
let url = GR.NodeAPI NodeList (Just id) $ GT.asyncTaskTypePath GT.ListUpload
-- { input: { data: ..., filetype: "JSON", name: "..." } }
let body = { input: { data: contents
, filetype: "JSON"
, name: fromMaybe "" mName } }
task <- post session url body
pure $ GT.AsyncTaskWithType { task, typ: GT.Form }
uploadFile session nodeType id fileType { mName, contents } = do uploadFile session nodeType id fileType { mName, contents } = do
-- contents <- readAsText blob -- contents <- readAsText blob
task <- postWwwUrlencoded session p (bodyParams contents) 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
p = case nodeType of p = case nodeType of
Corpus -> GR.NodeAPI nodeType (Just id) $ GT.asyncTaskTypePath GT.Form Corpus -> GR.NodeAPI nodeType (Just id) $ GT.asyncTaskTypePath GT.Form
Annuaire -> GR.NodeAPI nodeType (Just id) "annuaire" Annuaire -> GR.NodeAPI nodeType (Just id) "annuaire"
NodeList -> GR.NodeAPI nodeType (Just id) $ GT.asyncTaskTypePath GT.ListUpload
_ -> GR.NodeAPI nodeType (Just id) "" _ -> GR.NodeAPI nodeType (Just id) ""
-- { input: { data: ..., filetype: "JSON", name: "..." } } bodyParams = [ Tuple "_wf_data" (Just contents)
bodyParams c = case nodeType of
NodeList -> []
_ -> [ Tuple "_wf_data" (Just c)
, Tuple "_wf_filetype" (Just $ show fileType) , Tuple "_wf_filetype" (Just $ show fileType)
, Tuple "_wf_name" mName , Tuple "_wf_name" mName
] ]
...@@ -454,7 +459,7 @@ uploadTermButtonCpt = here.component "uploadTermButton" cpt ...@@ -454,7 +459,7 @@ uploadTermButtonCpt = here.component "uploadTermButton" cpt
onClick mFile' uploadType' e = do onClick mFile' uploadType' e = do
let {name, blob} = unsafePartial $ fromJust mFile' let {name, blob} = unsafePartial $ fromJust mFile'
void $ launchAff do void $ launchAff do
contents <- readAsText blob contents <- readUFBAsText blob
_ <- dispatch $ UploadFile nodeType uploadType' (Just name) contents _ <- dispatch $ UploadFile nodeType uploadType' (Just name) contents
liftEffect $ do liftEffect $ do
T.write_ Nothing mFile T.write_ Nothing mFile
...@@ -4,7 +4,9 @@ import Data.Generic.Rep (class Generic) ...@@ -4,7 +4,9 @@ import Data.Generic.Rep (class Generic)
import Data.Eq.Generic (genericEq) import Data.Eq.Generic (genericEq)
import Data.Show.Generic (genericShow) import Data.Show.Generic (genericShow)
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
import Effect.Aff (Aff)
import Web.File.Blob (Blob, size) import Web.File.Blob (Blob, size)
import Web.File.FileReader.Aff (readAsText)
import Gargantext.Prelude import Gargantext.Prelude
...@@ -31,3 +33,6 @@ newtype UploadFileBlob = UploadFileBlob Blob ...@@ -31,3 +33,6 @@ newtype UploadFileBlob = UploadFileBlob Blob
derive instance Generic UploadFileBlob _ derive instance Generic UploadFileBlob _
instance Eq UploadFileBlob where instance Eq UploadFileBlob where
eq (UploadFileBlob b1) (UploadFileBlob b2) = eq (size b1) (size b2) eq (UploadFileBlob b1) (UploadFileBlob b2) = eq (size b1) (size b2)
readUFBAsText :: UploadFileBlob -> Aff String
readUFBAsText (UploadFileBlob b) = readAsText b
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