mirror of
https://github.com/vercel/commerce.git
synced 2025-05-12 12:47:50 +00:00
improve YMM loading time
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
parent
a7d2816836
commit
735657f606
@ -1,12 +1,43 @@
|
|||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
import { getAllMetaobjects } from 'lib/shopify';
|
import { getAllMetaobjects } from 'lib/shopify';
|
||||||
|
import { Metaobject } from 'lib/shopify/types';
|
||||||
|
import get from 'lodash.get';
|
||||||
|
import { cache } from 'react';
|
||||||
|
|
||||||
export const fetMetaobjects = async (type: string) => {
|
export const fetMetaobjects = async (
|
||||||
|
type: string,
|
||||||
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
sortFn?: (a: Metaobject, b: Metaobject) => number
|
||||||
|
) => {
|
||||||
try {
|
try {
|
||||||
const data = await getAllMetaobjects(type);
|
const data = await getAllMetaobjects(type);
|
||||||
return data;
|
|
||||||
|
return sortFn ? data.toSorted(sortFn) : data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('fetMetaobjects action', error);
|
console.log('fetMetaobjects action', error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sortModelsFn = (a: Metaobject, b: Metaobject) => {
|
||||||
|
const modelA = get(a, 'name').toLowerCase();
|
||||||
|
const modelB = get(b, 'name').toLowerCase();
|
||||||
|
return modelA.localeCompare(modelB);
|
||||||
|
};
|
||||||
|
|
||||||
|
const sortYearsFn = (a: Metaobject, b: Metaobject) => {
|
||||||
|
const yearA = parseInt(get(a, 'name'), 10);
|
||||||
|
const yearB = parseInt(get(b, 'name'), 10);
|
||||||
|
return yearB - yearA; // Descending order for years
|
||||||
|
};
|
||||||
|
|
||||||
|
const sortMakesFn = (a: Metaobject, b: Metaobject) => {
|
||||||
|
const makeA = get(a, 'display_name').toLowerCase();
|
||||||
|
const makeB = get(b, 'display_name').toLowerCase();
|
||||||
|
return makeA.localeCompare(makeB);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const fetchModels = cache(() => fetMetaobjects('make_model_composite', sortModelsFn));
|
||||||
|
export const fetchYears = cache(() => fetMetaobjects('make_model_year_composite', sortYearsFn));
|
||||||
|
|
||||||
|
export const fetchMakes = cache(() => fetMetaobjects('make', sortMakesFn));
|
||||||
|
@ -6,17 +6,17 @@ import { Menu, Metaobject } from 'lib/shopify/types';
|
|||||||
import { createUrl, findParentCollection } from 'lib/utils';
|
import { createUrl, findParentCollection } from 'lib/utils';
|
||||||
import get from 'lodash.get';
|
import get from 'lodash.get';
|
||||||
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
import { useParams, useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { fetMetaobjects } from './actions';
|
import { fetchModels, fetchYears } from './actions';
|
||||||
import FilterField from './field';
|
import FilterField from './field';
|
||||||
|
|
||||||
type FiltersListProps = {
|
type FiltersListProps = {
|
||||||
makes: Metaobject[];
|
makes?: Metaobject[];
|
||||||
menu: Menu[];
|
menu: Menu[];
|
||||||
autoFocusField?: string;
|
autoFocusField?: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
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 searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
@ -69,9 +69,9 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
}, [makeIdFromSearchParams, makes, params.collection, partType]);
|
}, [makeIdFromSearchParams, makes, params.collection, partType]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchModels = async () => {
|
const getModels = async () => {
|
||||||
setLoadingAttribute('models');
|
setLoadingAttribute('models');
|
||||||
const modelsResponse = await fetMetaobjects('make_model_composite');
|
const modelsResponse = await fetchModels();
|
||||||
if (modelIdFromSearchParams) {
|
if (modelIdFromSearchParams) {
|
||||||
setModel(
|
setModel(
|
||||||
(currentModel) =>
|
(currentModel) =>
|
||||||
@ -83,14 +83,14 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (make?.id && models.length === 0) {
|
if (make?.id && models.length === 0) {
|
||||||
fetchModels();
|
getModels();
|
||||||
}
|
}
|
||||||
}, [make?.id, modelIdFromSearchParams, models.length]);
|
}, [make?.id, modelIdFromSearchParams, models.length]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchYears = async () => {
|
const getYears = async () => {
|
||||||
setLoadingAttribute('years');
|
setLoadingAttribute('years');
|
||||||
const yearsResponse = await fetMetaobjects('make_model_year_composite');
|
const yearsResponse = await fetchYears();
|
||||||
if (yearIdFromSearchParams) {
|
if (yearIdFromSearchParams) {
|
||||||
setYear(
|
setYear(
|
||||||
(currentYear) =>
|
(currentYear) =>
|
||||||
@ -102,7 +102,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (model?.id && years.length === 0) {
|
if (model?.id && years.length === 0) {
|
||||||
fetchYears();
|
getYears();
|
||||||
}
|
}
|
||||||
}, [model?.id, yearIdFromSearchParams, years.length]);
|
}, [model?.id, yearIdFromSearchParams, years.length]);
|
||||||
|
|
||||||
@ -136,31 +136,6 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
router.push(createUrl(`/search/${partType?.value}`, newSearchParams), { scroll: false });
|
router.push(createUrl(`/search/${partType?.value}`, newSearchParams), { scroll: false });
|
||||||
};
|
};
|
||||||
|
|
||||||
// Sorting logic
|
|
||||||
const sortedMakes = useMemo(() => {
|
|
||||||
return [...makes].sort((a, b) => {
|
|
||||||
const makeA = get(a, 'display_name').toLowerCase();
|
|
||||||
const makeB = get(b, 'display_name').toLowerCase();
|
|
||||||
return makeA.localeCompare(makeB);
|
|
||||||
});
|
|
||||||
}, [makes]);
|
|
||||||
|
|
||||||
const sortedModelOptions = useMemo(() => {
|
|
||||||
return [...modelOptions].sort((a, b) => {
|
|
||||||
const modelA = get(a, 'name').toLowerCase();
|
|
||||||
const modelB = get(b, 'name').toLowerCase();
|
|
||||||
return modelA.localeCompare(modelB);
|
|
||||||
});
|
|
||||||
}, [modelOptions]);
|
|
||||||
|
|
||||||
const sortedYearOptions = useMemo(() => {
|
|
||||||
return [...yearOptions].sort((a, b) => {
|
|
||||||
const yearA = parseInt(get(a, 'name'), 10);
|
|
||||||
const yearB = parseInt(get(b, 'name'), 10);
|
|
||||||
return yearB - yearA; // Descending order for years
|
|
||||||
});
|
|
||||||
}, [yearOptions]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FilterField
|
<FilterField
|
||||||
@ -176,7 +151,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Make"
|
label="Make"
|
||||||
onChange={onChangeMake}
|
onChange={onChangeMake}
|
||||||
selectedValue={make}
|
selectedValue={make}
|
||||||
options={sortedMakes}
|
options={makes}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!partType}
|
disabled={!partType}
|
||||||
autoFocus={autoFocusField === 'make'}
|
autoFocus={autoFocusField === 'make'}
|
||||||
@ -186,7 +161,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Model"
|
label="Model"
|
||||||
onChange={onChangeModel}
|
onChange={onChangeModel}
|
||||||
selectedValue={model}
|
selectedValue={model}
|
||||||
options={sortedModelOptions}
|
options={modelOptions}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!make}
|
disabled={!make}
|
||||||
autoFocus={autoFocusField === 'model'}
|
autoFocus={autoFocusField === 'model'}
|
||||||
@ -196,7 +171,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Year"
|
label="Year"
|
||||||
onChange={onChangeYear}
|
onChange={onChangeYear}
|
||||||
selectedValue={year}
|
selectedValue={year}
|
||||||
options={sortedYearOptions}
|
options={yearOptions}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!model || !make}
|
disabled={!model || !make}
|
||||||
autoFocus={autoFocusField === 'year'}
|
autoFocus={autoFocusField === 'year'}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { getAllMetaobjects, getMenu } from 'lib/shopify';
|
import { getMenu } from 'lib/shopify';
|
||||||
|
import { fetchMakes, fetchModels, fetchYears } from './actions';
|
||||||
import FiltersList from './filters-list';
|
import FiltersList from './filters-list';
|
||||||
|
|
||||||
const title: Record<string, string> = {
|
const title: Record<string, string> = {
|
||||||
@ -12,8 +13,11 @@ const title: Record<string, string> = {
|
|||||||
const { STORE_PREFIX } = process.env;
|
const { STORE_PREFIX } = process.env;
|
||||||
|
|
||||||
const HomePageFilters = async () => {
|
const HomePageFilters = async () => {
|
||||||
const makes = await getAllMetaobjects('make');
|
const makes = await fetchMakes();
|
||||||
const menu = await getMenu('main-menu');
|
const menu = await getMenu('main-menu');
|
||||||
|
// preload models and years
|
||||||
|
fetchModels();
|
||||||
|
fetchYears();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { getAllMetaobjects, getMenu } from 'lib/shopify';
|
import { getMenu } from 'lib/shopify';
|
||||||
import { ReactNode } from 'react';
|
import { ReactNode } from 'react';
|
||||||
|
import { fetchMakes, fetchModels, fetchYears } from './actions';
|
||||||
import FiltersList from './filters-list';
|
import FiltersList from './filters-list';
|
||||||
|
|
||||||
const YMMFiltersContainer = ({ children }: { children: ReactNode }) => {
|
const YMMFiltersContainer = ({ children }: { children: ReactNode }) => {
|
||||||
@ -14,8 +15,11 @@ const YMMFiltersContainer = ({ children }: { children: ReactNode }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const YMMFilters = async () => {
|
const YMMFilters = async () => {
|
||||||
const makes = await getAllMetaobjects('make');
|
const makes = await fetchMakes();
|
||||||
const menu = await getMenu('main-menu');
|
const menu = await getMenu('main-menu');
|
||||||
|
// preload models and years
|
||||||
|
fetchModels();
|
||||||
|
fetchYears();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<YMMFiltersContainer>
|
<YMMFiltersContainer>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user