Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UX: Rewrite date/time param-input using FormKit #316

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions assets/javascripts/discourse/components/param-input-form.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const layoutMap = {
bigint: "string",
boolean: "boolean",
string: "string",
time: "generic",
date: "generic",
datetime: "generic",
time: "time",
date: "date",
datetime: "datetime",
double: "string",
user_id: "user_id",
post_id: "string",
Expand All @@ -45,6 +45,8 @@ export const ERRORS = {
INVALID: I18n.t("explorer.form.errors.invalid"),
NO_SUCH_CATEGORY: I18n.t("explorer.form.errors.no_such_category"),
NO_SUCH_GROUP: I18n.t("explorer.form.errors.no_such_group"),
INVALID_DATE: (date) => I18n.t("explorer.form.errors.invalid_date", { date }),
INVALID_TIME: (time) => I18n.t("explorer.form.errors.invalid_time", { time }),
};

function digitalizeCategoryId(value) {
Expand Down Expand Up @@ -78,6 +80,8 @@ function serializeValue(type, value) {
return value?.join(",");
case "group_id":
return value[0];
case "datetime":
return value?.replaceAll("T", " ");
default:
return value?.toString();
}
Expand Down Expand Up @@ -119,6 +123,18 @@ function componentOf(info) {
return UserListInput;
case "group_list":
return GroupInput;
case "date":
return <template>
<@field.Input @type="date" name={{@info.identifier}} />
</template>;
case "time":
return <template>
<@field.Input @type="time" name={{@info.identifier}} />
</template>;
case "datetime":
return <template>
<@field.Input @type="datetime-local" name={{@info.identifier}} />
</template>;
case "bigint":
case "string":
default:
Expand Down Expand Up @@ -213,6 +229,40 @@ export default class ParamInputForm extends Component {
return value[0];
}
return value;
case "date":
try {
if (!value) {
return null;
}
return moment(new Date(value).toISOString()).format("YYYY-MM-DD");
} catch (err) {
this.addError(info.identifier, ERRORS.INVALID_DATE(String(value)));
return null;
}
case "time":
try {
if (!value) {
return null;
}
return moment(new Date(`1970/01/01 ${value}`).toISOString()).format(
"HH:mm"
);
} catch (err) {
this.addError(info.identifier, ERRORS.INVALID_TIME(String(value)));
return null;
}
case "datetime":
try {
if (!value) {
return null;
}
return moment(new Date(value).toISOString()).format(
"YYYY-MM-DD HH:mm"
);
} catch (err) {
this.addError(info.identifier, ERRORS.INVALID_TIME(String(value)));
return null;
}
default:
return value;
}
Expand Down
2 changes: 2 additions & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ en:
invalid: "Invalid"
no_such_category: "No such category"
no_such_group: "No such group"
invalid_date: "%{date} is a invalid date"
invalid_time: "%{time} is a invalid time"
group:
reports: "Reports"
admin:
Expand Down
96 changes: 96 additions & 0 deletions test/javascripts/components/param-input-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,54 @@ const InputTestCases = [
},
],
},
{
type: "date",
default: "2024-07-13",
initial: "1970-01-01",
tests: [
{
input: null,
data_null: undefined,
error: ERRORS.REQUIRED,
},
{
input: "2038-01-20",
data: "2038-01-20",
},
],
},
{
type: "time",
default: "12:34",
initial: "11:45",
tests: [
{
input: null,
data_null: undefined,
error: ERRORS.REQUIRED,
},
{
input: "03:14",
data: "03:14",
},
],
},
{
type: "datetime",
default: "2024-07-13 12:00",
initial: "1970-01-01 00:00",
tests: [
{
input: null,
data_null: undefined,
error: ERRORS.REQUIRED,
},
{
input: "2038-01-19 03:15",
data: "2038-01-19 03:15",
},
],
},
];

module("Data Explorer Plugin | Component | param-input", function (hooks) {
Expand Down Expand Up @@ -284,4 +332,52 @@ module("Data Explorer Plugin | Component | param-input", function (hooks) {
.field("group_id")
.hasError(`${ERRORS.NO_SUCH_GROUP}: invalid_group_name`);
});

test("date, time, datetime with initial value in other formats", async function (assert) {
this.setProperties({
param_info: [
{
identifier: "date",
type: "date",
default: null,
nullable: false,
},
{
identifier: "time",
type: "time",
default: null,
nullable: false,
},
{
identifier: "datetime",
type: "datetime",
default: null,
nullable: false,
},
],
initialValues: {
date: "19 January 2038",
time: "3:15 am",
datetime: "19 January 2038 3:15 am",
},
onRegisterApi: ({ submit }) => {
this.submit = submit;
},
});

await render(hbs`
<ParamInputForm
@initialValues={{this.initialValues}}
@paramInfo={{this.param_info}}
@onRegisterApi={{this.onRegisterApi}}
/>`);

this.submit().then((res) => {
assert.deepEqual(res, {
date: "2038-01-19",
time: "03:15",
datetime: "2038-01-19 03:15",
});
});
});
});