[action] adjust fileType/fileFormat to uploaded file extension

This is only when form is "pristine", i.e. user didn't set file
type/format/lang by hand.
parent 94083247
Pipeline #7163 passed with stages
in 17 minutes and 49 seconds
...@@ -101,8 +101,8 @@ addNodeViewCpt = here.component "addNodeView" cpt ...@@ -101,8 +101,8 @@ addNodeViewCpt = here.component "addNodeView" cpt
if length nodeTypes > 1 then if length nodeTypes > 1 then
[ Tools.formChoice [ Tools.formChoice
{ items: nodeTypes { items: nodeTypes
, default: nodeType' , box: nodeType
, callback: setNodeType' , callback: \nt -> T.write_ (GT.prettyNodeType nt) nodeName
, print: print hasChromeAgent' , print: print hasChromeAgent'
} }
[] []
......
...@@ -75,8 +75,8 @@ updateDashboardCpt = here.component "updateDashboard" cpt ...@@ -75,8 +75,8 @@ updateDashboardCpt = here.component "updateDashboard" cpt
[ -- H.text "Update with" [ -- H.text "Update with"
Tools.formChoiceSafe Tools.formChoiceSafe
{ items: [ All, Sources, Authors, Institutes, Ngrams ] { items: [ All, Sources, Authors, Institutes, Ngrams ]
, default: methodBoard' , box: methodBoard
, callback: \val -> T.write_ val methodBoard , callback: const $ pure unit
, print: show , print: show
} }
[] []
...@@ -131,8 +131,8 @@ updateGraphCpt = here.component "updateGraph" cpt ...@@ -131,8 +131,8 @@ updateGraphCpt = here.component "updateGraph" cpt
[ H.text "Show subjects with Order1 or concepts with Order2 ?" [ H.text "Show subjects with Order1 or concepts with Order2 ?"
, Tools.formChoiceSafe , Tools.formChoiceSafe
{ items: [ Order1, Order2 ] { items: [ Order1, Order2 ]
, default: methodGraphMetric' , box: methodGraphMetric
, callback: \val -> T.write_ val methodGraphMetric , callback: const $ pure unit
, print: show , print: show
} }
[] []
...@@ -288,8 +288,8 @@ updateCorpusCpt = here.component "updateTexts" cpt ...@@ -288,8 +288,8 @@ updateCorpusCpt = here.component "updateTexts" cpt
[ H.text "Term update mode" [ H.text "Term update mode"
, Tools.formChoiceSafe , Tools.formChoiceSafe
{ items: [ Basic, Advanced, WithModel ] { items: [ Basic, Advanced, WithModel ]
, default: methodList' , box: methodList
, callback: \val -> T.write_ val methodList , callback: const $ pure unit
, print: show , print: show
} }
[] []
...@@ -316,8 +316,8 @@ updateNodeListCpt = here.component "updateNodeList" cpt ...@@ -316,8 +316,8 @@ updateNodeListCpt = here.component "updateNodeList" cpt
[ -- H.text "Update with" [ -- H.text "Update with"
Tools.formChoiceSafe Tools.formChoiceSafe
{ items: [ Basic, Advanced, WithModel ] { items: [ Basic, Advanced, WithModel ]
, default: methodList' , box: methodList
, callback: \val -> T.write_ val methodList , callback: const $ pure unit
, print: show , print: show
} }
[] []
......
...@@ -7,8 +7,10 @@ import Data.Foldable (intercalate) ...@@ -7,8 +7,10 @@ import Data.Foldable (intercalate)
import Data.Generic.Rep (class Generic) import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..), fromJust, fromMaybe, isNothing) import Data.Maybe (Maybe(..), fromJust, fromMaybe, isNothing)
import Data.Newtype (class Newtype) import Data.Newtype (class Newtype)
import Data.String (toLower)
import Data.String.Regex as DSR import Data.String.Regex as DSR
import Data.String.Regex.Flags as DSRF import Data.String.Regex.Flags as DSRF
import Data.String.Utils (endsWith)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Effect (Effect) import Effect (Effect)
...@@ -142,10 +144,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -142,10 +144,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
fileFormat <- T.useBox Plain fileFormat <- T.useBox Plain
lang <- T.useBox EN lang <- T.useBox EN
selection <- T.useBox ListSelection.MyListsFirst selection <- T.useBox ListSelection.MyListsFirst
-- whether user introduced his own changes or not
let setFileType' val = T.write_ val fileType isPristine <- T.useBox true
let setFileFormat' val = T.write_ val fileFormat
let setLang' val = T.write_ val lang
let let
bodies = bodies =
...@@ -156,7 +156,7 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -156,7 +156,7 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
{ type: "file" { type: "file"
, className: "form-control" , className: "form-control"
, placeholder: "Choose file" , placeholder: "Choose file"
, on: { change: onChangeContents mFile } , on: { change: onChangeContents mFile fileType fileFormat isPristine }
} }
] ]
] ]
...@@ -172,8 +172,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -172,8 +172,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
, JSON , JSON
-- , Iramuteq -- , Iramuteq
] ]
, default: TSV , box: fileType
, callback: setFileType' , callback: const $ T.write_ false isPristine
, print: show , print: show
} }
[] []
...@@ -182,8 +182,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -182,8 +182,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
[ Plain [ Plain
, ZIP , ZIP
] ]
, default: Plain , box: fileFormat
, callback: setFileFormat' , callback: const $ T.write_ false isPristine
, print: show , print: show
} }
[] []
...@@ -193,8 +193,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -193,8 +193,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
[ H.div { className: "col-6 flex-space-around" } [ H.div { className: "col-6 flex-space-around" }
[ Tools.formChoiceSafe [ Tools.formChoiceSafe
{ items: langs <> [ No_extraction ] { items: langs <> [ No_extraction ]
, default: EN , box: lang
, callback: setLang' , callback: const $ T.write_ false isPristine
, print: show , print: show
} }
[] []
...@@ -221,11 +221,19 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt ...@@ -221,11 +221,19 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
] ]
pure $ Tools.panel { mError: Nothing } (bodies <> [ footer ]) pure $ Tools.panel { mError: Nothing } (bodies <> [ footer ])
onChangeContents :: forall e. T.Box (Maybe UploadFile) -> E.SyntheticEvent_ e -> Effect Unit onChangeContents
onChangeContents mFile e = do :: forall e
. T.Box (Maybe UploadFile)
-> T.Box FileType
-> T.Box FileFormat
-> T.Box Boolean
-> E.SyntheticEvent_ e
-> Effect Unit
onChangeContents mFile fileType fileFormat isPristine e = do
let mF = R2.inputFileNameWithBlob 0 e let mF = R2.inputFileNameWithBlob 0 e
E.preventDefault e E.preventDefault e
E.stopPropagation e E.stopPropagation e
isPristine' <- T.read isPristine
case mF of case mF of
Nothing -> pure unit Nothing -> pure unit
Just { blob, name } -> void $ launchAff do Just { blob, name } -> void $ launchAff do
...@@ -233,6 +241,17 @@ onChangeContents mFile e = do ...@@ -233,6 +241,17 @@ onChangeContents mFile e = do
--contents <- readAsDataURL blob --contents <- readAsDataURL blob
liftEffect $ do liftEffect $ do
T.write_ (Just $ { blob: UploadFileBlob blob, name }) mFile T.write_ (Just $ { blob: UploadFileBlob blob, name }) mFile
-- try to automatically adjust fileType to it's extension, but only for pristine state
let nameLower = toLower name
when isPristine' $ do
when (endsWith ".json" nameLower) $ do
T.write_ JSON fileType
T.write_ Plain fileFormat
when (endsWith ".tsv" nameLower) $ do
T.write_ TSV fileType
T.write_ Plain fileFormat
when (endsWith ".zip" nameLower) $ do
T.write_ ZIP fileFormat
type UploadButtonProps = type UploadButtonProps =
( dispatch :: Action -> Aff Unit ( dispatch :: Action -> Aff Unit
......
...@@ -252,7 +252,8 @@ inviteInputBoxCpt = here.component "textInputBox" cpt ...@@ -252,7 +252,8 @@ inviteInputBoxCpt = here.component "textInputBox" cpt
type FormChoiceSafeProps item m = type FormChoiceSafeProps item m =
( items :: Array item ( items :: Array item
, default :: item -- , default :: item
, box :: T.Box item
, callback :: item -> Effect m , callback :: item -> Effect m
, print :: item -> String , print :: item -> String
) )
...@@ -262,21 +263,27 @@ type FormChoiceSafeProps item m = ...@@ -262,21 +263,27 @@ type FormChoiceSafeProps item m =
formChoiceSafe formChoiceSafe
:: forall item m :: forall item m
. Show item . Show item
=> Eq item
=> R2.Component (FormChoiceSafeProps item m) => R2.Component (FormChoiceSafeProps item m)
formChoiceSafe = R.createElement formChoiceSafeCpt formChoiceSafe = R.createElement formChoiceSafeCpt
formChoiceSafeCpt :: forall item m. Show item => R.Component (FormChoiceSafeProps item m) formChoiceSafeCpt
:: forall item m
. Show item
=> Eq item
=> R.Component (FormChoiceSafeProps item m)
formChoiceSafeCpt = here.component "formChoiceSafe" cpt formChoiceSafeCpt = here.component "formChoiceSafe" cpt
where where
cpt { items, default, callback, print } _ = do cpt { items, box, callback, print } _ = do
pure $ case items of pure $ case items of
[] -> H.div {} [] [] -> H.div {} []
[ n ] -> formButton { item: n, callback, print } [] [ n ] -> formButton { item: n, callback, print } []
_ -> formChoice { items, default, callback, print } [] _ -> formChoice { items, box, callback, print } []
type FormChoiceProps item m = type FormChoiceProps item m =
( items :: Array item ( items :: Array item
, default :: item -- , default :: item
, box :: T.Box item
, callback :: item -> Effect m , callback :: item -> Effect m
, print :: item -> String , print :: item -> String
) )
...@@ -285,23 +292,36 @@ type FormChoiceProps item m = ...@@ -285,23 +292,36 @@ type FormChoiceProps item m =
formChoice formChoice
:: forall item m :: forall item m
. Show item . Show item
=> Eq item
=> R2.Component (FormChoiceProps item m) => R2.Component (FormChoiceProps item m)
formChoice = R.createElement formChoiceCpt formChoice = R.createElement formChoiceCpt
formChoiceCpt :: forall item m. Show item => R.Component (FormChoiceProps item m) formChoiceCpt
:: forall item m
. Show item
=> Eq item
=> R.Component (FormChoiceProps item m)
formChoiceCpt = here.component "formChoice" cpt formChoiceCpt = here.component "formChoice" cpt
where where
cpt { items, callback, default, print } _ = do cpt { items, callback, box, print } _ = do
value' <- T.useLive T.unequal box
pure $ H.div { className: "form-group" } pure $ H.div { className: "form-group" }
[ R2.select [ R2.select
{ className: "form-control with-icon-font" { className: "form-control with-icon-font"
, defaultValue: show default , value: show value'
, on: { change } , on: { change }
} $ map option items } $ map option items
] ]
where where
change e = callback $ fromMaybe default $ reader $ R.unsafeEventValue e change e = do
case reader (R.unsafeEventValue e) of
Nothing -> here.log2 "[formChoice] error: no value for" e
Just val -> do
T.write_ val box
void $ callback val
-- callback $ fromMaybe default $ reader $ R.unsafeEventValue e
option opt = H.option { value: show opt } [ H.text $ print opt ] option opt = H.option { value: show opt } [ H.text $ print opt ]
......
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