Skip to content

Commit a6733ec

Browse files
committed
AKR(Frontend): Move to new ComboBox implementation [deploy]
1 parent acc6862 commit a6733ec

File tree

8 files changed

+57
-67
lines changed

8 files changed

+57
-67
lines changed

frontend/packages/akr/src/components/clerkTranslator/authorisation/AuthorisationFields.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import dayjs, { Dayjs } from 'dayjs';
22
import { ChangeEvent, useEffect, useState } from 'react';
33
import {
4-
AutocompleteValue,
54
ComboBox,
65
CustomButton,
76
CustomSwitch,
@@ -91,8 +90,8 @@ export const AuthorisationFields = ({
9190
setIsAuthorisationDataChanged(true);
9291
};
9392

94-
const handleBasisChange = ({}, value: AutocompleteValue) => {
95-
const basis = value?.value as AuthorisationBasis;
93+
const handleBasisChange = (value?: string) => {
94+
const basis = value as AuthorisationBasis;
9695
const examinationDate =
9796
basis === AuthorisationBasisEnum.AUT
9897
? authorisation.examinationDate
@@ -108,8 +107,8 @@ export const AuthorisationFields = ({
108107
setIsAuthorisationDataChanged(true);
109108
};
110109

111-
const handleTermBeginDateChange = ({}, value: AutocompleteValue) => {
112-
const termBeginDate = value?.value ? dayjs(value?.value) : undefined;
110+
const handleTermBeginDateChange = (value?: string) => {
111+
const termBeginDate = value ? dayjs(value) : undefined;
113112
const termEndDate = getNewTermEndDate(authorisation.basis, termBeginDate);
114113

115114
setAuthorisation({
@@ -131,10 +130,10 @@ export const AuthorisationFields = ({
131130
: undefined;
132131
};
133132

134-
const handleExaminationDateChange = ({}, value: AutocompleteValue) => {
133+
const handleExaminationDateChange = (value?: string) => {
135134
setAuthorisation({
136135
...authorisation,
137-
examinationDate: value?.value ? dayjs(value?.value) : undefined,
136+
examinationDate: value ? dayjs(value) : undefined,
138137
});
139138
setIsAuthorisationDataChanged(true);
140139
};

frontend/packages/akr/src/components/clerkTranslator/filters/ClerkTranslatorAutocompleteFilters.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useEffect, useState } from 'react';
22
import {
3-
AutocompleteValue,
43
ComboBox,
54
CustomTextField,
65
H3,
@@ -62,9 +61,8 @@ export const ClerkTranslatorAutocompleteFilters = () => {
6261
};
6362

6463
const handleFilterChange =
65-
(filter: keyof ClerkTranslatorFilter) =>
66-
({}, value: AutocompleteValue) => {
67-
dispatch(addClerkTranslatorFilter({ [filter]: value?.value }));
64+
(filter: keyof ClerkTranslatorFilter) => (value?: string) => {
65+
dispatch(addClerkTranslatorFilter({ [filter]: value }));
6866
dispatch(setPage(0));
6967
};
7068
const handleFromLanguageChange = (language?: string) => {

frontend/packages/akr/src/components/clerkTranslator/filters/ClerkTranslatorEmailFilter.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AutocompleteValue, ComboBox, H3 } from 'shared/components';
1+
import { ComboBox, H3 } from 'shared/components';
22
import { TextFieldVariant } from 'shared/enums';
33

44
import { useAppTranslation } from 'configs/i18n';
@@ -19,9 +19,8 @@ export const ClerkTranslatorEmailFilter = () => {
1919
const dispatch = useAppDispatch();
2020

2121
const handleFilterChange =
22-
(filter: keyof ClerkTranslatorFilter) =>
23-
({}, value: AutocompleteValue) => {
24-
dispatch(addClerkTranslatorFilter({ [filter]: value?.value }));
22+
(filter: keyof ClerkTranslatorFilter) => (value?: string) => {
23+
dispatch(addClerkTranslatorFilter({ [filter]: value }));
2524
dispatch(setPage(0));
2625
};
2726

frontend/packages/akr/src/components/clerkTranslator/new/NewTranslatorBasicInformation.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ChangeEvent } from 'react';
2-
import { AutocompleteValue } from 'shared/components';
32

43
import { ClerkTranslatorDetailsFields } from 'components/clerkTranslator/overview/ClerkTranslatorDetailsFields';
54
import { useAppDispatch, useAppSelector } from 'configs/redux';
@@ -27,9 +26,8 @@ export const NewTranslatorBasicInformation = ({
2726
};
2827

2928
const handleComboBoxChange =
30-
(field: keyof ClerkTranslatorBasicInformation) =>
31-
({}, autocompleteValue?: AutocompleteValue) => {
32-
handleFieldChange(field, autocompleteValue?.value);
29+
(field: keyof ClerkTranslatorBasicInformation) => (value?: string) => {
30+
handleFieldChange(field, value);
3331
};
3432

3533
const handleCheckBoxChange =

frontend/packages/akr/src/components/clerkTranslator/overview/ClerkTranslatorDetails.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ChangeEvent, useCallback, useEffect, useState } from 'react';
2-
import { AutocompleteValue } from 'shared/components';
32
import { APIResponseStatus, Severity, Variant } from 'shared/enums';
43
import { useDialog, useToast } from 'shared/hooks';
54
import { StringUtils } from 'shared/utils';
@@ -87,9 +86,8 @@ export const ClerkTranslatorDetails = () => {
8786
};
8887

8988
const handleComboBoxChange =
90-
(field: keyof ClerkTranslatorBasicInformation) =>
91-
({}, autocompleteValue?: AutocompleteValue) => {
92-
handleFieldChange(field, autocompleteValue?.value);
89+
(field: keyof ClerkTranslatorBasicInformation) => (value?: string) => {
90+
handleFieldChange(field, value);
9391
};
9492

9593
const handleCheckBoxChange =

frontend/packages/akr/src/components/clerkTranslator/overview/ClerkTranslatorDetailsFields.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { FormHelperTextProps } from '@mui/material';
22
import { ChangeEvent, useState } from 'react';
33
import {
4-
AutocompleteValue,
54
ComboBox,
65
CustomSwitch,
76
CustomTextField,
@@ -129,7 +128,7 @@ export const ClerkTranslatorDetailsFields = ({
129128
) => (event: ChangeEvent<HTMLTextAreaElement>) => void;
130129
onComboBoxChange: (
131130
field: keyof ClerkTranslatorBasicInformation
132-
) => ({}, autocompleteValue?: AutocompleteValue) => void;
131+
) => (value?: string) => void;
133132
onCheckBoxChange: (
134133
field: keyof ClerkTranslatorBasicInformation
135134
) => (event: ChangeEvent<HTMLInputElement>, checked: boolean) => void;

frontend/packages/akr/src/components/publicTranslator/filters/PublicTranslatorFilters.tsx

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import {
55
KeyboardEvent,
66
SetStateAction,
77
SyntheticEvent,
8+
useCallback,
89
useMemo,
910
useRef,
1011
useState,
1112
} from 'react';
1213
import {
13-
AutocompleteValue,
1414
Caption,
1515
ComboBox,
1616
CustomButton,
@@ -83,7 +83,7 @@ export const PublicTranslatorFilters = ({
8383
fromLang: '',
8484
toLang: '',
8585
name: '',
86-
town: null,
86+
town: '',
8787
};
8888
const debounce = useDebounce(300);
8989

@@ -161,31 +161,18 @@ export const PublicTranslatorFilters = ({
161161
setSearchButtonDisabled(false);
162162
};
163163

164-
const handleComboboxFilterChange =
165-
(filterName: SearchFilter) =>
166-
(
167-
{},
168-
value: AutocompleteValue,
169-
reason:
170-
| 'selectOption'
171-
| 'createOption'
172-
| 'removeOption'
173-
| 'blur'
174-
| 'clear'
175-
) => {
176-
if (reason === 'clear') {
177-
setFilters((prevState) => ({ ...prevState, [filterName]: '' }));
178-
setValues((prevState) => ({ ...prevState, [filterName]: null }));
179-
} else {
180-
setFilters((prevState) => ({
181-
...prevState,
182-
[filterName]: value?.value || '',
183-
}));
184-
setValues((prevState) => ({ ...prevState, [filterName]: value }));
185-
}
186-
dispatch(removePublicTranslatorFilterError(filterName));
187-
setSearchButtonDisabled(false);
188-
};
164+
const handleTownChange = (value?: string) => {
165+
setFilters((prevState) => ({
166+
...prevState,
167+
[SearchFilter.Town]: value || '',
168+
}));
169+
setValues((prevState) => ({
170+
...prevState,
171+
[SearchFilter.Town]: value || '',
172+
}));
173+
dispatch(removePublicTranslatorFilterError(SearchFilter.Town));
174+
setSearchButtonDisabled(false);
175+
};
189176

190177
const onLanguageChange =
191178
(languageField: SearchFilter.FromLang | SearchFilter.ToLang) =>
@@ -240,16 +227,24 @@ export const PublicTranslatorFilters = ({
240227
}
241228
};
242229

243-
const townAsComboboxOption = (publicTown: PublicTown) => {
244-
const country = publicTown.country
245-
? translateCountry(publicTown.country)
246-
: '';
247-
const value = `${publicTown.name}::${publicTown.country ?? ''}`;
248-
const townName = isCurrentLangSv() ? publicTown.nameSv : publicTown.name;
249-
const label = country ? `${townName} (${country})` : townName;
230+
const townAsComboboxOption = useCallback(
231+
(publicTown: PublicTown) => {
232+
const country = publicTown.country
233+
? translateCountry(publicTown.country)
234+
: '';
235+
const value = `${publicTown.name}::${publicTown.country ?? ''}`;
236+
const townName = isCurrentLangSv() ? publicTown.nameSv : publicTown.name;
237+
const label = country ? `${townName} (${country})` : townName;
250238

251-
return { value, label };
252-
};
239+
return { value, label };
240+
},
241+
[translateCountry]
242+
);
243+
244+
const memoizedTownOptions = useMemo(
245+
() => towns.map(townAsComboboxOption),
246+
[townAsComboboxOption, towns]
247+
);
253248

254249
const renderPhoneBottomAppBar = () =>
255250
isPhone &&
@@ -359,13 +354,19 @@ export const PublicTranslatorFilters = ({
359354
<ComboBox
360355
data-testid="public-translator-filters__town-combobox"
361356
{...getComboBoxAttributes(SearchFilter.Town)}
362-
value={values.town ?? null}
357+
value={
358+
values.town
359+
? memoizedTownOptions.filter(
360+
({ value }) => values.town === value
361+
)[0]
362+
: null
363+
}
363364
placeholder={t('town.placeholder')}
364365
label={t('town.placeholder')}
365366
id="filters-town"
366-
values={towns.map(townAsComboboxOption)}
367+
values={memoizedTownOptions}
367368
onKeyUp={handleKeyUp}
368-
onChange={handleComboboxFilterChange(SearchFilter.Town)}
369+
onChange={handleTownChange}
369370
/>
370371
</div>
371372
</div>

frontend/packages/akr/src/interfaces/publicTranslator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { AutocompleteValue } from 'shared/components';
2-
31
import { SearchFilter } from 'enums/app';
42
import { LanguagePair, LanguagePairsDict } from 'interfaces/languagePair';
53
import { WithId } from 'interfaces/with';
@@ -24,7 +22,7 @@ export interface PublicTranslatorFilterValues {
2422
fromLang: string;
2523
toLang: string;
2624
name: string;
27-
town: AutocompleteValue;
25+
town: string;
2826
}
2927

3028
export interface PublicTown {

0 commit comments

Comments
 (0)