Skip to content

Feat/additional dataflags in datepicker #3090

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

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
8 changes: 8 additions & 0 deletions src/app-components/Datepicker/utils/dateHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ describe('dateHelpers', () => {
{ props: ['2022-45-15', 'max'], expected: DatepickerMaxDateDefault },
{ props: [DateFlags.Today, 'min'], expected: '2023-07-07T00:00:00Z' },
{ props: [DateFlags.Today, 'max'], expected: '2023-07-07T23:59:59Z' },
{ props: [DateFlags.Yesterday, 'min'], expected: '2023-07-06T00:00:00Z' },
{ props: [DateFlags.Yesterday, 'max'], expected: '2023-07-06T23:59:59Z' },
{ props: [DateFlags.Tomorrow, 'min'], expected: '2023-07-08T00:00:00Z' },
{ props: [DateFlags.Tomorrow, 'max'], expected: '2023-07-08T23:59:59Z' },
{ props: [DateFlags.OneYearAgo, 'min'], expected: '2022-07-07T00:00:00Z' },
{ props: [DateFlags.OneYearAgo, 'max'], expected: '2022-07-07T23:59:59Z' },
{ props: [DateFlags.OneYearFromNow, 'min'], expected: '2024-07-07T00:00:00Z' },
{ props: [DateFlags.OneYearFromNow, 'max'], expected: '2024-07-07T23:59:59Z' },
{ props: ['2022-11-05T12:00:00.000Z', 'min'], expected: '2022-11-05T00:00:00Z' },
{ props: ['2022-11-05T12:00:00.000Z', 'max'], expected: '2022-11-05T23:59:59Z' },
{ props: ['2022-01-31', 'min'], expected: '2022-01-31T00:00:00Z' },
Expand Down
20 changes: 20 additions & 0 deletions src/app-components/Datepicker/utils/dateHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ export function getDateConstraint(dateOrFlag: string | DateFlags | undefined, co
if (dateOrFlag === DateFlags.Today) {
return shiftTime(new Date());
}
if (dateOrFlag === DateFlags.Yesterday) {
const date = new Date();
date.setDate(date.getDate() - 1);
return shiftTime(date);
}
if (dateOrFlag === DateFlags.Tomorrow) {
const date = new Date();
date.setDate(date.getDate() + 1);
return shiftTime(date);
}
if (dateOrFlag === DateFlags.OneYearAgo) {
const date = new Date();
date.setFullYear(date.getFullYear() - 1);
return shiftTime(date);
}
if (dateOrFlag === DateFlags.OneYearFromNow) {
const date = new Date();
date.setFullYear(date.getFullYear() + 1);
return shiftTime(date);
}

const date = strictParseISO(dateOrFlag);
if (date && isValid(date)) {
Expand Down
18 changes: 16 additions & 2 deletions src/layout/Datepicker/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ export const Config = new CG.component({
.addProperty(
new CG.prop(
'minDate',
new CG.union(new CG.str(), new CG.const('today'))
new CG.union(
new CG.str(),
new CG.const('today'),
new CG.const('yesterday'),
new CG.const('tomorrow'),
new CG.const('oneYearAgo'),
new CG.const('oneYearFromNow'),
)
.optional({ default: '1900-01-01T12:00:00.000Z' })
.setTitle('Earliest date')
.setDescription(
Expand All @@ -39,7 +46,14 @@ export const Config = new CG.component({
.addProperty(
new CG.prop(
'maxDate',
new CG.union(new CG.str(), new CG.const('today'))
new CG.union(
new CG.str(),
new CG.const('today'),
new CG.const('yesterday'),
new CG.const('tomorrow'),
new CG.const('oneYearAgo'),
new CG.const('oneYearFromNow'),
)
.optional({ default: '2100-01-01T12:00:00.000Z' })
.setTitle('Latest date')
.setDescription(
Expand Down
4 changes: 4 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export enum PresentationType {

export enum DateFlags {
Today = 'today',
Yesterday = 'yesterday',
Tomorrow = 'tomorrow',
OneYearAgo = 'oneYearAgo',
OneYearFromNow = 'oneYearFromNow',
}

export function isProcessTaskType(taskType: string): taskType is ProcessTaskType {
Expand Down
Loading