@smg-automotive/filters 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/README.md +28 -9
  2. package/dist/cjs/contexts/Filter.d.ts +4 -1
  3. package/dist/cjs/filters/__tests__/createTestFilterComponent.d.ts +4 -0
  4. package/dist/cjs/filters/__tests__/createTestReferenceData.d.ts +4 -0
  5. package/dist/cjs/filters/useFuelTypeFilter.d.ts +4 -0
  6. package/dist/cjs/hooks/useCheckboxFilter.d.ts +22 -0
  7. package/dist/cjs/hooks/useCheckboxFilterReducer.d.ts +49 -0
  8. package/dist/cjs/index.d.ts +4 -1
  9. package/dist/cjs/index.js +617 -7
  10. package/dist/cjs/index.js.map +1 -1
  11. package/dist/cjs/types/query.d.ts +2 -0
  12. package/dist/cjs/types/referenceData.d.ts +23 -0
  13. package/dist/cjs/utilities/checkbox.d.ts +63 -0
  14. package/dist/cjs/utilities/fetchReferenceData.d.ts +2 -0
  15. package/dist/cjs/utilities/isInArray.d.ts +1 -0
  16. package/dist/cjs/utilities/range.d.ts +37 -0
  17. package/dist/contexts/Filter.d.ts +4 -1
  18. package/dist/esm/contexts/Filter.d.ts +4 -1
  19. package/dist/esm/filters/__tests__/createTestFilterComponent.d.ts +4 -0
  20. package/dist/esm/filters/__tests__/createTestReferenceData.d.ts +4 -0
  21. package/dist/esm/filters/useFuelTypeFilter.d.ts +4 -0
  22. package/dist/esm/hooks/useCheckboxFilter.d.ts +22 -0
  23. package/dist/esm/hooks/useCheckboxFilterReducer.d.ts +49 -0
  24. package/dist/esm/index.d.ts +4 -1
  25. package/dist/esm/index.js +618 -10
  26. package/dist/esm/index.js.map +1 -1
  27. package/dist/esm/types/query.d.ts +2 -0
  28. package/dist/esm/types/referenceData.d.ts +23 -0
  29. package/dist/esm/utilities/checkbox.d.ts +63 -0
  30. package/dist/esm/utilities/fetchReferenceData.d.ts +2 -0
  31. package/dist/esm/utilities/isInArray.d.ts +1 -0
  32. package/dist/esm/utilities/range.d.ts +37 -0
  33. package/dist/filters/__tests__/createTestFilterComponent.d.ts +4 -0
  34. package/dist/filters/__tests__/createTestReferenceData.d.ts +4 -0
  35. package/dist/filters/useFuelTypeFilter.d.ts +4 -0
  36. package/dist/hooks/useCheckboxFilter.d.ts +22 -0
  37. package/dist/hooks/useCheckboxFilterReducer.d.ts +49 -0
  38. package/dist/index.d.ts +4 -1
  39. package/dist/translations.cjs.js +88 -4
  40. package/dist/translations.cjs.js.map +1 -1
  41. package/dist/translations.esm.js +88 -4
  42. package/dist/translations.esm.js.map +1 -1
  43. package/dist/types/query.d.ts +2 -0
  44. package/dist/types/referenceData.d.ts +23 -0
  45. package/dist/utilities/checkbox.d.ts +63 -0
  46. package/dist/utilities/fetchReferenceData.d.ts +2 -0
  47. package/dist/utilities/isInArray.d.ts +1 -0
  48. package/dist/utilities/range.d.ts +37 -0
  49. package/package.json +3 -3
  50. package/dist/cjs/utilities/facets.d.ts +0 -37
  51. package/dist/esm/utilities/facets.d.ts +0 -37
  52. package/dist/utilities/facets.d.ts +0 -37
package/README.md CHANGED
@@ -8,10 +8,10 @@ npm install @smg-automotive/filters
8
8
 
9
9
  ## Environment variables
10
10
 
11
- | Variable | Used by | Required | Description |
12
- |----------|---------|----------|-------------|
13
- | `API_ROOT` | `fetchSearchConfiguration` | Yes (for search config) | Base URL of the search-configuration API. |
14
- | `API_VERSION` | `fetchSearchConfiguration` | Yes (for search config) | API version segment (e.g. `v1`). |
11
+ | Variable | Used by | Required | Description |
12
+ |----------|--------------------------------------------------|----------|-------------|
13
+ | `API_ROOT` | `fetchSearchConfiguration`, `fetchReferenceData` | Yes (for search config) | Base URL of the search-configuration API. |
14
+ | `API_VERSION` | `fetchSearchConfiguration`, `fetchReferenceData` | Yes (for search config) | API version segment (e.g. `v1`). |
15
15
 
16
16
  ## Reference data
17
17
 
@@ -23,7 +23,11 @@ Use the package’s **`fetchSearchConfiguration`** utility so the app doesn’t
23
23
 
24
24
  ### Filter options
25
25
 
26
- TODO
26
+ | Option | Type | Source | Description |
27
+ |--------|------|--------|-------------|
28
+ | `referenceData` | `ReferenceData` | `fetchReferenceData()` or build-time JSON | Per–vehicle-category reference data (fuel types, body types, etc.) for checkbox filters. |
29
+
30
+ Use **`fetchReferenceData`** to load reference data; it calls the reference-data API for each vehicle category (car, utility, motorcycle, truck, trailer, camper) and uses `process.env.API_ROOT` and `process.env.API_VERSION`. For build-time prefetch, call it during your build and write the result to JSON, then import and pass it into `FilterProvider`.
27
31
 
28
32
  ## i18n setup (translations)
29
33
 
@@ -51,20 +55,35 @@ If you are not in a server/RSC context, you can instead import from the main ent
51
55
 
52
56
  ## FilterProvider
53
57
 
54
- **Wrap your filter tree with `FilterProvider`** and pass `t`, `brand`, and `histogramRanges`. Do this inside a component that is already under `I18nProvider`. Pass the result of `fetchSearchConfiguration` (or the JSON you wrote at build time) as `histogramRanges`:
58
+ **Wrap your filter tree with `FilterProvider`** and pass `t`, `brand`, `histogramRanges`, `referenceData`, and `language`. Do this inside a component that is already under `I18nProvider`. Pass the result of `fetchSearchConfiguration` (or the JSON you wrote at build time) as `histogramRanges`. Pass your app’s reference data (e.g. from prefetched data) as `referenceData` so checkbox filters (e.g. fuel type) can read options per vehicle category. Pass the current locale as `language` (e.g. from `useI18n().language`).
59
+
60
+ | Prop | Type | Required | Description |
61
+ |------|------|----------|-------------|
62
+ | `t` | `TFunction` | Yes | Translation function from `useI18n()` (or equivalent). |
63
+ | `brand` | `Brand` | Yes | Current brand (e.g. `AutoScout24`, `MotoScout24`). |
64
+ | `histogramRanges` | `HistogramRanges` | Yes | Search configuration for range filters; from `fetchSearchConfiguration` or build-time JSON. |
65
+ | `referenceData` | `ReferenceData` | Yes | Per–vehicle-category reference data (fuel types, body types, etc.). Used by checkbox filters. |
66
+ | `language` | `Language` | Yes | Current UI language (`'de'`, `'en'`, `'fr'`, or `'it'`). Used for sorting and display. |
55
67
 
56
68
  ```tsx
57
69
  import { useI18n } from 'shared/utilities/i18nInit';
58
70
  import { useDataDrivenAppRendering } from 'shared/contexts/dataDrivenRendering/App';
59
71
  import { FilterProvider } from '@smg-automotive/filters';
60
- // e.g. from a file written at build time using fetchSearchConfiguration:
72
+ // e.g. from files written at build time:
61
73
  import { searchConfiguration as histogramRanges } from 'prefetchedData';
74
+ import { referenceData } from 'prefetchedData';
62
75
 
63
76
  function FilterLayout() {
64
- const { t } = useI18n();
77
+ const { t, language } = useI18n();
65
78
  const { brand } = useDataDrivenAppRendering();
66
79
  return (
67
- <FilterProvider t={t} brand={brand} histogramRanges={histogramRanges}>
80
+ <FilterProvider
81
+ t={t}
82
+ brand={brand}
83
+ histogramRanges={histogramRanges}
84
+ referenceData={referenceData}
85
+ language={language}
86
+ >
68
87
  <YourFilterUI />
69
88
  </FilterProvider>
70
89
  );
@@ -1,11 +1,14 @@
1
1
  import { type FC, type PropsWithChildren } from 'react';
2
- import type { TFunction } from '@smg-automotive/i18n-pkg';
2
+ import type { Language, TFunction } from '@smg-automotive/i18n-pkg';
3
+ import { ReferenceData } from '../types/referenceData';
3
4
  import type { HistogramRanges } from '../types/facets';
4
5
  import type { Brand } from '../types/brand';
5
6
  export type FilterContextType = {
6
7
  t: TFunction;
7
8
  brand: Brand;
8
9
  histogramRanges: HistogramRanges;
10
+ referenceData: ReferenceData;
11
+ language: Language;
9
12
  };
10
13
  export declare const useFilterContext: () => FilterContextType;
11
14
  export type FilterProviderProps = PropsWithChildren<FilterContextType>;
@@ -1,5 +1,7 @@
1
1
  import React from 'react';
2
+ import type { Language } from '@smg-automotive/i18n-pkg';
2
3
  import type { SharedArgs, SharedReturn } from 'src/types/useFilter';
4
+ import type { ReferenceData } from 'src/types/referenceData';
3
5
  import type { Query } from 'src/types/query';
4
6
  import type { HistogramRanges } from 'src/types/facets';
5
7
  import { VehicleCategory } from 'src/types/enums';
@@ -15,5 +17,7 @@ export declare function createTestFilterComponent(useFilterHook: (args: SharedAr
15
17
  }) => SharedReturn<Partial<Query>>, options: {
16
18
  brand: Brand;
17
19
  histogramRanges: HistogramRanges;
20
+ referenceData?: ReferenceData;
21
+ language: Language;
18
22
  }): (args: Args) => React.JSX.Element;
19
23
  export {};
@@ -0,0 +1,4 @@
1
+ import type { ReferenceData, ReferenceDataByCategory } from 'src/types/referenceData';
2
+ import { VehicleCategory } from 'src/types/enums';
3
+ export declare function createTestReferenceData(partial?: Partial<Record<VehicleCategory, Partial<ReferenceDataByCategory>>>): ReferenceData;
4
+ export declare const carFuelTypeReferenceData: ReferenceData;
@@ -0,0 +1,4 @@
1
+ import type { UseCheckboxGroupFilter } from '../types/useFilter';
2
+ import type { Query } from '../types/query';
3
+ export declare const useFuelTypeFilter: UseCheckboxGroupFilter<Partial<Pick<Query, 'fuelTypes' | 'fuelTypeGroups'>>>;
4
+ export default useFuelTypeFilter;
@@ -0,0 +1,22 @@
1
+ import { CheckboxFilterItem } from '@smg-automotive/components';
2
+ import { type CheckboxFilterName, type GenerateCheckboxFilterItemsArgs, type Group, type Option } from '../utilities/checkbox';
3
+ import type { SharedArgs } from '../types/useFilter';
4
+ import type { Query } from '../types/query';
5
+ export type CheckboxItem = CheckboxFilterItem<Group | Option, CheckboxFilterName>;
6
+ export type CheckboxItems = CheckboxItem[];
7
+ type Args<Filters extends Partial<Query>, O extends Option, G extends Group> = {
8
+ isSearchable: boolean;
9
+ } & Omit<GenerateCheckboxFilterItemsArgs<O, G>, 'language'> & Pick<SharedArgs<Filters>, 'filters' | 'onAddFilter' | 'onChange'>;
10
+ declare const useCheckboxFilter: <Filters extends Partial<Query>, OptionType extends Option, GroupType extends Group>({ filters, onAddFilter, onChange: onFilterChange, ...generateCheckboxFilterItemsArgs }: Args<Filters, OptionType, GroupType>) => {
11
+ state: {
12
+ [x: string]: string[] | Group[];
13
+ };
14
+ checkboxItems: CheckboxItems;
15
+ onApply: (updatedItem: CheckboxItem) => void;
16
+ numberOfAppliedFilters: number;
17
+ isApplied: boolean;
18
+ reset: () => {
19
+ [x: string]: never[];
20
+ };
21
+ };
22
+ export default useCheckboxFilter;
@@ -0,0 +1,49 @@
1
+ import type { Group, Option } from '../utilities/checkbox';
2
+ export type State<OptionType, GroupType> = {
3
+ options: OptionType[];
4
+ groups: GroupType[];
5
+ };
6
+ export declare enum ActionType {
7
+ DeselectChildOption = "DESELECT_CHILD_OPTION",
8
+ SelectChildOption = "SELECT_CHILD_OPTION",
9
+ DeselectGroup = "DESELECT_GROUP",
10
+ SelectGroup = "SELECT_GROUP",
11
+ DeselectChildAndGroupAndAddAllOtherChildOptions = "DESELECT_CHILD_AND_GROUP_AND_ADD_ALL_OTHER_CHILD_OPTIONS",
12
+ SetFilter = "SET_FILTER",
13
+ Reset = "RESET"
14
+ }
15
+ export type Action<OptionType extends Option, GroupType extends Group> = {
16
+ type: ActionType.DeselectChildOption | ActionType.SelectChildOption | ActionType.DeselectGroup;
17
+ payload: {
18
+ key: GroupType | OptionType;
19
+ };
20
+ } | {
21
+ type: ActionType.SelectGroup;
22
+ payload: {
23
+ key: GroupType | OptionType;
24
+ parentGroupChildItems: OptionType[];
25
+ };
26
+ } | {
27
+ type: ActionType.DeselectChildAndGroupAndAddAllOtherChildOptions;
28
+ payload: {
29
+ key: GroupType | OptionType;
30
+ parentGroup: GroupType;
31
+ parentGroupChildItems: OptionType[];
32
+ };
33
+ } | {
34
+ type: ActionType.SetFilter;
35
+ payload: {
36
+ options: OptionType[];
37
+ groups: GroupType[];
38
+ };
39
+ } | {
40
+ type: ActionType.Reset;
41
+ };
42
+ export declare const reducer: <OptionType extends Option, GroupType extends Group>(state: State<OptionType, GroupType>, action: Action<OptionType, GroupType>) => State<OptionType, GroupType>;
43
+ export declare const useCheckboxFilterReducer: <OptionType extends Option, GroupType extends Group>({ initialState, onChange, }: {
44
+ initialState: State<OptionType, GroupType>;
45
+ onChange: (state: State<OptionType, GroupType>) => void;
46
+ }) => {
47
+ state: State<OptionType, GroupType>;
48
+ dispatch: (action: Action<OptionType, GroupType>) => void;
49
+ };
@@ -1,9 +1,12 @@
1
1
  export { BodyType, ColorType, ConditionType, ConditionTypeGroup, DriveSystem, DriveType, EmissionStandard, EnergyLabelType, EquipmentSearchAttributeGroup, EquipmentSearchAttribute, GeoLocation, FuelType, FuelTypeGroup, LicenseCategory, SellerType, TransmissionType, TransmissionTypeGroup, VehicleCategory, } from './types/enums';
2
- export type { MakeModelVersionQuery, Query, RangeFilters } from './types/query';
2
+ export type { MakeModelVersionQuery, Query, RangeFilters, CheckboxFilters, CheckboxGroupFilters, } from './types/query';
3
+ export type { ReferenceData, ReferenceDataByCategory, } from './types/referenceData';
3
4
  export type { Facet, Facets, FacetName, FacetNameWithRanges, FacetNameWithRange, HistogramRanges, } from './types/facets';
4
5
  export { FilterProvider, type FilterProviderProps } from './contexts/Filter';
5
6
  export { resetAllFilters, useResetAllFilters, } from './utilities/resetAllFilters';
6
7
  export { fetchSearchConfiguration } from './utilities/fetchSearchConfiguration';
8
+ export { fetchReferenceData } from './utilities/fetchReferenceData';
7
9
  export { filterTranslations, filterTranslationsDe, filterTranslationsEn, filterTranslationsFr, filterTranslationsIt, type FilterTranslationLocale, } from './translations';
8
10
  export { cleanQuery } from './utilities/cleanQuery';
9
11
  export { useMileageFilter } from './filters/useMileageFilter';
12
+ export { useFuelTypeFilter } from './filters/useFuelTypeFilter';