diff options
author | stuebinm | 2021-04-12 18:07:41 +0200 |
---|---|---|
committer | stuebinm | 2021-04-12 18:07:41 +0200 |
commit | 5596ebcbad7721f767972c3143f8e5be1ba8fc09 (patch) | |
tree | 4f792eaf042e6d8236690588b4bf59641463002c | |
parent | 6b892239f80e8523e02d7348e5483e9a7aea58ee (diff) |
add date answer space
for now, this is just a simple date picker done via the html input type
"date". Unfortunately, that's not supported by Safari, so the rust util
now prints out a warning if it's used.
I'm not sure if there's any other reasonable option which would go
around this that's not "using a web framework" or "hacking a fallback
date picker which just consists of a couple of combo boxes".
-rw-r--r-- | example-config.dhall | 1 | ||||
-rw-r--r-- | site/examples/example.json | 2 | ||||
-rw-r--r-- | site/index.html | 28 | ||||
-rw-r--r-- | site/style.css | 1 | ||||
-rw-r--r-- | utils/src/main.rs | 8 |
5 files changed, 38 insertions, 2 deletions
diff --git a/example-config.dhall b/example-config.dhall index ae12897..2dbaf30 100644 --- a/example-config.dhall +++ b/example-config.dhall @@ -17,5 +17,6 @@ , "third possible answer" ] } + , { question = "fourth question", name = "question4", space = Answers.Date } ] } diff --git a/site/examples/example.json b/site/examples/example.json index fcd09d8..140fe7f 100644 --- a/site/examples/example.json +++ b/site/examples/example.json @@ -1 +1 @@ -{"title":"Test Survey","description":"This is a test survey.","questions":[{"question":"first question","name":"question1","space":"YesOrNo"},{"question":"second question","name":"question2","space":{"Freeform":"test"}},{"question":"third question","name":"question3","space":{"Multiple":["first possible answer","second possible answer","third possible answer"]}}],"pubkey":"age1f380w6eaeuydey65mmu88h4kqv50dda96pxlgkal78sufpnngypqnetmuu","lang":"en"}
\ No newline at end of file +{"title":"Test Survey","description":"This is a test survey.","questions":[{"question":"first question","name":"question1","space":"YesOrNo"},{"question":"second question","name":"question2","space":{"Freeform":"test"}},{"question":"third question","name":"question3","space":{"Multiple":["first possible answer","second possible answer","third possible answer"]}},{"question":"fourth question","name":"question4","space":"Date"}],"pubkey":"age1f380w6eaeuydey65mmu88h4kqv50dda96pxlgkal78sufpnngypqnetmuu","lang":"en"}
\ No newline at end of file diff --git a/site/index.html b/site/index.html index 6141163..1555a21 100644 --- a/site/index.html +++ b/site/index.html @@ -125,6 +125,32 @@ ]; } + function mkDatePicker (name) { + let [input, label] = mkOption("date", name); + let errormsg = setErrormsg( + mkElement("div","","error"), + null + ); + let div = appendChildren ( + mkElement("div"), + [label, + mkElement("span", " "), + input, + errormsg] + ); + return [ + div, + () => { + let date = input.valueAsDate; + let [ret, error] = date !== null && !isNaN(date.valueOf()) + ? [date.toISOString(), null] + : [null, _("Please pick a date")] + setErrormsg (errormsg, error); + return ret; + } + ] + } + // essentially a case - of statement over the answerspace type. // since there is no validation of the config json, it may turn // out to be a partial function in practice. @@ -132,6 +158,8 @@ let space = question.space; if (space === "YesOrNo") { return mkListSpace("radio", ["yes", "no"], question.name, question.question); + } if (space === "Date") { + return mkDatePicker(question.name); } if ("Single" in space) { return mkListSpace("radio", space.Single, question.name, question.question); } if ("Multiple" in space) { diff --git a/site/style.css b/site/style.css index 08f22a6..9c4f668 100644 --- a/site/style.css +++ b/site/style.css @@ -22,6 +22,7 @@ li { .error { color: red; + margin-top: 0.5em; } textarea { diff --git a/utils/src/main.rs b/utils/src/main.rs index 4428ac3..cafb4f9 100644 --- a/utils/src/main.rs +++ b/utils/src/main.rs @@ -34,12 +34,13 @@ struct Question { space: AnswerSpace, } -#[derive(Deserialize, Serialize, StaticType, Debug)] +#[derive(Deserialize, Serialize, StaticType, Debug, PartialEq)] enum AnswerSpace { Single(Vec<String>), Multiple(Vec<String>), YesOrNo, Freeform(String), + Date, } #[derive(StructOpt, Debug)] @@ -79,6 +80,11 @@ fn main () { std::process::exit(1); }, Ok(data) => { + if data.questions.iter().any(|q| q.space == AnswerSpace::Date) { + eprintln!(">> Warning: html input type date is not supported by Safari and Internet Explorer.\n See https://caniuse.com/input-datetime for details."); + } + + let json = json::to_string(&data).unwrap(); // if a public key is given to encrypt the survey, ad-hoc typecheck it |