Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
desoindx committed Dec 16, 2024
1 parent 2703aea commit 4422672
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/components/shareable/overScreens/TransportComparison.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useTranslations } from 'next-intl'
import { Dispatch, SetStateAction, useEffect, useMemo } from 'react'
import { ComputedEquivalent } from 'types/equivalent'
import { computedEquivalents } from 'data/categories/computedEquivalents'
import { deplacements } from 'data/categories/deplacement'
import { getEquivalentWithCarpool } from 'utils/carpool'
Expand All @@ -9,6 +10,24 @@ import styles from './TransportListParam.module.css'

const equivalents = computedEquivalents('transport', deplacements).flatMap(getEquivalentWithCarpool)

const findNextEquivalent = (equivalents: ComputedEquivalent[], comparison: string) => {
const [slug] = comparison.split('+')
let equivalent = equivalents.find((equivalent) => {
const [equivalentSlug] = equivalent.slug.split('+')
return slug !== equivalentSlug
})
if (equivalent) {
return equivalent.slug
}

equivalent = equivalents.find((equivalent) => slug !== equivalent.slug)
if (equivalent) {
return equivalent.slug
}

return equivalents[0].slug
}

const TransportComparison = ({
comparison,
setComparison,
Expand Down Expand Up @@ -47,11 +66,9 @@ const TransportComparison = ({
if (!equivalent1 && !equivalent2) {
setComparison([filteredEquivalents[0].slug, filteredEquivalents[1].slug])
} else if (!equivalent1) {
const newComparison = filteredEquivalents[0].slug
setComparison([newComparison === comparison[1] ? filteredEquivalents[1].slug : newComparison, comparison[1]])
setComparison([findNextEquivalent(filteredEquivalents, comparison[1]), comparison[1]])
} else if (!equivalent2) {
const newComparison = filteredEquivalents[0].slug
setComparison([comparison[0], newComparison === comparison[0] ? filteredEquivalents[1].slug : newComparison])
setComparison([comparison[0], findNextEquivalent(filteredEquivalents, comparison[0])])
}
}, [equivalent1, equivalent2])

Expand Down

0 comments on commit 4422672

Please sign in to comment.