Skip to content

Commit 5a051f6

Browse files
committed
fix: 🐛 country selection works
1 parent e316f68 commit 5a051f6

4 files changed

Lines changed: 56 additions & 42 deletions

File tree

frontend/src/components/forms/choices-question-editor.tsx

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,28 +94,26 @@ export const ChoicesQuestionEditor = ({
9494
const choicesText = question.configuration.choices.join(separator);
9595

9696
return (
97-
<>
98-
<FlexRowContainer>
99-
<FlexRowColumnContainer>
100-
<TextInput
101-
type={TextInputType.Area}
102-
title="Options (one per line)"
103-
placeholder="no options"
104-
value={choicesText}
105-
onChange={handleChoicesUpdate}
106-
/>
107-
</FlexRowColumnContainer>
108-
<Spacer />
109-
<FlexRowColumnContainer>
110-
<Checkboxes
111-
values={appearanceOptions}
112-
selected={selectedAppearanceOptions}
113-
onChange={handleAppearanceChange}
114-
title="Appearance"
115-
radio
116-
/>
117-
</FlexRowColumnContainer>
118-
</FlexRowContainer>
119-
</>
97+
<FlexRowContainer>
98+
<FlexRowColumnContainer>
99+
<TextInput
100+
type={TextInputType.Area}
101+
title="Options (one per line)"
102+
placeholder="no options"
103+
value={choicesText}
104+
onChange={handleChoicesUpdate}
105+
/>
106+
</FlexRowColumnContainer>
107+
<Spacer />
108+
<FlexRowColumnContainer>
109+
<Checkboxes
110+
values={appearanceOptions}
111+
selected={selectedAppearanceOptions}
112+
onChange={handleAppearanceChange}
113+
title="Appearance"
114+
radio
115+
/>
116+
</FlexRowColumnContainer>
117+
</FlexRowContainer>
120118
);
121119
};

frontend/src/components/forms/country-question-editor.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as React from "react";
2-
import { countryNames } from "./country-question";
2+
import countries from "country-json/src/country-by-abbreviation.json";
3+
4+
const countryNames = countries.map((country) => country.country);
35

46
/**
57
* A pseud-editor for country questions.

frontend/src/components/forms/country-question.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ import type {
44
CountryQuestionConfigurationDTO,
55
QuestionDTO,
66
} from "../../api/types/dto";
7-
import { Autocomplete, TextField } from "@mui/material";
7+
import { Autocomplete, Box, TextField } from "@mui/material";
88
import { FormField } from "../base/form-field";
99
import { useCallback } from "react";
1010

1111
/**
1212
* A list of countries known to tilt.
1313
*/
14-
export const countryNames = countries.map(({ country }) => country).sort();
1514

1615
interface ICountryQuestionProps {
1716
question: QuestionDTO<CountryQuestionConfigurationDTO>;
1817
valueInput: string;
19-
onChange: (event: React.SyntheticEvent, value: string) => any;
18+
onChange: (value: string) => any;
2019
isDisabled?: boolean;
2120
}
2221

@@ -30,25 +29,37 @@ export const CountryQuestion = ({
3029
question,
3130
isDisabled,
3231
}: ICountryQuestionProps) => {
33-
const handleChange = useCallback(
34-
(event: React.SyntheticEvent, value: string | null) =>
35-
onChange(event, value!),
36-
[onChange],
37-
);
38-
3932
return (
4033
<div>
4134
<FormField title={question.title} mandatory={question.mandatory}>
4235
<Autocomplete
4336
disablePortal
44-
options={countryNames}
37+
options={countries}
38+
getOptionLabel={(option) => option.country}
4539
fullWidth
40+
autoHighlight
41+
onChange={(e, v) => onChange(v?.country ?? "")}
4642
renderInput={(params) => (
4743
<TextField {...params} label={question.description} />
4844
)}
45+
value={countries.find((c) => c.country === valueInput) ?? null}
4946
disabled={isDisabled}
50-
onChange={handleChange}
51-
value={valueInput}
47+
renderOption={(props, option) => (
48+
<Box
49+
component="li"
50+
sx={{ "& > img": { mr: 2, flexShrink: 0 } }}
51+
{...props}
52+
>
53+
<img
54+
loading="lazy"
55+
width="20"
56+
srcSet={`https://flagcdn.com/w40/${option.abbreviation.toLowerCase()}.png 2x`}
57+
src={`https://flagcdn.com/w20/${option.abbreviation.toLowerCase()}.png`}
58+
alt=""
59+
/>
60+
{option.country}
61+
</Box>
62+
)}
5263
/>
5364
</FormField>
5465
</div>

frontend/src/components/forms/form.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ export const Form = ({ type }: IFormProps) => {
112112
useState<Nullable<readonly AnswerDTO[]>>(null);
113113

114114
const handleSubmit = useCallback(() => {
115-
setSynchronizedAnswers(
116-
[...Object.entries(state)].map<AnswerDTO>(([questionID, value]) => ({
117-
questionID: Number(questionID),
118-
value,
119-
})),
120-
);
115+
{
116+
console.log(state);
117+
return setSynchronizedAnswers(
118+
[...Object.entries(state)].map<AnswerDTO>(([questionID, value]) => ({
119+
questionID: Number(questionID),
120+
value,
121+
})),
122+
);
123+
}
121124
}, [state]);
122125

123126
const { updateUser } = useLoginContext();

0 commit comments

Comments
 (0)