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

feat(kt-filters): add date range shortcuts support #828

Merged
merged 1 commit into from
Nov 8, 2023
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
121 changes: 100 additions & 21 deletions packages/documentation/pages/usage/components/filters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@
label="maximum"
/>
</div>
<div class="field-row">
<KtFieldDate
formKey="dateRangeMaximumDate"
helpText="Support on DATE_RANGE column type only"
isOptional
label="maximumDate"
/>
<KtFieldDate
formKey="dateRangeMinimumDate"
helpText="Support on DATE_RANGE column type only"
isOptional
label="minimumDate"
/>
</div>
<KtFieldToggleGroup
formKey="NONE"
helpText="Support on DATE_RANGE column type only"
isOptional
label="shortcuts"
:options="shortcutsOptions"
:value="selectedShortcuts"
@input="onSelectedShortcutsChange"
/>
<KtFieldSingleSelect
formKey="currencyCurrency"
helpText='Available Currencies can be defined via <KtI18nContext :currencyMap="..."/>'
Expand Down Expand Up @@ -120,12 +143,40 @@
<script lang="ts">
import { Kotti, KtFilters } from '@3yourmind/kotti-ui'
import { computed, defineComponent, ref } from '@vue/composition-api'
import dayjs from 'dayjs'
import cloneDeep from 'lodash/cloneDeep'

import { ISO8601 } from '../../../../kotti-ui/source/constants'
import { ComponentValue, generateComponentCode } from '../../utilities'

import ComponentInfo from '~/components/ComponentInfo.vue'

const today = (): string => dayjs().format(ISO8601)
const getLast = (unit: 'day' | 'month' | 'week' | 'year') =>
dayjs().subtract(1, unit).format(ISO8601)

const shortcuts: Record<
string,
NonNullable<Kotti.FieldDateRange.Props['shortcuts']>[0]
> = {
today: {
label: 'Today',
value: [today(), today()],
},
lastWeek: {
label: 'Last Week',
value: [getLast('week'), today()],
},
lastMonth: {
label: 'Last Month',
value: [getLast('month'), today()],
},
lastYear: {
label: 'Last Year',
value: [getLast('year'), today()],
},
}

export default defineComponent({
name: 'DocumentationPageUsageComponentsFilters',
components: {
Expand All @@ -150,6 +201,9 @@ export default defineComponent({
isLoading: boolean
}
currencyCurrency: string
dateRangeMaximumDate: Kotti.FieldDateRange.Props['maximumDate']
dateRangeMinimumDate: Kotti.FieldDateRange.Props['minimumDate']
dateRangeShortcuts: Kotti.FieldDateRange.Props['shortcuts']
locale: Kotti.I18n.SupportedLanguages
numberDecimalPlaces: Kotti.FieldNumber.Props['decimalPlaces'] | null
numberMaximum: Kotti.FieldNumber.Props['maximum'] | null
Expand All @@ -163,6 +217,9 @@ export default defineComponent({
isLoading: false,
},
currencyCurrency: 'USD',
dateRangeMaximumDate: null,
dateRangeMinimumDate: null,
dateRangeShortcuts: [],
locale: 'en-US',
numberDecimalPlaces: null,
numberMaximum: null,
Expand All @@ -173,6 +230,12 @@ export default defineComponent({
searchPlaceholder: null,
})

const selectedShortcuts = ref<Kotti.FieldToggleGroup.Value>(
Object.fromEntries(
Object.entries(shortcuts).map(([key]) => [key, false]),
),
)

const componentProps = computed(
(): Omit<Kotti.Filters.Props, 'value'> => ({
columns: [
Expand Down Expand Up @@ -259,6 +322,9 @@ export default defineComponent({
Kotti.Filters.Operation.DateRange.IS_EMPTY,
],
type: Kotti.Filters.FilterType.DATE_RANGE,
maximumDate: settings.value.dateRangeMaximumDate,
minimumDate: settings.value.dateRangeMinimumDate,
shortcuts: settings.value.dateRangeShortcuts,
},
{
key: 'singleEnumColumn',
Expand Down Expand Up @@ -293,33 +359,46 @@ export default defineComponent({
}),
)

const componentCode = computed<string>(() => {
const component: ComponentValue = {
contentSlot: null,
defaultSlot: null,
hasActions: false,
hasHelpTextSlot: false,
hasOptionSlot: false,
hasRemoteUpload: false,
headerSlot: null,
name: 'KtFilters',
props: cloneDeep(componentProps.value),
showHeaderSideSlot: false,
validation: 'empty',
}
return generateComponentCode(component)
})

const reset = () => (filters.value = [])

return {
component: KtFilters,
componentCode,
componentCode: computed<string>(() => {
const component: ComponentValue = {
contentSlot: null,
defaultSlot: null,
hasActions: false,
hasHelpTextSlot: false,
hasOptionSlot: false,
hasRemoteUpload: false,
headerSlot: null,
name: 'KtFilters',
props: cloneDeep(componentProps.value),
showHeaderSideSlot: false,
validation: 'empty',
}
return generateComponentCode(component)
}),
componentProps,
filters,
Kotti,
reset,
onSelectedShortcutsChange: (value: Kotti.FieldToggleGroup.Value) => {
selectedShortcuts.value = value
settings.value = {
...settings.value,
dateRangeShortcuts: Object.entries(selectedShortcuts.value ?? {})
.filter(([_, value]) => value)
.map(([key]) => {
const { label, value } = shortcuts[key as keyof typeof shortcuts]
return { label, value }
}),
}
},
reset: () => (filters.value = []),
selectedShortcuts,
settings,
shortcutsOptions: Object.entries(shortcuts).map(([key, value]) => ({
key,
label: value.label,
})),
}
},
})
Expand Down
1 change: 1 addition & 0 deletions packages/kotti-ui/source/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ISO8601 = 'YYYY-MM-DD' as const
export const ISO8601_SECONDS = 'YYYY-MM-DD HH:mm:ss' as const

// eslint-disable-next-line no-magic-numbers
Expand Down
12 changes: 12 additions & 0 deletions packages/kotti-ui/source/kotti-filters/components/FilterRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,19 @@ export default defineComponent({
props.column?.type === KottiFilters.FilterType.INTEGER
? props.column.maximum
: undefined,
maximumDate:
props.column?.type === KottiFilters.FilterType.DATE_RANGE
? props.column.maximumDate
: undefined,
minimum:
props.column?.type === KottiFilters.FilterType.FLOAT ||
props.column?.type === KottiFilters.FilterType.INTEGER
? props.column.minimum
: undefined,
minimumDate:
props.column?.type === KottiFilters.FilterType.DATE_RANGE
? props.column.minimumDate
: undefined,
options:
props.column?.type === KottiFilters.FilterType.MULTI_ENUM ||
props.column?.type === KottiFilters.FilterType.SINGLE_ENUM
Expand All @@ -204,6 +212,10 @@ export default defineComponent({
props.column?.type === KottiFilters.FilterType.INTEGER
? props.column.prefix
: undefined,
shortcuts:
props.column?.type === KottiFilters.FilterType.DATE_RANGE
? props.column.shortcuts
: undefined,
step:
props.column?.type === KottiFilters.FilterType.FLOAT
? props.column.step
Expand Down
5 changes: 4 additions & 1 deletion packages/kotti-ui/source/kotti-filters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ export namespace KottiFilters {
> = Common & {
operations: OPERATION[]
type: FilterType.DATE_RANGE
}
} & Pick<
KottiFieldDateRange.Props,
'maximumDate' | 'minimumDate' | 'shortcuts'
>

export type Float<OPERATION extends Operation.Float = Operation.Float> =
Common & {
Expand Down
Loading