[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
if length nodeTypes > 1 then
[ Tools.formChoice
{ items: nodeTypes
, default: nodeType'
, callback: setNodeType'
, box: nodeType
, callback: \nt -> T.write_ (GT.prettyNodeType nt) nodeName
, print: print hasChromeAgent'
}
[]
......
......@@ -75,8 +75,8 @@ updateDashboardCpt = here.component "updateDashboard" cpt
[ -- H.text "Update with"
Tools.formChoiceSafe
{ items: [ All, Sources, Authors, Institutes, Ngrams ]
, default: methodBoard'
, callback: \val -> T.write_ val methodBoard
, box: methodBoard
, callback: const $ pure unit
, print: show
}
[]
......@@ -131,8 +131,8 @@ updateGraphCpt = here.component "updateGraph" cpt
[ H.text "Show subjects with Order1 or concepts with Order2 ?"
, Tools.formChoiceSafe
{ items: [ Order1, Order2 ]
, default: methodGraphMetric'
, callback: \val -> T.write_ val methodGraphMetric
, box: methodGraphMetric
, callback: const $ pure unit
, print: show
}
[]
......@@ -288,8 +288,8 @@ updateCorpusCpt = here.component "updateTexts" cpt
[ H.text "Term update mode"
, Tools.formChoiceSafe
{ items: [ Basic, Advanced, WithModel ]
, default: methodList'
, callback: \val -> T.write_ val methodList
, box: methodList
, callback: const $ pure unit
, print: show
}
[]
......@@ -316,8 +316,8 @@ updateNodeListCpt = here.component "updateNodeList" cpt
[ -- H.text "Update with"
Tools.formChoiceSafe
{ items: [ Basic, Advanced, WithModel ]
, default: methodList'
, callback: \val -> T.write_ val methodList
, box: methodList
, callback: const $ pure unit
, print: show
}
[]
......
......@@ -7,8 +7,10 @@ import Data.Foldable (intercalate)
import Data.Generic.Rep (class Generic)
import Data.Maybe (Maybe(..), fromJust, fromMaybe, isNothing)
import Data.Newtype (class Newtype)
import Data.String (toLower)
import Data.String.Regex as DSR
import Data.String.Regex.Flags as DSRF
import Data.String.Utils (endsWith)
import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\))
import Effect (Effect)
......@@ -142,10 +144,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
fileFormat <- T.useBox Plain
lang <- T.useBox EN
selection <- T.useBox ListSelection.MyListsFirst
let setFileType' val = T.write_ val fileType
let setFileFormat' val = T.write_ val fileFormat
let setLang' val = T.write_ val lang
-- whether user introduced his own changes or not
isPristine <- T.useBox true
let
bodies =
......@@ -156,7 +156,7 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
{ type: "file"
, className: "form-control"
, placeholder: "Choose file"
, on: { change: onChangeContents mFile }
, on: { change: onChangeContents mFile fileType fileFormat isPristine }
}
]
]
......@@ -172,8 +172,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
, JSON
-- , Iramuteq
]
, default: TSV
, callback: setFileType'
, box: fileType
, callback: const $ T.write_ false isPristine
, print: show
}
[]
......@@ -182,8 +182,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
[ Plain
, ZIP
]
, default: Plain
, callback: setFileFormat'
, box: fileFormat
, callback: const $ T.write_ false isPristine
, print: show
}
[]
......@@ -193,8 +193,8 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
[ H.div { className: "col-6 flex-space-around" }
[ Tools.formChoiceSafe
{ items: langs <> [ No_extraction ]
, default: EN
, callback: setLang'
, box: lang
, callback: const $ T.write_ false isPristine
, print: show
}
[]
......@@ -221,11 +221,19 @@ uploadFileViewWithLangsCpt = here.component "uploadFileViewWithLangs" cpt
]
pure $ Tools.panel { mError: Nothing } (bodies <> [ footer ])
onChangeContents :: forall e. T.Box (Maybe UploadFile) -> E.SyntheticEvent_ e -> Effect Unit
onChangeContents mFile e = do
onChangeContents
:: 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
E.preventDefault e
E.stopPropagation e
isPristine' <- T.read isPristine
case mF of
Nothing -> pure unit
Just { blob, name } -> void $ launchAff do
......@@ -233,6 +241,17 @@ onChangeContents mFile e = do
--contents <- readAsDataURL blob
liftEffect $ do
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 =
( dispatch :: Action -> Aff Unit
......
......@@ -252,7 +252,8 @@ inviteInputBoxCpt = here.component "textInputBox" cpt
type FormChoiceSafeProps item m =
( items :: Array item
, default :: item
-- , default :: item
, box :: T.Box item
, callback :: item -> Effect m
, print :: item -> String
)
......@@ -262,21 +263,27 @@ type FormChoiceSafeProps item m =
formChoiceSafe
:: forall item m
. Show item
=> Eq item
=> R2.Component (FormChoiceSafeProps item m)
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
where
cpt { items, default, callback, print } _ = do
cpt { items, box, callback, print } _ = do
pure $ case items of
[] -> H.div {} []
[ n ] -> formButton { item: n, callback, print } []
_ -> formChoice { items, default, callback, print } []
_ -> formChoice { items, box, callback, print } []
type FormChoiceProps item m =
( items :: Array item
, default :: item
-- , default :: item
, box :: T.Box item
, callback :: item -> Effect m
, print :: item -> String
)
......@@ -285,23 +292,36 @@ type FormChoiceProps item m =
formChoice
:: forall item m
. Show item
=> Eq item
=> R2.Component (FormChoiceProps item m)
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
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" }
[ R2.select
{ className: "form-control with-icon-font"
, defaultValue: show default
, value: show value'
, on: { change }
} $ map option items
]
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 ]
......
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