Skip to content

Commit

Permalink
feat: define the date range of date questions
Browse files Browse the repository at this point in the history
  • Loading branch information
MitanOmar committed Sep 27, 2023
1 parent bd9bb3f commit 6d51645
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/-ember-caluma/mirage/scenarios/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export default function (server) {
slug: "date",
label: "When?",
formIds: [form.id],
minDate: "2023-09-01",
maxDate: "2023-10-30",
type: "DATE",
hintText: null,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ fragment InquiryRequestDeadline on Document {
id
... on DateAnswer {
value
minDate
maxDate
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,41 @@
</div>
{{/if}}

{{#if (has-question-type f.model "date")}}
<div uk-grid class="uk-grid-small uk-child-width-1-2 uk-margin">
<div class="">
<label for="" class="uk-form-label">
{{t "caluma.form-builder.question.min-date"}}
</label>
<EmberFlatpickr
@altFormat={{this.dateFormat}}
class="uk-input form-control input"
@date={{changeset-get f.model "minDate"}}
@onChange={{fn this.onDateMinMaxChange "minDate"}}
@maxDate={{changeset-get f.model "maxDate"}}
@altInput={{true}}
@allowInput={{true}}
@locale={{this.locale}}
/>
</div>
<div class="">
<label for="" class="uk-form-label">
{{t "caluma.form-builder.question.max-date"}}
</label>
<EmberFlatpickr
@altFormat={{this.dateFormat}}
class="uk-input form-control input"
@date={{changeset-get f.model "maxDate"}}
@onChange={{fn this.onDateMinMaxChange "maxDate"}}
@minDate={{changeset-get f.model "minDate"}}
@altInput={{true}}
@allowInput={{true}}
@locale={{this.locale}}
/>
</div>
</div>
{{/if}}

{{#if (has-question-type f.model "choice" "multiple-choice")}}
<f.input
@name="options"
Expand Down
34 changes: 34 additions & 0 deletions packages/form-builder/addon/components/cfb-form-editor/question.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getOwner } from "@ember/application";
import { A } from "@ember/array";
import { action } from "@ember/object";
import { inject as service } from "@ember/service";
Expand All @@ -8,6 +9,7 @@ import { queryManager } from "ember-apollo-client";
import Changeset from "ember-changeset";
import lookupValidator from "ember-changeset-validations";
import { dropTask, restartableTask, task } from "ember-concurrency";
import { DateTime } from "luxon";

import { hasQuestionType } from "@projectcaluma/ember-core/helpers/has-question-type";
import slugify from "@projectcaluma/ember-core/utils/slugify";
Expand Down Expand Up @@ -162,6 +164,36 @@ export default class CfbFormEditorQuestion extends Component {
});
}

get locale() {
return this.intl.primaryLocale.split("-")[0];
}

get config() {
return getOwner(this).resolveRegistration("config:environment");
}

get dateFormat() {
const {
FLATPICKR_DATE_FORMAT = {
de: "d.m.Y",
fr: "d.m.Y",
en: "m/d/Y",
},
FLATPICKR_DATE_FORMAT_DEFAULT = "m/d/Y",
} = this.config["ember-caluma"] || {};

return FLATPICKR_DATE_FORMAT[this.locale] ?? FLATPICKR_DATE_FORMAT_DEFAULT;
}

@action
onDateMinMaxChange(key, [date]) {
// Change Javascript date to ISO string if not null.
this.changeset.set(
key,
date ? DateTime.fromJSDate(date).toISODate() : null,
);
}

get possibleTypes() {
return Object.keys(TYPES).map((value) => ({
value,
Expand Down Expand Up @@ -281,6 +313,8 @@ export default class CfbFormEditorQuestion extends Component {
_getDateQuestionInput(changeset) {
return {
hintText: changeset.get("hintText"),
maxDate: changeset.get("maxDate"),
minDate: changeset.get("minDate"),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ mutation SaveDateQuestion($input: SaveDateQuestionInput!) {
...QuestionInfo
... on DateQuestion {
hintText
dateMinValue: minDate
dateMaxValue: maxDate
}
}
clientMutationId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ query FormEditorQuestion($slug: String!) {
}
... on DateQuestion {
hintText
minDate
maxDate
defaultAnswer {
id
dateValue: value
Expand Down
2 changes: 2 additions & 0 deletions packages/form-builder/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ caluma:

min-value: "Minimaler Wert"
max-value: "Maximaler Wert"
min-date: "Mindestdatum"
max-date: "Maximales Datum"
min-length: "Minimale Länge"
max-length: "Maximale Länge"
rowForm: "Formular für Tabelleneinträge"
Expand Down
2 changes: 2 additions & 0 deletions packages/form-builder/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ caluma:

min-value: "Minimum value"
max-value: "Maximum value"
min-date: "Minimum date"
max-date: "Maximum date"
min-length: "Minimum length"
max-length: "Maximum length"
rowForm: "Form to use for rows"
Expand Down
2 changes: 2 additions & 0 deletions packages/form-builder/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ caluma:

min-value: "Valeur minimale"
max-value: "Valeur maximale"
min-date: "Date minimale"
max-date: "Date maximale"
min-length: "Longueur minimale"
max-length: "Longueur maximale"
rowForm: "Formulaire pour les entrées de tableau"
Expand Down
2 changes: 2 additions & 0 deletions packages/form/addon/components/cf-field/input/date.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@altFormat={{this.dateFormat}}
@altInput={{true}}
@allowInput={{true}}
@maxDate={{@field.question.raw.maxDate}}
@minDate={{@field.question.raw.minDate}}
@onChange={{this.onChange}}
@onReady={{this.onReady}}
@onClose={{this.onClose}}
Expand Down
4 changes: 4 additions & 0 deletions packages/form/addon/gql/fragments/field.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ fragment SimpleQuestion on Question {
value
}
hintText
minDate
maxDate
}
... on StaticQuestion {
staticContent
Expand Down Expand Up @@ -164,6 +166,8 @@ fragment FieldTableQuestion on Question {
}
... on DateAnswer {
dateValue: value
minDate
maxDate
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/testing/addon/mirage-graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,8 @@ type DateAnswer implements Answer & Node {
id: ID!
question: Question!
value: Date
minDate: Date
maxDate: Date
meta: GenericScalar!
date: Date
}
Expand All @@ -987,6 +989,8 @@ type DateQuestion implements Question & Node {
createdByGroup: String
modifiedByUser: String
modifiedByGroup: String
minDate: Date
maxDate: Date
slug: String!
label: String!

Expand Down Expand Up @@ -2831,6 +2835,8 @@ input SaveDateQuestionInput {
isArchived: Boolean
hintText: String
clientMutationId: String
minDate: Date
maxDate: Date
}

type SaveDateQuestionPayload {
Expand Down

0 comments on commit 6d51645

Please sign in to comment.