Skip to content

Commit

Permalink
Implement raw date filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Sep 21, 2024
1 parent b3156c1 commit 007cda3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
13 changes: 10 additions & 3 deletions src/lib/Crud/Form.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CrudOperation, FieldInterface, FieldOptions } from '$lib';

export type SubmittedData = Record<string, FormDataEntryValue>;
export type SubmittedData = Record<string, FormDataEntryValue|Array<FormDataEntryValue>>;

/**
* Function to get an record of {@link FormDataEntryValue} items from an "onSubmit" form {@link SubmitEvent} object.
Expand All @@ -18,8 +18,15 @@ export function getSubmittedFormData(event: SubmitEvent): SubmittedData {
return {};
}

new FormData(target as HTMLFormElement, event.submitter).forEach((value, key) => {
normalizedData[key] = value;
const formData = new FormData(target as HTMLFormElement, event.submitter);

formData.forEach((value, key) => {
if (normalizedData[key] && !Array.isArray(normalizedData[key])) {
normalizedData[key] = [normalizedData[key]];
normalizedData[key].push(value);
} else {
normalizedData[key] = value;
}
});

return normalizedData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
delete data[key];
}
});
console.info('submitted filters', data);
dispatchEvent('submitFilters', data);
}
Expand All @@ -67,8 +66,8 @@
{/if}

{#if filters.length}
<Accordion open={Object.keys(filtersValues).length > 0}>
<AccordionItem>
<Accordion>
<AccordionItem open={Object.values(filtersValues).filter(i => !!i).length > 0}>
<slot slot="title">
<FilterIcon />
{$_('datatable.filters.menu_title')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
export let action: Action;
export let action_arguments: Array<unknown> = [];
console.info('Action: ', action);
</script>

{#if action instanceof UrlAction}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
let from: string = '' ;
let to: string = '' ;
// Extracted from Carbon's DatePicker types
type ChangeEvent = string | {
selectedDates: [dateFrom: Date, dateTo?: Date | undefined];
dateStr: string | { from: string; to: string; };
};
if (value && Array.isArray(value)) {
if (value[0]) {
from = value[0];
}
if (value[1]) {
to = value[1];
}
}
</script>

<FilterContainer {filter}>
<DatePicker datePickerType="range" bind:valueFrom={from} bind:valueTo={to}>
<DatePickerInput labelText={$_('filters.date.from')} name={filter.field + '[0]'} value={from || ''} />
<DatePickerInput labelText={$_('filters.date.to')} name={filter.field + '[1]'} value={to || ''} />
<DatePickerInput labelText={$_('filters.date.from')} name={filter.field} value={from || ''} />
<DatePickerInput labelText={$_('filters.date.to')} name={filter.field} value={to || ''} />
</DatePicker>
</FilterContainer>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe(
expect(label).toBeDefined();
expect(label?.innerHTML).toStrictEqual('Text field');
const valueElement = label?.parentElement?.nextElementSibling;
console.info('+=====================+');
expect(valueElement).toBeDefined();
expect(valueElement?.childNodes).toBeDefined();
expect(valueElement?.childNodes[0]).toBeInstanceOf(Text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@
(def: CrudDefinition<unknown>) => def.name === field.options.crud_name
)[0] ?? undefined;
console.info('CrudEntityField created with params: ', { field, operation, value });
async function fetchData() {
if (!crud) {
console.error('No CRUD to fetch data from.');
return;
}
console.info('Fetching...');
const fieldOperation = new SingleField(
field.options.get_provider_operation?.name ?? 'entity_view',
field.options.get_provider_operation?.options ?? {}
);
fieldOperation.crud = crud;
fieldOperation.dashboard = operation.dashboard;
console.info('Shallow field: ', fieldOperation);
console.info('value: ', value);
return crud.options.stateProvider.provide(fieldOperation, {
field_value: value
Expand Down
11 changes: 7 additions & 4 deletions src/testApp/BookCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,19 @@ export const bookCrud = new CrudDefinition<Book>({
return false;
}
if (filters.publishedAt) {
console.info('PUBLISHED AT', filters, filters.publishedAt);
return true;
const publishedAtFilter = filters.publishedAt as unknown as [string|undefined, string|undefined];
if (publishedAtFilter[0] && new Date(entity.publishedAt) < new Date(publishedAtFilter[0])) {
return false;
}
if (publishedAtFilter[1] && new Date(entity.publishedAt) > new Date(publishedAtFilter[1])) {
return false;
}
}

return true;
});
}

console.info('Sort: ', requestParameters.sort);

if (requestParameters.sort) {
if (!requestParameters.sort?.title && !requestParameters.sort?.publishedAt) {
console.warn('Sorting not supported for these sort parameters:');
Expand Down

0 comments on commit 007cda3

Please sign in to comment.