mirror of
https://github.com/vercel/commerce.git
synced 2025-05-13 05:07:51 +00:00
revert behavior of MMY filter
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
parent
0a2e3d8e38
commit
16673d9c53
@ -1,12 +1,11 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { Button } from '@headlessui/react';
|
import { Button } from '@headlessui/react';
|
||||||
import { PART_TYPES } from 'lib/constants';
|
import { MAKE_FILTER_ID, MODEL_FILTER_ID, PART_TYPES, YEAR_FILTER_ID } from 'lib/constants';
|
||||||
import { Menu, Metaobject } from 'lib/shopify/types';
|
import { Menu, Metaobject } from 'lib/shopify/types';
|
||||||
import { findParentCollection } from 'lib/utils';
|
import { createUrl, findParentCollection } from 'lib/utils';
|
||||||
import get from 'lodash.get';
|
import get from 'lodash.get';
|
||||||
import kebabCase from 'lodash.kebabcase';
|
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useParams, useRouter } from 'next/navigation';
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { fetMetaobjects } from './actions';
|
import { fetMetaobjects } from './actions';
|
||||||
import FilterField from './field';
|
import FilterField from './field';
|
||||||
@ -20,7 +19,10 @@ type FiltersListProps = {
|
|||||||
const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
||||||
const params = useParams<{ collection?: string }>();
|
const params = useParams<{ collection?: string }>();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [, initialMake, initialModel, initialYear] = params.collection?.split('_') || [];
|
const searchParams = useSearchParams();
|
||||||
|
const makeIdFromSearchParams = searchParams.get(MAKE_FILTER_ID);
|
||||||
|
const modelIdFromSearchParams = searchParams.get(MODEL_FILTER_ID);
|
||||||
|
const yearIdFromSearchParams = searchParams.get(YEAR_FILTER_ID);
|
||||||
|
|
||||||
const parentCollection = params.collection ? findParentCollection(menu, params.collection) : null;
|
const parentCollection = params.collection ? findParentCollection(menu, params.collection) : null;
|
||||||
// get the active collection (if any) to identify the default part type.
|
// get the active collection (if any) to identify the default part type.
|
||||||
@ -33,15 +35,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
(partTypeCollection && partTypeCollection.includes(type.value))
|
(partTypeCollection && partTypeCollection.includes(type.value))
|
||||||
) || null
|
) || null
|
||||||
);
|
);
|
||||||
const [make, setMake] = useState<Metaobject | null>(
|
const [make, setMake] = useState<Metaobject | null>(null);
|
||||||
(partType &&
|
|
||||||
makes.find((make) =>
|
|
||||||
initialMake
|
|
||||||
? kebabCase(make.name) === initialMake
|
|
||||||
: params.collection?.includes(make.name!.toLowerCase())
|
|
||||||
)) ||
|
|
||||||
null
|
|
||||||
);
|
|
||||||
const [model, setModel] = useState<Metaobject | null>(null);
|
const [model, setModel] = useState<Metaobject | null>(null);
|
||||||
const [year, setYear] = useState<Metaobject | null>(null);
|
const [year, setYear] = useState<Metaobject | null>(null);
|
||||||
|
|
||||||
@ -57,8 +51,8 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (partType) {
|
if (partType) {
|
||||||
const _make = makes.find((make) =>
|
const _make = makes.find((make) =>
|
||||||
initialMake
|
makeIdFromSearchParams
|
||||||
? kebabCase(make.name) === initialMake
|
? make.id === makeIdFromSearchParams
|
||||||
: params.collection?.includes(make.name!.toLowerCase())
|
: params.collection?.includes(make.name!.toLowerCase())
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -72,16 +66,16 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [initialMake, makes, params.collection, partType]);
|
}, [makeIdFromSearchParams, makes, params.collection, partType]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchModels = async () => {
|
const fetchModels = async () => {
|
||||||
setLoadingAttribute('models');
|
setLoadingAttribute('models');
|
||||||
const modelsResponse = await fetMetaobjects('make_model_composite');
|
const modelsResponse = await fetMetaobjects('make_model_composite');
|
||||||
if (initialModel) {
|
if (modelIdFromSearchParams) {
|
||||||
setModel(
|
setModel(
|
||||||
(currentModel) =>
|
(currentModel) =>
|
||||||
modelsResponse?.find((model) => kebabCase(model.name) === initialModel) || currentModel
|
modelsResponse?.find((model) => model.id === modelIdFromSearchParams) || currentModel
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
setModels(modelsResponse || []);
|
setModels(modelsResponse || []);
|
||||||
@ -91,15 +85,16 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
if (make?.id && models.length === 0) {
|
if (make?.id && models.length === 0) {
|
||||||
fetchModels();
|
fetchModels();
|
||||||
}
|
}
|
||||||
}, [make?.id, initialModel, models.length]);
|
}, [make?.id, modelIdFromSearchParams, models.length]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchYears = async () => {
|
const fetchYears = async () => {
|
||||||
setLoadingAttribute('years');
|
setLoadingAttribute('years');
|
||||||
const yearsResponse = await fetMetaobjects('make_model_year_composite');
|
const yearsResponse = await fetMetaobjects('make_model_year_composite');
|
||||||
if (initialYear) {
|
if (yearIdFromSearchParams) {
|
||||||
setYear(
|
setYear(
|
||||||
(currentYear) => yearsResponse?.find((year) => year.name === initialYear) || currentYear
|
(currentYear) =>
|
||||||
|
yearsResponse?.find((year) => year.id === yearIdFromSearchParams) || currentYear
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
setYears(yearsResponse || []);
|
setYears(yearsResponse || []);
|
||||||
@ -109,7 +104,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
if (model?.id && years.length === 0) {
|
if (model?.id && years.length === 0) {
|
||||||
fetchYears();
|
fetchYears();
|
||||||
}
|
}
|
||||||
}, [model?.id, initialYear, years.length]);
|
}, [model?.id, yearIdFromSearchParams, years.length]);
|
||||||
|
|
||||||
const onChangeMake = async (value: Metaobject | null) => {
|
const onChangeMake = async (value: Metaobject | null) => {
|
||||||
setMake(value);
|
setMake(value);
|
||||||
@ -134,10 +129,11 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const onSearch = () => {
|
const onSearch = () => {
|
||||||
router.push(
|
const newSearchParams = new URLSearchParams(searchParams.toString());
|
||||||
`/${partType?.value}/${kebabCase(make?.name)}/${kebabCase(model?.name)}/${kebabCase(year?.name)}`,
|
newSearchParams.set(MAKE_FILTER_ID, make?.id || '');
|
||||||
{ scroll: false }
|
newSearchParams.set(MODEL_FILTER_ID, model?.id || '');
|
||||||
);
|
newSearchParams.set(YEAR_FILTER_ID, year?.id || '');
|
||||||
|
router.push(createUrl(`/search/${partType?.value}`, newSearchParams), { scroll: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user