mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
fix: improve MMY filters
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
parent
4cbc6962b9
commit
a077bbe753
12
components/filters/actions.ts
Normal file
12
components/filters/actions.ts
Normal file
@ -0,0 +1,12 @@
|
||||
'use server';
|
||||
|
||||
import { getAllMetaobjects } from 'lib/shopify';
|
||||
|
||||
export const fetMetaobjects = async (type: string) => {
|
||||
try {
|
||||
const data = await getAllMetaobjects(type);
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.log('fetMetaobjects action', error);
|
||||
}
|
||||
};
|
@ -8,6 +8,7 @@ import {
|
||||
ComboboxOptions
|
||||
} from '@headlessui/react';
|
||||
import { ChevronDownIcon } from '@heroicons/react/16/solid';
|
||||
import Spinner from 'components/spinner';
|
||||
import get from 'lodash.get';
|
||||
import { useCallback, useState } from 'react';
|
||||
|
||||
@ -22,6 +23,7 @@ type FilterFieldProps<T extends { [key: string]: unknown }> = {
|
||||
getId: (option: T) => string;
|
||||
disabled?: boolean;
|
||||
autoFocus?: boolean;
|
||||
isLoading?: boolean;
|
||||
};
|
||||
|
||||
const FilterField = <T extends { [key: string]: unknown }>({
|
||||
@ -32,6 +34,7 @@ const FilterField = <T extends { [key: string]: unknown }>({
|
||||
displayKey = 'name',
|
||||
getId,
|
||||
disabled,
|
||||
isLoading,
|
||||
autoFocus = false
|
||||
}: FilterFieldProps<T>) => {
|
||||
const [query, setQuery] = useState('');
|
||||
@ -63,7 +66,7 @@ const FilterField = <T extends { [key: string]: unknown }>({
|
||||
onChange={onChange}
|
||||
onClose={() => setQuery('')}
|
||||
immediate
|
||||
disabled={disabled}
|
||||
disabled={disabled || isLoading}
|
||||
>
|
||||
<div className="relative">
|
||||
<ComboboxInput
|
||||
@ -75,7 +78,11 @@ const FilterField = <T extends { [key: string]: unknown }>({
|
||||
autoFocus={autoFocus}
|
||||
/>
|
||||
<ComboboxButton className="group absolute inset-y-0 right-0 px-2.5 data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50">
|
||||
<ChevronDownIcon className="size-5 fill-black/60 group-data-[hover]:fill-black" />
|
||||
{isLoading ? (
|
||||
<Spinner className="fill-black/60" />
|
||||
) : (
|
||||
<ChevronDownIcon className="size-5 fill-black/60 group-data-[hover]:fill-black" />
|
||||
)}
|
||||
</ComboboxButton>
|
||||
</div>
|
||||
<ComboboxOptions
|
||||
|
@ -6,18 +6,17 @@ import { Menu, Metaobject } from 'lib/shopify/types';
|
||||
import { createUrl, findParentCollection } from 'lib/utils';
|
||||
import get from 'lodash.get';
|
||||
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useState, useTransition } from 'react';
|
||||
import { fetMetaobjects } from './actions';
|
||||
import FilterField from './field';
|
||||
|
||||
type FiltersListProps = {
|
||||
years: Metaobject[];
|
||||
models: Metaobject[];
|
||||
makes: Metaobject[];
|
||||
menu: Menu[];
|
||||
autoFocusField?: string;
|
||||
};
|
||||
|
||||
const FiltersList = ({ years, makes, models, menu, autoFocusField }: FiltersListProps) => {
|
||||
const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
||||
const params = useParams<{ collection?: string }>();
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
@ -32,6 +31,12 @@ const FiltersList = ({ years, makes, models, menu, autoFocusField }: FiltersList
|
||||
);
|
||||
|
||||
const makeIdFromSearchParams = searchParams.get(MAKE_FILTER_ID);
|
||||
const modelIdFromSearchParams = searchParams.get(MODEL_FILTER_ID);
|
||||
const yearIdFromSearchParams = searchParams.get(YEAR_FILTER_ID);
|
||||
|
||||
const [models, setModels] = useState<Metaobject[]>([]);
|
||||
const [years, setYears] = useState<Metaobject[]>([]);
|
||||
const [isLoading, startTransition] = useTransition();
|
||||
|
||||
const [make, setMake] = useState<Metaobject | null>(
|
||||
(partType &&
|
||||
@ -75,13 +80,43 @@ const FiltersList = ({ years, makes, models, menu, autoFocusField }: FiltersList
|
||||
}
|
||||
}, [makeIdFromSearchParams, makes, params.collection, partType]);
|
||||
|
||||
const onChangeMake = (value: Metaobject | null) => {
|
||||
useEffect(() => {
|
||||
if (make?.id && models.length === 0) {
|
||||
startTransition(async () => {
|
||||
const modelsResponse = await fetMetaobjects('make_model_composite');
|
||||
if (modelIdFromSearchParams) {
|
||||
setModel(
|
||||
(currentModel) =>
|
||||
modelsResponse?.find((model) => model.id === modelIdFromSearchParams) || currentModel
|
||||
);
|
||||
}
|
||||
setModels(modelsResponse || []);
|
||||
});
|
||||
}
|
||||
}, [make?.id, modelIdFromSearchParams, models.length]);
|
||||
|
||||
useEffect(() => {
|
||||
if (model?.id && years.length === 0) {
|
||||
startTransition(async () => {
|
||||
const yearsResponse = await fetMetaobjects('make_model_year_composite');
|
||||
if (yearIdFromSearchParams) {
|
||||
setYear(
|
||||
(currentYear) =>
|
||||
yearsResponse?.find((year) => year.id === yearIdFromSearchParams) || currentYear
|
||||
);
|
||||
}
|
||||
setYears(yearsResponse || []);
|
||||
});
|
||||
}
|
||||
}, [model?.id, yearIdFromSearchParams, years.length]);
|
||||
|
||||
const onChangeMake = async (value: Metaobject | null) => {
|
||||
setMake(value);
|
||||
setModel(null);
|
||||
setYear(null);
|
||||
};
|
||||
|
||||
const onChangeModel = (value: Metaobject | null) => {
|
||||
const onChangeModel = async (value: Metaobject | null) => {
|
||||
setModel(value);
|
||||
setYear(null);
|
||||
};
|
||||
@ -134,6 +169,7 @@ const FiltersList = ({ years, makes, models, menu, autoFocusField }: FiltersList
|
||||
getId={(option) => option.id}
|
||||
disabled={!make}
|
||||
autoFocus={autoFocusField === 'model'}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
<FilterField
|
||||
label="Year"
|
||||
@ -143,6 +179,7 @@ const FiltersList = ({ years, makes, models, menu, autoFocusField }: FiltersList
|
||||
getId={(option) => option.id}
|
||||
disabled={!model || !make}
|
||||
autoFocus={autoFocusField === 'year'}
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
<Button
|
||||
onClick={onSearch}
|
||||
|
@ -12,25 +12,16 @@ const title: Record<string, string> = {
|
||||
const { STORE_PREFIX } = process.env;
|
||||
|
||||
const HomePageFilters = async () => {
|
||||
const yearsData = getAllMetaobjects('make_model_year_composite');
|
||||
const modelsData = getAllMetaobjects('make_model_composite');
|
||||
const makesData = getAllMetaobjects('make');
|
||||
|
||||
const [years, models, makes] = await Promise.all([yearsData, modelsData, makesData]);
|
||||
const makes = await getAllMetaobjects('make');
|
||||
const menu = await getMenu('main-menu');
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1 className="text-4xl font-bold tracking-tight text-white lg:text-6xl">
|
||||
{title[STORE_PREFIX!] || 'Find Your Car Part'}
|
||||
</h1>
|
||||
<div className="mt-5 flex grow flex-col items-center gap-3 @md:flex-row">
|
||||
<FiltersList
|
||||
years={years}
|
||||
makes={makes}
|
||||
models={models}
|
||||
menu={menu}
|
||||
autoFocusField="partType"
|
||||
/>
|
||||
<FiltersList makes={makes} menu={menu} autoFocusField="partType" />
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
@ -14,17 +14,13 @@ const YMMFiltersContainer = ({ children }: { children: ReactNode }) => {
|
||||
};
|
||||
|
||||
const YMMFilters = async () => {
|
||||
const yearsData = getAllMetaobjects('make_model_year_composite');
|
||||
const modelsData = getAllMetaobjects('make_model_composite');
|
||||
const makesData = getAllMetaobjects('make');
|
||||
|
||||
const [years, models, makes] = await Promise.all([yearsData, modelsData, makesData]);
|
||||
const makes = await getAllMetaobjects('make');
|
||||
const menu = await getMenu('main-menu');
|
||||
|
||||
return (
|
||||
<YMMFiltersContainer>
|
||||
<div className="flex grow flex-col items-center gap-3 @md:flex-row">
|
||||
<FiltersList years={years} makes={makes} models={models} menu={menu} />
|
||||
<FiltersList makes={makes} menu={menu} />
|
||||
</div>
|
||||
</YMMFiltersContainer>
|
||||
);
|
||||
|
27
components/spinner.tsx
Normal file
27
components/spinner.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
import { cn } from 'lib/utils';
|
||||
|
||||
const Spinner = ({ className }: { className?: string }) => {
|
||||
return (
|
||||
<div role="status">
|
||||
<svg
|
||||
aria-hidden="true"
|
||||
className={cn('inline h-5 w-5 animate-spin fill-gray-600 text-gray-200', className)}
|
||||
viewBox="0 0 100 101"
|
||||
fill="none"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
>
|
||||
<path
|
||||
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
<path
|
||||
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
|
||||
fill="currentFill"
|
||||
/>
|
||||
</svg>
|
||||
<span className="sr-only">Loading...</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default Spinner;
|
@ -43,7 +43,11 @@ import {
|
||||
} from './queries/collection';
|
||||
import { getCustomerQuery } from './queries/customer';
|
||||
import { getMenuQuery } from './queries/menu';
|
||||
import { getMetaobjectQuery, getMetaobjectsQuery } from './queries/metaobject';
|
||||
import {
|
||||
getMetaobjectQuery,
|
||||
getMetaobjectsQuery,
|
||||
getOrderConfirmationQuery
|
||||
} from './queries/metaobject';
|
||||
import { getFileQuery, getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
|
||||
import getCustomerOrderQuery from './queries/order';
|
||||
import { getCustomerOrdersQuery } from './queries/orders';
|
||||
@ -934,7 +938,7 @@ export async function getMetaobject({
|
||||
|
||||
export async function getOrderConfirmationContent(): Promise<OrderConfirmationContent> {
|
||||
const res = await shopifyFetch<ShopifyMetaobjectOperation>({
|
||||
query: getMetaobjectQuery,
|
||||
query: getOrderConfirmationQuery,
|
||||
variables: { handle: { handle: 'order-confirmation-pdf', type: 'order_confirmation_pdf' } }
|
||||
});
|
||||
|
||||
|
@ -34,6 +34,21 @@ export const getMetaobjectQuery = /* GraphQL */ `
|
||||
... on Metaobject {
|
||||
id
|
||||
}
|
||||
}
|
||||
key
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const getOrderConfirmationQuery = /* GraphQL */ `
|
||||
query getOrderConfirmation($handle: MetaobjectHandleInput) {
|
||||
metaobject(handle: $handle) {
|
||||
id
|
||||
type
|
||||
fields {
|
||||
reference {
|
||||
... on MediaImage {
|
||||
image {
|
||||
url
|
||||
|
Loading…
x
Reference in New Issue
Block a user