Skip to content

Commit

Permalink
refactor: refactor choice display
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Mar 1, 2024
1 parent 4561aab commit ad6c8bc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
18 changes: 3 additions & 15 deletions web/frontend/src/pages/form/components/RankResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { FC } from 'react';
import { RankQuestion } from 'types/configuration';
import { ProgressBar } from './ProgressBar';
import { countRankResult } from './utils/countResult';
import { default as i18n } from 'i18next';
import { prettifyChoice } from './utils/display';

type RankResultProps = {
rank: RankQuestion;
Expand All @@ -20,12 +20,7 @@ const RankResult: FC<RankResultProps> = ({ rank, rankResult }) => {
return (
<React.Fragment key={index}>
<div className="px-2 sm:px-4 break-words max-w-xs w-max">
<span>
{i18n.language === 'en' && rank.ChoicesMap.ChoicesMap.get('en')[index]}
{i18n.language === 'fr' && rank.ChoicesMap.ChoicesMap.get('fr')[index]}
{i18n.language === 'de' && rank.ChoicesMap.ChoicesMap.get('de')[index]}
</span>
:
<span>{prettifyChoice(rank.ChoicesMap, index)}</span>:
</div>
<ProgressBar isBest={isBest}>{percent}</ProgressBar>
</React.Fragment>
Expand All @@ -48,14 +43,7 @@ export const IndividualRankResult: FC<RankResultProps> = ({ rank, rankResult })
<React.Fragment key={`rank_${index}`}>
<div className="flex flex-row px-2 sm:px-4 break-words max-w-xs w-max">
<div className="mr-2 font-bold">{index + 1}:</div>
<div>
{i18n.language === 'en' &&
rank.ChoicesMap.ChoicesMap.get('en')[rankResult[0].indexOf(index)]}
{i18n.language === 'fr' &&
rank.ChoicesMap.ChoicesMap.get('fr')[rankResult[0].indexOf(index)]}
{i18n.language === 'de' &&
rank.ChoicesMap.ChoicesMap.get('de')[rankResult[0].indexOf(index)]}
</div>
<div>{prettifyChoice(rank.ChoicesMap, rankResult[0].indexOf(index))}</div>
</div>
</React.Fragment>
);
Expand Down
22 changes: 3 additions & 19 deletions web/frontend/src/pages/form/components/SelectResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@ import React, { FC } from 'react';
import { SelectQuestion } from 'types/configuration';
import { SelectProgressBar } from './ProgressBar';
import { countSelectResult } from './utils/countResult';
import { default as i18n } from 'i18next';
import { prettifyChoice } from './utils/display';

type SelectResultProps = {
select: SelectQuestion;
selectResult: number[][];
};

const prettifyChoice = (select, index) => {
const choice = (
select.ChoicesMap.ChoicesMap.has(i18n.language)
? select.ChoicesMap.ChoicesMap.get(i18n.language)
: select.ChoicesMap.ChoicesMap.get('en')
)[index];
const url = select.ChoicesMap.URLs[index];
return url ? (
<a href={url} style={{ color: 'blue', textDecoration: 'underline' }}>
{choice}
</a>
) : (
choice
);
};

// Display the results of a select question.
const SelectResult: FC<SelectResultProps> = ({ select, selectResult }) => {
const sortedResults = countSelectResult(selectResult)
Expand All @@ -40,7 +24,7 @@ const SelectResult: FC<SelectResultProps> = ({ select, selectResult }) => {
return (
<React.Fragment key={index}>
<div className="px-2 sm:px-4 break-words max-w-xs w-max">
<span>{prettifyChoice(select, origIndex)}</span>:
<span>{prettifyChoice(select.ChoicesMap, origIndex)}</span>:
</div>
<SelectProgressBar
percent={percent}
Expand Down Expand Up @@ -74,7 +58,7 @@ export const IndividualSelectResult: FC<SelectResultProps> = ({ select, selectRe
<React.Fragment key={`select_${index}`}>
<div className="flex flex-row px-2 sm:px-4 break-words max-w-xs w-max">
<div className="h-4 w-4 mr-2 accent-indigo-500 ">{displayChoices(result, index)}</div>
<div>{prettifyChoice(select, index)}</div>
<div>{prettifyChoice(select.ChoicesMap, index)}</div>
</div>
</React.Fragment>
);
Expand Down
11 changes: 2 additions & 9 deletions web/frontend/src/pages/form/components/TextResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { FC } from 'react';
import { TextQuestion } from 'types/configuration';
import { ProgressBar } from './ProgressBar';
import { countTextResult } from './utils/countResult';
import { default as i18n } from 'i18next';
import { prettifyChoice } from './utils/display';

type TextResultProps = {
textResult: string[][];
Expand Down Expand Up @@ -45,14 +45,7 @@ export const IndividualTextResult: FC<IndividualTextResultProps> = ({ text, text
return (
<React.Fragment key={`txt_${index}`}>
<div className="flex flex-row px-2 sm:px-4 break-words max-w-xs w-max">
<div className="mr-2 font-bold">
{
(text.ChoicesMap.ChoicesMap.has(i18n.language)
? text.ChoicesMap.ChoicesMap.get(i18n.language)
: text.ChoicesMap.ChoicesMap.get('en'))[index]
}
:
</div>
<div className="mr-2 font-bold">{prettifyChoice(text.ChoicesMap, index)}:</div>
<div>{result}</div>
</div>
</React.Fragment>
Expand Down
17 changes: 17 additions & 0 deletions web/frontend/src/pages/form/components/utils/display.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { default as i18n } from 'i18next';

export const prettifyChoice = (choicesMap, index) => {
const choice = (
choicesMap.ChoicesMap.has(i18n.language)
? choicesMap.ChoicesMap.get(i18n.language)
: choicesMap.ChoicesMap.get('en')
)[index];
const url = choicesMap.URLs[index];
return url ? (
<a href={url} style={{ color: 'blue', textDecoration: 'underline' }}>
{choice}
</a>
) : (
choice
);
};

0 comments on commit ad6c8bc

Please sign in to comment.