Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SFS-2085] refactor: sortVariationsByLabel logic when not numbers and redability #1128

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Changed

- Fully refactor of `sortVariationsByLabel` property

## [3.176.1] - 2024-11-08

## [3.176.0] - 2024-11-06
Expand Down
29 changes: 20 additions & 9 deletions react/components/SKUSelector/components/SKUSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ function isSkuAvailable(item?: SelectorProductItem) {
return seller.commertialOffer?.AvailableQuantity > 0
}

function sortOptionsNumerically(options: DisplayOption[]) {
return options.sort((a, b) => parseFloat(a.label) - parseFloat(b.label))
}

function sortOptionsAlphabetically(options: DisplayOption[]) {
return options.sort((a, b) => {
if (a.label < b.label) return -1
if (a.label > b.label) return 1

return 0
})
}

const showItemAsAvailable = ({
possibleItems,
selectedVariations,
Expand Down Expand Up @@ -265,7 +278,7 @@ const variationNameToDisplayVariation =
(variationName: string): DisplayVariation => {
const name = variationName
const { values, originalName } = variations[variationName]
const options = values
let options = values
.map(
parseOptionNameToDisplayOption({
selectedVariations,
Expand All @@ -281,17 +294,15 @@ const variationNameToDisplayVariation =
.filter(Boolean) as DisplayOption[]

if (sortVariationsByLabel) {
const allNumbers = options.every(
(option: any) => !Number.isNaN(option.label)
const areAllNumbers = options.every(
(option: DisplayOption) => !Number.isNaN(Number(option.label))
)

options.sort((a: any, b: any) => {
if (allNumbers) {
return a.label - b.label
}
const sortedOptions = areAllNumbers
? sortOptionsNumerically([...options])
: sortOptionsAlphabetically([...options])

return a.label < b.label ? -1 : a.label > b.label ? 1 : 0
})
options = [...sortedOptions]
}

return { name, originalName, options }
Expand Down
Loading