mirror of
https://github.com/vercel/commerce.git
synced 2025-05-12 12:47:50 +00:00
feat: sort the make model and year dropdowns
Signed-off-by: jrphilo <james.philo@me.com>
This commit is contained in:
parent
16673d9c53
commit
e31f7087b2
@ -6,7 +6,7 @@ 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, useState } from 'react';
|
import { useEffect, useMemo, useState } from 'react';
|
||||||
import { fetMetaobjects } from './actions';
|
import { fetMetaobjects } from './actions';
|
||||||
import FilterField from './field';
|
import FilterField from './field';
|
||||||
|
|
||||||
@ -136,6 +136,31 @@ 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
|
||||||
@ -151,7 +176,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Make"
|
label="Make"
|
||||||
onChange={onChangeMake}
|
onChange={onChangeMake}
|
||||||
selectedValue={make}
|
selectedValue={make}
|
||||||
options={makes}
|
options={sortedMakes}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!partType}
|
disabled={!partType}
|
||||||
autoFocus={autoFocusField === 'make'}
|
autoFocus={autoFocusField === 'make'}
|
||||||
@ -161,7 +186,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Model"
|
label="Model"
|
||||||
onChange={onChangeModel}
|
onChange={onChangeModel}
|
||||||
selectedValue={model}
|
selectedValue={model}
|
||||||
options={modelOptions}
|
options={sortedModelOptions}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!make}
|
disabled={!make}
|
||||||
autoFocus={autoFocusField === 'model'}
|
autoFocus={autoFocusField === 'model'}
|
||||||
@ -171,7 +196,7 @@ const FiltersList = ({ makes, menu, autoFocusField }: FiltersListProps) => {
|
|||||||
label="Year"
|
label="Year"
|
||||||
onChange={onChangeYear}
|
onChange={onChangeYear}
|
||||||
selectedValue={year}
|
selectedValue={year}
|
||||||
options={yearOptions}
|
options={sortedYearOptions}
|
||||||
getId={(option) => option.id}
|
getId={(option) => option.id}
|
||||||
disabled={!model || !make}
|
disabled={!model || !make}
|
||||||
autoFocus={autoFocusField === 'year'}
|
autoFocus={autoFocusField === 'year'}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user