-
Notifications
You must be signed in to change notification settings - Fork 119
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
refactor: improve typing of sorting in facetGetters/category.vue #1080
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const perPageOptions = [10, 20, 50]; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,50 @@ | ||
import type { | ||
AgnosticPagination, | ||
AgnosticSort, | ||
} from '~/composables/types'; | ||
|
||
import { ProductInterface } from '~/modules/GraphQL/types'; | ||
import { FacetSearchParams } from '~/modules/catalog/category/composables/useFacet'; | ||
import { PerPageOptions } from '~/modules/catalog/category/composables/useFacet/PerPageOptions'; | ||
|
||
export interface FacetSearchResult<S> { | ||
data: S; | ||
input: FacetSearchParams; | ||
import { perPageOptions } from '~/modules/catalog/category/composables/useFacet/perPageOptions'; | ||
import type { ProductInterface } from '~/modules/GraphQL/types'; | ||
import type { AgnosticPagination } from '~/composables/types'; | ||
import type { SortingModel } from '~/modules/catalog/category/composables/useFacet/sortingOptions'; | ||
import type { UseFacetSearchResult } from '~/modules/catalog/category/composables/useFacet/useFacet'; | ||
|
||
export interface FacetsGetters { | ||
getSortOptions: (searchData: UseFacetSearchResult) => SortingModel; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously the type here was |
||
getProducts: (searchData: UseFacetSearchResult) => ProductInterface[]; | ||
getPagination: (searchData: UseFacetSearchResult) => AgnosticPagination; | ||
} | ||
|
||
export interface FacetsGetters<SEARCH_DATA, RESULTS = any> { | ||
getSortOptions: (searchData: FacetSearchResult<SEARCH_DATA>) => AgnosticSort; | ||
getProducts: (searchData: FacetSearchResult<SEARCH_DATA>) => RESULTS; | ||
getPagination: (searchData: FacetSearchResult<SEARCH_DATA>) => AgnosticPagination; | ||
[getterName: string]: (element: any, options?: any) => unknown; | ||
} | ||
const facetGetters: FacetsGetters = { | ||
getSortOptions(searchData) { | ||
if (!searchData || !searchData.data || !searchData.data.availableSortingOptions) { | ||
return { | ||
options: [], | ||
selected: '', | ||
} as SortingModel; | ||
} | ||
|
||
const getSortOptions = (searchData): AgnosticSort => { | ||
if (!searchData || !searchData.data || !searchData.data.availableSortingOptions) { | ||
return { | ||
options: [], | ||
selected: '', | ||
} as AgnosticSort; | ||
} | ||
options: searchData.data.availableSortingOptions, | ||
selected: searchData.input.sort, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if there will be no sort param? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CategoryNavbar will not show any sort option. This will only happen if someone puts a sort id that doesn't exist into the url. |
||
}; | ||
}, | ||
getProducts(searchData) { | ||
if (!searchData || !searchData.data || !searchData.data.items) { | ||
return []; | ||
} | ||
return searchData.data.items; | ||
}, | ||
getPagination(searchData) { | ||
const totalPages = (searchData?.data) ? ( | ||
Number.isNaN(Math.ceil(searchData.data.total / searchData.input.itemsPerPage)) | ||
? 1 | ||
: Math.ceil(searchData.data.total / searchData.input.itemsPerPage) | ||
) : 1; | ||
|
||
return { | ||
options: searchData.data.availableSortingOptions, | ||
selected: searchData.input.sort, | ||
}; | ||
}; | ||
|
||
const getProducts = (searchData): ProductInterface[] => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I inlined the methods into the exported asserted interface since I didn't want to duplicate the typedefs |
||
if (!searchData || !searchData.data || !searchData.data.items) { | ||
return []; | ||
} | ||
return searchData.data.items; | ||
}; | ||
|
||
const getPagination = (searchData): AgnosticPagination => { | ||
const totalPages = (searchData?.data) ? ( | ||
Number.isNaN(Math.ceil(searchData.data.total / searchData.input.itemsPerPage)) | ||
? 1 | ||
: Math.ceil(searchData.data.total / searchData.input.itemsPerPage) | ||
) : 1; | ||
|
||
return { | ||
currentPage: (searchData?.input?.page > totalPages ? 1 : searchData?.input?.page) || 1, | ||
totalPages, | ||
totalItems: (searchData?.data?.total) ? searchData.data.total : 0, | ||
itemsPerPage: searchData?.input?.itemsPerPage || 10, | ||
pageOptions: PerPageOptions, | ||
}; | ||
}; | ||
|
||
const facetGetters: FacetsGetters<any, any> = { | ||
getSortOptions, | ||
getProducts, | ||
getPagination, | ||
return { | ||
currentPage: (searchData?.input?.page > totalPages ? 1 : searchData?.input?.page) || 1, | ||
totalPages, | ||
totalItems: (searchData?.data?.total) ? searchData.data.total : 0, | ||
itemsPerPage: searchData?.input?.itemsPerPage || 10, | ||
pageOptions: perPageOptions, | ||
}; | ||
}, | ||
}; | ||
|
||
export default facetGetters; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SortingModel as to avoid having "SortingOptions" and "SortingOption" interfaces with similar names