populate initial YMM for specific collection

Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
Chloe 2024-07-08 22:18:29 +07:00
parent f8ef1b6350
commit 26a90359d0
No known key found for this signature in database
GPG Key ID: CFD53CE570D42DF5

View File

@ -47,13 +47,14 @@ const FiltersList = ({ makes = [], menu, autoFocusField }: FiltersListProps) =>
const yearOptions = model ? years.filter((y) => get(y, 'make_model') === model.id) : years; const yearOptions = model ? years.filter((y) => get(y, 'make_model') === model.id) : years;
const disabled = !partType || !make || !model || !year; const disabled = !partType || !make || !model || !year;
const [, initialMake, initialModel, initialYear] = params.collection?.split('_') || [];
useEffect(() => { useEffect(() => {
if (partType) { if (partType) {
const _make = makes.find((make) => const _make = makes.find((make) =>
makeIdFromSearchParams makeIdFromSearchParams
? make.id === makeIdFromSearchParams ? make.id === makeIdFromSearchParams
: params.collection?.includes(make.name!.toLowerCase()) : initialMake === make.name!.toLowerCase()
); );
if (_make) { if (_make) {
@ -72,12 +73,15 @@ const FiltersList = ({ makes = [], menu, autoFocusField }: FiltersListProps) =>
const getModels = async () => { const getModels = async () => {
setLoadingAttribute('models'); setLoadingAttribute('models');
const modelsResponse = await fetchModels(); const modelsResponse = await fetchModels();
if (modelIdFromSearchParams) {
setModel( setModel(
(currentModel) => (currentModel) =>
modelsResponse?.find((model) => model.id === modelIdFromSearchParams) || currentModel modelsResponse?.find((model) =>
modelIdFromSearchParams
? model.id === modelIdFromSearchParams
: initialModel === model.name!.toLowerCase()
) || currentModel
); );
}
setModels(modelsResponse || []); setModels(modelsResponse || []);
setLoadingAttribute(undefined); setLoadingAttribute(undefined);
}; };
@ -91,12 +95,12 @@ const FiltersList = ({ makes = [], menu, autoFocusField }: FiltersListProps) =>
const getYears = async () => { const getYears = async () => {
setLoadingAttribute('years'); setLoadingAttribute('years');
const yearsResponse = await fetchYears(); const yearsResponse = await fetchYears();
if (yearIdFromSearchParams) {
setYear( setYear(
(currentYear) => (currentYear) =>
yearsResponse?.find((year) => year.id === yearIdFromSearchParams) || currentYear yearsResponse?.find((year) =>
yearIdFromSearchParams ? year.id === yearIdFromSearchParams : initialYear === year.name
) || currentYear
); );
}
setYears(yearsResponse || []); setYears(yearsResponse || []);
setLoadingAttribute(undefined); setLoadingAttribute(undefined);
}; };