Skip to content

Commit

Permalink
refactor: use Title object instead of MainTitle, TitleDe and TitleFr
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Nov 9, 2023
1 parent 0757c3b commit 348cff1
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 220 deletions.
26 changes: 5 additions & 21 deletions web/frontend/src/pages/ballot/components/BallotDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Rank, { handleOnDragEnd } from './Rank';
import Select from './Select';
import Text from './Text';
import { DragDropContext } from 'react-beautiful-dnd';
import { isJson } from 'types/JSONparser';

type BallotDisplayProps = {
configuration: Configuration;
Expand All @@ -24,18 +23,8 @@ const BallotDisplay: FC<BallotDisplayProps> = ({
}) => {
const [titles, setTitles] = useState<any>({});
useEffect(() => {
if (configuration.MainTitle === '') return;
if (isJson(configuration.MainTitle)) {
const ts = JSON.parse(configuration.MainTitle);
setTitles(ts);
} else {
const t = {
en: configuration.MainTitle,
fr: configuration.TitleFr,
de: configuration.TitleDe,
};
setTitles(t);
}
if (configuration.Title === undefined) return;
setTitles(configuration.Title);
}, [configuration]);

const SubjectElementDisplay = (element: types.SubjectElement) => {
Expand Down Expand Up @@ -65,17 +54,12 @@ const BallotDisplay: FC<BallotDisplayProps> = ({
};

const SubjectTree = (subject: types.Subject) => {
let sbj;
if (isJson(subject.Title)) {
sbj = JSON.parse(subject.Title);
}
if (sbj === undefined) sbj = { en: subject.Title, fr: subject.TitleFr, de: subject.TitleDe };
return (
<div key={subject.ID}>
<h3 className="text-xl break-all pt-1 pb-1 sm:pt-2 sm:pb-2 border-t font-bold text-gray-600">
{language === 'en' && sbj.en}
{language === 'fr' && sbj.fr}
{language === 'de' && sbj.de}
{language === 'en' && subject.Title.en}
{language === 'fr' && subject.Title.fr}
{language === 'de' && subject.Title.de}
</h3>
{subject.Order.map((id: ID) => (
<div key={id}>
Expand Down
8 changes: 1 addition & 7 deletions web/frontend/src/pages/ballot/components/Rank.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Draggable, DropResult, Droppable } from 'react-beautiful-dnd';
import { Answers, ID, RankQuestion } from 'types/configuration';
import { answersFrom } from 'types/getObjectType';
import HintButton from 'components/buttons/HintButton';
import { isJson } from 'types/JSONparser';

export const handleOnDragEnd = (
result: DropResult,
Expand Down Expand Up @@ -59,12 +58,7 @@ const Rank: FC<RankProps> = ({ rank, answers, language }) => {
};
const [titles, setTitles] = useState<any>({});
useEffect(() => {
if (isJson(rank.Title)) {
const ts = JSON.parse(rank.Title);
setTitles(ts);
} else {
setTitles({ en: rank.Title, fr: rank.TitleFr, de: rank.TitleDe });
}
setTitles(rank.Title);
}, [rank]);
const choiceDisplay = (choice: string, rankIndex: number) => {
return (
Expand Down
8 changes: 1 addition & 7 deletions web/frontend/src/pages/ballot/components/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { useTranslation } from 'react-i18next';
import { Answers, SelectQuestion } from 'types/configuration';
import { answersFrom } from 'types/getObjectType';
import HintButton from 'components/buttons/HintButton';
import { isJson } from 'types/JSONparser';
type SelectProps = {
select: SelectQuestion;
answers: Answers;
Expand Down Expand Up @@ -57,12 +56,7 @@ const Select: FC<SelectProps> = ({ select, answers, setAnswers, language }) => {
};
const [titles, setTitles] = useState<any>({});
useEffect(() => {
if (isJson(select.Title)) {
const ts = JSON.parse(select.Title);
setTitles(ts);
} else {
setTitles({ en: select.Title, fr: select.TitleFr, de: select.TitleDe });
}
setTitles(select.Title);
}, [select]);

const choiceDisplay = (isChecked: boolean, choice: string, choiceIndex: number) => {
Expand Down
6 changes: 3 additions & 3 deletions web/frontend/src/pages/ballot/components/Text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ const Text: FC<TextProps> = ({ text, answers, setAnswers, language }) => {
<div className="grid grid-rows-1 grid-flow-col">
<div>
<h3 className="text-lg break-words text-gray-600 w-96">
{language === 'en' && text.Title}
{language === 'fr' && text.TitleFr}
{language === 'de' && text.TitleDe}
{language === 'en' && text.Title.en}
{language === 'fr' && text.Title.fr}
{language === 'de' && text.Title.de}
</h3>
</div>
<div className="text-right">
Expand Down
40 changes: 12 additions & 28 deletions web/frontend/src/pages/form/GroupedResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
import { default as i18n } from 'i18next';
import SelectResult from './components/SelectResult';
import TextResult from './components/TextResult';
import { isJson } from 'types/JSONparser';

type GroupedResultProps = {
rankResult: RankResults;
Expand All @@ -53,12 +52,7 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR

const SubjectElementResultDisplay = (element: SubjectElement) => {
let titles;
if (isJson(element.Title)) {
titles = JSON.parse(element.Title);
}
if (titles === undefined) {
titles = { en: element.Title, fr: element.TitleFr, de: element.TitleDe };
}
titles = element.Title;
return (
<div className="pl-4 pb-4 sm:pl-6 sm:pb-6">
<div className="flex flex-row">
Expand Down Expand Up @@ -88,19 +82,12 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
};

const displayResults = (subject: Subject) => {
let sbj;
if (isJson(subject.Title)) {
sbj = JSON.parse(subject.Title);
}
if (sbj === undefined) {
sbj = { en: subject.Title, fr: subject.TitleFr, de: subject.TitleDe };
}
return (
<div key={subject.ID}>
<h2 className="text-xl pt-1 pb-1 sm:pt-2 sm:pb-2 border-t font-bold text-gray-600">
{i18n.language === 'en' && sbj.en}
{i18n.language === 'fr' && sbj.fr}
{i18n.language === 'de' && sbj.de}
{i18n.language === 'en' && subject.Title.en}
{i18n.language === 'fr' && subject.Title.fr}
{i18n.language === 'de' && subject.Title.de}
</h2>
{subject.Order.map((id: ID) => (
<div key={id}>
Expand All @@ -118,7 +105,7 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
};

const getResultData = (subject: Subject, dataToDownload: DownloadedResults[]) => {
dataToDownload.push({ Title: subject.Title });
dataToDownload.push({ Title: subject.Title.en });

subject.Order.forEach((id: ID) => {
const element = subject.Elements.get(id);
Expand All @@ -134,7 +121,7 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
return { Candidate: rank.Choices[index], Percentage: `${percent}%` };
}
);
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;

Expand All @@ -145,7 +132,7 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
res = countSelectResult(selectResult.get(id)).resultsInPercent.map((percent, index) => {
return { Candidate: select.Choices[index], Percentage: `${percent}%` };
});
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;

Expand All @@ -158,18 +145,17 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
res = Array.from(countTextResult(textResult.get(id)).resultsInPercent).map((r) => {
return { Candidate: r[0], Percentage: `${r[1]}%` };
});
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;
}
});
};

const exportJSONData = () => {
const fileName = `result_${configuration.MainTitle.replace(/[^a-zA-Z0-9]/g, '_').slice(
0,
99
)}__grouped`; // replace spaces with underscores;
const fileName = `result_${configuration.Title.en
.replace(/[^a-zA-Z0-9]/g, '_')
.slice(0, 99)}__grouped`; // replace spaces with underscores;

const dataToDownload: DownloadedResults[] = [];

Expand All @@ -178,9 +164,7 @@ const GroupedResult: FC<GroupedResultProps> = ({ rankResult, selectResult, textR
});

const data = {
MainTitle: configuration.MainTitle,
TitleFr: configuration.TitleFr,
TitleDe: configuration.TitleDe,
Title: configuration.Title,
NumberOfVotes: result.length,
Results: dataToDownload,
};
Expand Down
46 changes: 14 additions & 32 deletions web/frontend/src/pages/form/IndividualResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import Loading from 'pages/Loading';
import saveAs from 'file-saver';
import { useNavigate } from 'react-router';
import { default as i18n } from 'i18next';
import { isJson } from 'types/JSONparser';

type IndividualResultProps = {
rankResult: RankResults;
Expand Down Expand Up @@ -70,23 +69,16 @@ const IndividualResult: FC<IndividualResultProps> = ({
[TEXT]: <MenuAlt1Icon />,
};

let titles;
if (isJson(element.Title)) {
titles = JSON.parse(element.Title);
}
if (titles === undefined) {
titles = { en: element.Title, fr: element.TitleFr, de: element.TitleDe };
}
return (
<div className="pl-4 pb-4 sm:pl-6 sm:pb-6">
<div className="flex flex-row">
<div className="align-text-middle flex mt-1 mr-2 h-5 w-5" aria-hidden="true">
{questionIcons[element.Type]}
</div>
<h2 className="flex align-text-middle text-lg pb-2">
{i18n.language === 'en' && titles.en}
{i18n.language === 'fr' && titles.fr}
{i18n.language === 'de' && titles.de}
{i18n.language === 'en' && element.Title.en}
{i18n.language === 'fr' && element.Title.fr}
{i18n.language === 'de' && element.Title.de}
</h2>
</div>
{element.Type === RANK && rankResult.has(element.ID) && (
Expand Down Expand Up @@ -115,19 +107,12 @@ const IndividualResult: FC<IndividualResultProps> = ({

const displayResults = useCallback(
(subject: Subject) => {
let sbj;
if (isJson(subject.Title)) {
sbj = JSON.parse(subject.Title);
}
if (sbj === undefined) {
sbj = { en: subject.Title, fr: subject.TitleFr, de: subject.TitleDe };
}
return (
<div key={subject.ID}>
<h2 className="text-xl pt-1 pb-1 sm:pt-2 sm:pb-2 border-t font-bold text-gray-600">
{i18n.language === 'en' && sbj.en}
{i18n.language === 'fr' && sbj.fr}
{i18n.language === 'de' && sbj.de}
{i18n.language === 'en' && subject.Title.en}
{i18n.language === 'fr' && subject.Title.fr}
{i18n.language === 'de' && subject.Title.de}
</h2>
{subject.Order.map((id: ID) => (
<div key={id}>
Expand All @@ -151,7 +136,7 @@ const IndividualResult: FC<IndividualResultProps> = ({
dataToDownload: DownloadedResults[],
BallotID: number
) => {
dataToDownload.push({ Title: subject.Title });
dataToDownload.push({ Title: subject.Title.en });

subject.Order.forEach((id: ID) => {
const element = subject.Elements.get(id);
Expand All @@ -168,7 +153,7 @@ const IndividualResult: FC<IndividualResultProps> = ({
Choice: rankQues.Choices[rankResult.get(id)[BallotID].indexOf(index)],
};
});
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;

Expand All @@ -180,7 +165,7 @@ const IndividualResult: FC<IndividualResultProps> = ({
const checked = select ? 'True' : 'False';
return { Candidate: selectQues.Choices[index], Checked: checked };
});
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;

Expand All @@ -195,18 +180,17 @@ const IndividualResult: FC<IndividualResultProps> = ({
res = textResult.get(id)[BallotID].map((text, index) => {
return { Field: textQues.Choices[index], Answer: text };
});
dataToDownload.push({ Title: element.Title, Results: res });
dataToDownload.push({ Title: element.Title.en, Results: res });
}
break;
}
});
};

const exportJSONData = () => {
const fileName = `result_${configuration.MainTitle.replace(/[^a-zA-Z0-9]/g, '_').slice(
0,
99
)}__individual`;
const fileName = `result_${configuration.Title.en
.replace(/[^a-zA-Z0-9]/g, '_')
.slice(0, 99)}__individual`;
const ballotsToDownload: BallotResults[] = [];

const indices: number[] = [...Array(ballotNumber).keys()];
Expand All @@ -219,9 +203,7 @@ const IndividualResult: FC<IndividualResultProps> = ({
});

const data = {
MainTitle: configuration.MainTitle,
TitleFr: configuration.TitleFr,
TitleDe: configuration.TitleDe,
Title: configuration.Title,
NumberOfVotes: ballotNumber,
Ballots: ballotsToDownload,
};
Expand Down
6 changes: 3 additions & 3 deletions web/frontend/src/pages/form/Result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ const FormResult: FC = () => {
{t('totalNumberOfVotes', { votes: result.length })}
</h2>
<h3 className="py-6 border-t text-2xl text-center text-gray-700">
{i18n.language === 'en' && configuration.MainTitle}
{i18n.language === 'fr' && configuration.TitleFr}
{i18n.language === 'de' && configuration.TitleDe}
{i18n.language === 'en' && configuration.Title.en}
{i18n.language === 'fr' && configuration.Title.fr}
{i18n.language === 'de' && configuration.Title.de}
</h3>

<div>
Expand Down
8 changes: 4 additions & 4 deletions web/frontend/src/pages/form/components/AddQuestionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const AddQuestionModal: FC<AddQuestionModalProps> = ({
updateChoice,
} = useQuestionForm(question);
const [language, setLanguage] = useState('en');
const { Title, TitleDe, TitleFr, MaxN, MinN, ChoicesMap, Hint, HintFr, HintDe } = values;
const { Title, MaxN, MinN, ChoicesMap, Hint, HintFr, HintDe } = values;
const [errors, setErrors] = useState([]);
const handleSave = async () => {
try {
Expand Down Expand Up @@ -189,7 +189,7 @@ const AddQuestionModal: FC<AddQuestionModalProps> = ({
</label>
{language === 'en' && (
<input
value={Title}
value={Title.en}
onChange={handleChange()}
name="Title"
type="text"
Expand All @@ -199,7 +199,7 @@ const AddQuestionModal: FC<AddQuestionModalProps> = ({
)}
{language === 'fr' && (
<input
value={TitleFr}
value={Title.fr}
onChange={handleChange()}
name="TitleFr"
type="text"
Expand All @@ -209,7 +209,7 @@ const AddQuestionModal: FC<AddQuestionModalProps> = ({
)}
{language === 'de' && (
<input
value={TitleDe}
value={Title.de}
onChange={handleChange()}
name="TitleDe"
type="text"
Expand Down
Loading

0 comments on commit 348cff1

Please sign in to comment.