diff --git a/frontend/src/features/comparisons/ComparisonInput.tsx b/frontend/src/features/comparisons/ComparisonInput.tsx index 03b7c70d10..a907ab208b 100644 --- a/frontend/src/features/comparisons/ComparisonInput.tsx +++ b/frontend/src/features/comparisons/ComparisonInput.tsx @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { useContext, useState } from 'react'; import { useTranslation } from 'react-i18next'; import { @@ -6,19 +6,20 @@ import { Box, LinearProgress, Paper, - Typography, useMediaQuery, } from '@mui/material'; import { useCurrentPoll } from 'src/hooks'; +import { useOrderedCriteria } from 'src/hooks/useOrderedCriteria'; import { ComparisonRequest } from 'src/services/openapi'; import ComparisonSliders from 'src/features/comparisons/ComparisonSliders'; +import { TutorialContext } from 'src/features/comparisonSeries/TutorialContext'; +import { getCriterionScoreMax } from 'src/utils/criteria'; import CriteriaButtons from './inputs/CriteriaButtons'; import { BUTTON_SCORE_MAX } from './inputs/CriterionButtons'; import { SLIDER_SCORE_MAX } from './CriteriaSlider'; import CriteriaButtonsScoreReview from './inputs/CriteriaButtonsScoreReview'; -import { getCriterionScoreMax } from 'src/utils/criteria'; interface ComparisonInputProps { uidA: string; @@ -28,6 +29,38 @@ interface ComparisonInputProps { isComparisonPublic: boolean; } +interface PositionBarProps { + position: number; + initialComparison: ComparisonRequest | null; +} + +const PositionBar = ({ position, initialComparison }: PositionBarProps) => { + const tutorial = useContext(TutorialContext); + const { options, criterias } = useCurrentPoll(); + const displayedCriteria = useOrderedCriteria(); + + const criterion = displayedCriteria[position]; + const criterionScore = initialComparison?.criteria_scores.find( + (crit) => crit.criteria === criterion.name + ); + + const progressHidden = + tutorial.isActive || + (criterion.name === options?.mainCriterionName && + criterionScore == undefined); + + if (progressHidden) { + return <>; + } + + return ( + + ); +}; + /** * A component displaying different comparison inputs depending to the user's * device. @@ -40,9 +73,12 @@ const ComparisonInput = ({ isComparisonPublic, }: ComparisonInputProps) => { const { t } = useTranslation(); - const { options, criterias } = useCurrentPoll(); + const { options } = useCurrentPoll(); const pointerFine = useMediaQuery('(pointer:fine)', { noSsr: true }); + // Position of the displayed criterion in the list of criteria. + const [position, setPosition] = useState(0); + const mainScoreMax = getCriterionScoreMax( initialComparison?.criteria_scores, options?.mainCriterionName @@ -52,9 +88,9 @@ const ComparisonInput = ({ const slidersUsed = mainScoreMax == SLIDER_SCORE_MAX; const fallBackToButtons = !buttonsUsed && !slidersUsed && !pointerFine; - const progress = initialComparison?.criteria_scores.length - ? (initialComparison?.criteria_scores.length / criterias.length) * 100 - : 0; + const changePosition = (newPos: number) => { + setPosition(newPos); + }; return ( <> @@ -70,25 +106,13 @@ const ComparisonInput = ({ uidA={uidA || ''} uidB={uidB || ''} onSubmit={onSubmit} + onPositionChanged={changePosition} + initialComparison={initialComparison} + /> + - - - - - - {progress}% - - diff --git a/frontend/src/features/comparisons/inputs/CriteriaButtons.tsx b/frontend/src/features/comparisons/inputs/CriteriaButtons.tsx index 8c1e9b5faa..318c8cda1d 100644 --- a/frontend/src/features/comparisons/inputs/CriteriaButtons.tsx +++ b/frontend/src/features/comparisons/inputs/CriteriaButtons.tsx @@ -23,6 +23,8 @@ interface CriteriaButtonsProps { uidB: string; initialComparison: ComparisonRequest | null; onSubmit: (c: ComparisonRequest, partialUpdate?: boolean) => Promise; + // Called when a new criterion is displayed. + onPositionChanged: (pos: number) => void; } const removeCriteria = ( @@ -43,6 +45,7 @@ const CriteriaButtons = ({ uidB, initialComparison, onSubmit, + onPositionChanged, }: CriteriaButtonsProps) => { const { t } = useTranslation(); const containerRef = useRef(null); @@ -95,6 +98,7 @@ const CriteriaButtons = ({ if (submittedScore === undefined) { setSlideIn(true); + onPositionChanged(nextCritPosition); return; } @@ -113,6 +117,7 @@ const CriteriaButtons = ({ try { await onSubmit(comparisonRequest, true); + onPositionChanged(nextCritPosition); } catch { setCritPosition(critPosition); } finally {