Skip to content

Commit f7642fe

Browse files
authored
Merge pull request #378 from IFRCGo/fix/filter-typing-issues
Fix typing issues in DREF filters
2 parents 528232d + 654e2d0 commit f7642fe

File tree

17 files changed

+133
-104
lines changed

17 files changed

+133
-104
lines changed

src/components/domain/AppealsOverYearsChart/MonthlyChart/index.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import TimeSeriesChart from '#components/TimeSeriesChart';
88
import Button from '#components/Button';
99
import { getDatesSeparatedByMonths } from '#utils/chart';
1010
import { resolveToComponent } from '#utils/translation';
11+
import { getFormattedDateKey } from '#utils/common';
1112
import useTranslation from '#hooks/useTranslation';
12-
import { formatDate } from '#utils/common';
1313

1414
import PointDetails from '../PointDetails';
1515

1616
import i18n from './i18n.json';
1717
import styles from './styles.module.css';
1818

1919
// FIXME: these must be a constant defined somewhere else
20+
// with satisfies
2021
const APPEAL_TYPE_DREF = 0;
2122
const APPEAL_TYPE_EMERGENCY = 1;
2223

@@ -42,11 +43,6 @@ const dateFormatter = new Intl.DateTimeFormat(
4243
{ month: 'long' },
4344
);
4445

45-
const getFormattedKey = (dateFromProps: string | Date) => {
46-
const date = new Date(dateFromProps);
47-
return formatDate(date, 'yyyy-MM');
48-
};
49-
5046
const currentDate = new Date();
5147

5248
interface Props {
@@ -73,7 +69,7 @@ function MonthlyChart(props: Props) {
7369
);
7470

7571
const [activePointKey, setActivePointKey] = useState<string>(
76-
() => getFormattedKey(dateList[0]),
72+
() => getFormattedDateKey(dateList[0]),
7773
);
7874

7975
const query = {
@@ -120,12 +116,12 @@ function MonthlyChart(props: Props) {
120116

121117
const drefData = listToMap(
122118
monthlyDrefResponse,
123-
(appeal) => getFormattedKey(appeal.timespan),
119+
(appeal) => getFormattedDateKey(appeal.timespan),
124120
);
125121

126122
const emergencyAppealData = listToMap(
127123
monthlyEmergencyAppealResponse,
128-
(appeal) => getFormattedKey(appeal.timespan),
124+
(appeal) => getFormattedDateKey(appeal.timespan),
129125
);
130126

131127
const data = {
@@ -140,7 +136,7 @@ function MonthlyChart(props: Props) {
140136

141137
const dateListWithData = listToMap(
142138
dateList,
143-
(date) => getFormattedKey(date),
139+
(date) => getFormattedDateKey(date),
144140
(date, key) => ({
145141
date,
146142
dref: combinedData?.dref?.[key],
@@ -155,15 +151,15 @@ function MonthlyChart(props: Props) {
155151
);
156152
const chartValueSelector = useCallback(
157153
(dataKey: DATA_KEY, date: Date) => {
158-
const value = combinedData?.[dataKey]?.[getFormattedKey(date)]?.count;
154+
const value = combinedData?.[dataKey]?.[getFormattedDateKey(date)]?.count;
159155
// NOTE: if there are missing values for a given month or year
160156
// less then the current date we assume the value to be 0
161157
// FIXME: This could be done in the aggregation logic of the server itself
162158
if (isNotDefined(value) && date < currentDate) {
163159
return 0;
164160
}
165161

166-
return combinedData?.[dataKey]?.[getFormattedKey(date)]?.count;
162+
return combinedData?.[dataKey]?.[getFormattedDateKey(date)]?.count;
167163
},
168164
[combinedData],
169165
);

src/components/domain/AppealsOverYearsChart/YearlyChart/index.tsx

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import {
55
isNotDefined,
66
isDefined,
77
} from '@togglecorp/fujs';
8-
import { useRequest } from '#utils/restRequest';
98

109
import BlockLoading from '#components/BlockLoading';
1110
import Container from '#components/Container';
1211
import TimeSeriesChart from '#components/TimeSeriesChart';
1312
import Button from '#components/Button';
1413
import Message from '#components/Message';
14+
import { useRequest } from '#utils/restRequest';
1515
import { getDatesSeparatedByYear } from '#utils/chart';
16+
import { getFormattedDateKey } from '#utils/common';
1617
import useTranslation from '#hooks/useTranslation';
17-
import { formatDate } from '#utils/common';
1818

1919
import PointDetails from '../PointDetails';
2020

@@ -31,11 +31,6 @@ const dataKeys: DATA_KEY[] = [
3131
'emergencyAppeal',
3232
];
3333

34-
const getFormattedKey = (dateFromProps: string | Date) => {
35-
const date = new Date(dateFromProps);
36-
return formatDate(date, 'yyyy-MM');
37-
};
38-
3934
// FIXME: use a separate utility
4035
const now = new Date();
4136
const startDate = new Date(now.getFullYear() - 10, 0, 1);
@@ -65,7 +60,7 @@ function YearlyChart(props: Props) {
6560
const strings = useTranslation(i18n);
6661

6762
const [activePointKey, setActivePointKey] = useState<string>(
68-
() => getFormattedKey(dateList[dateList.length - 1]),
63+
() => getFormattedDateKey(dateList[dateList.length - 1]),
6964
);
7065

7166
const queryParams = {
@@ -113,12 +108,12 @@ function YearlyChart(props: Props) {
113108

114109
const drefData = listToMap(
115110
monthlyDrefResponse,
116-
(appeal) => getFormattedKey(appeal.timespan),
111+
(appeal) => getFormattedDateKey(appeal.timespan),
117112
);
118113

119114
const emergencyAppealData = listToMap(
120115
monthlyEmergencyAppealResponse,
121-
(appeal) => getFormattedKey(appeal.timespan),
116+
(appeal) => getFormattedDateKey(appeal.timespan),
122117
);
123118

124119
const data = {
@@ -133,7 +128,7 @@ function YearlyChart(props: Props) {
133128

134129
const dateListWithData = listToMap(
135130
dateList,
136-
(date) => getFormattedKey(date),
131+
(date) => getFormattedDateKey(date),
137132
(date, key) => ({
138133
date,
139134
dref: combinedData?.dref?.[key],
@@ -144,7 +139,7 @@ function YearlyChart(props: Props) {
144139
const activePointData = activePointKey ? dateListWithData[activePointKey] : undefined;
145140
const chartValueSelector = useCallback(
146141
(dataKey: DATA_KEY, date: Date) => (
147-
combinedData?.[dataKey]?.[getFormattedKey(date)]?.count
142+
combinedData?.[dataKey]?.[getFormattedDateKey(date)]?.count
148143
),
149144
[combinedData],
150145
);

src/components/domain/RiskSeasonalMap/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,8 @@ function RiskSeasonalMap(props: Props) {
791791
[data, filters, mappings],
792792
);
793793

794+
const MAX_RISK_SCORE = CATEGORY_RISK_VERY_HIGH;
795+
794796
// NOTE: we need to generate the layerOptions because we cannot use MapState
795797
// The id in the vector tile does not match the id in GO
796798
// We also cannot use promoteId as it is a non-managed mapbox source
@@ -1021,7 +1023,7 @@ function RiskSeasonalMap(props: Props) {
10211023
riskCategory,
10221024
}) => {
10231025
// eslint-disable-next-line max-len
1024-
const percentage = (100 * riskCategory) / (5 * filters.hazardTypes.length);
1026+
const percentage = (100 * riskCategory) / (MAX_RISK_SCORE * filters.hazardTypes.length);
10251027

10261028
if (percentage < 1) {
10271029
return null;

src/utils/common.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { type Language } from '#contexts/language';
1818
import type { GoApiResponse } from '#utils/restRequest';
1919

20-
import { DEFAULT_DATE_FORMAT } from './constants';
20+
import { DEFAULT_DATE_FORMAT, KEY_DATE_FORMAT } from './constants';
2121

2222
export type UnsafeNumberList = Maybe<Maybe<number>[]>;
2323

@@ -678,3 +678,10 @@ export function isValidDate<T extends DateLike>(
678678

679679
return true;
680680
}
681+
682+
export function getFormattedDateKey(dateLike: DateLike) {
683+
const date = formatDate(dateLike, KEY_DATE_FORMAT);
684+
685+
// FIXME: we should throw error in case of undefined
686+
return date ?? '??';
687+
}

src/utils/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type components } from '#generated/types';
22

33
export const DEFAULT_DATE_FORMAT = 'yyyy-MM-dd';
44
export const DEFAULT_PRINT_DATE_FORMAT = 'dd-MM-yyyy';
5+
export const KEY_DATE_FORMAT = 'yyyy-MM';
56

67
// Alert
78

src/views/AccountDetails/ChangePassword/index.tsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useContext, useMemo } from 'react';
1+
import { useCallback, useMemo } from 'react';
22
import {
33
useForm,
44
ObjectSchema,
@@ -21,7 +21,6 @@ import NonFieldError from '#components/NonFieldError';
2121
import useTranslation from '#hooks/useTranslation';
2222
import useAlert from '#hooks/useAlert';
2323
import { GoApiBody, useLazyRequest } from '#utils/restRequest';
24-
import UserContext from '#contexts/user';
2524
import { transformObjectError } from '#utils/restRequest/error';
2625

2726
import i18n from './i18n.json';
@@ -46,7 +45,6 @@ function ChangePasswordModal(props: Props) {
4645

4746
const strings = useTranslation(i18n);
4847
const alert = useAlert();
49-
const { userAuth } = useContext(UserContext);
5048

5149
const getPasswordMatchCondition = useCallback((referenceVal: string | undefined) => {
5250
function passwordMatchCondition(val: string | undefined) {
@@ -64,9 +62,7 @@ function ChangePasswordModal(props: Props) {
6462
{
6563
fields: (value): FormSchemaFields => {
6664
let fields: FormSchemaFields = {
67-
username: {},
68-
token: {},
69-
password: {
65+
old_password: {
7066
required: true,
7167
requiredValidation: requiredStringCondition,
7268
},
@@ -139,14 +135,11 @@ function ChangePasswordModal(props: Props) {
139135
});
140136

141137
const handleConfirmPasswordChange = useCallback((formValues: PartialFormValue) => {
142-
// FIXME: We should not pass username and token to server
143138
const passwordFormValues = {
144139
...formValues,
145-
username: userAuth?.username,
146-
token: userAuth?.token,
147140
};
148141
updatePassword(passwordFormValues as PasswordChangeRequestBody);
149-
}, [userAuth, updatePassword]);
142+
}, [updatePassword]);
150143

151144
const handleSubmitPassword = createSubmitHandler(
152145
validate,
@@ -189,12 +182,12 @@ function ChangePasswordModal(props: Props) {
189182
withFallbackError
190183
/>
191184
<TextInput
192-
name="password"
185+
name="old_password"
193186
type="password"
194187
label={strings.oldPasswordInputLabel}
195-
value={formValue.password}
188+
value={formValue.old_password}
196189
onChange={setFieldValue}
197-
error={fieldError?.password}
190+
error={fieldError?.old_password}
198191
disabled={updatePasswordPending}
199192
withAsterisk
200193
autoFocus

src/views/AccountMyFormsDref/ActiveDrefTable/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function ActiveDrefTable(props: Props) {
6969
offset,
7070
limit,
7171
// FIXME server should accept country
72-
country: filter.country,
73-
type_of_dref: filter.type_of_dref,
72+
country: isDefined(filter.country) ? [filter.country] : undefined,
73+
type_of_dref: isDefined(filter.type_of_dref) ? [filter.type_of_dref] : undefined,
7474
disaster_type: filter.disaster_type,
7575
appeal_code: filter.appeal_code,
7676
},

src/views/AccountMyFormsDref/CompletedDrefTable/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useMemo, useState } from 'react';
2-
import { _cs } from '@togglecorp/fujs';
2+
import { _cs, isDefined } from '@togglecorp/fujs';
33

44
import {
55
createStringColumn,
@@ -59,8 +59,8 @@ function CompletedDrefTable(props: Props) {
5959
offset,
6060
limit,
6161
// FIXME server should accept country
62-
country: filter.country,
63-
type_of_dref: filter.type_of_dref,
62+
country: isDefined(filter.country) ? [filter.country] : undefined,
63+
type_of_dref: isDefined(filter.type_of_dref) ? [filter.type_of_dref] : undefined,
6464
disaster_type: filter.disaster_type,
6565
appeal_code: filter.appeal_code,
6666
},

src/views/AccountMyFormsDref/DrefTableActions/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { type components } from '#generated/types';
2323
import useBooleanState from '#hooks/useBooleanState';
2424
import useTranslation from '#hooks/useTranslation';
2525
import useAlert from '#hooks/useAlert';
26-
import { useLazyRequest } from '#utils/restRequest';
26+
import { GoApiBody, useLazyRequest } from '#utils/restRequest';
2727
import { DREF_STATUS_IN_PROGRESS, DREF_TYPE_IMMINENT, DREF_TYPE_LOAN } from '#utils/constants';
2828

2929
import { exportDrefAllocation } from './drefAllocationExport';
@@ -237,14 +237,17 @@ function DrefTableActions(props: Props) {
237237
},
238238
});
239239

240+
// FIXME: the type should be fixed on the server
241+
type OpsUpdateRequestBody = GoApiBody<'/api/v2/dref-op-update/', 'POST'>;
242+
240243
const {
241244
trigger: createOpsUpdate,
242245
pending: createOpsUpdatePending,
243246
} = useLazyRequest({
244247
method: 'POST',
245248
url: '/api/v2/dref-op-update/',
246249
// FIXME: the type should be fixed on the server
247-
body: (drefId: number) => ({ dref: drefId }),
250+
body: (drefId: number) => ({ dref: drefId } as OpsUpdateRequestBody),
248251
onSuccess: (response) => {
249252
navigate(
250253
'drefOperationalUpdateForm',
@@ -265,14 +268,16 @@ function DrefTableActions(props: Props) {
265268
},
266269
});
267270

271+
// FIXME: the type should be fixed on the server
272+
type FinalReportRequestBody = GoApiBody<'/api/v2/dref-final-report/', 'POST'>;
268273
const {
269274
trigger: createFinalReport,
270275
pending: createFinalReportPending,
271276
} = useLazyRequest({
272277
method: 'POST',
273278
url: '/api/v2/dref-final-report/',
274279
// FIXME: the type should be fixed on the server
275-
body: (drefId: number) => ({ dref: drefId }),
280+
body: (drefId: number) => ({ dref: drefId } as FinalReportRequestBody),
276281
onSuccess: (response) => {
277282
navigate(
278283
'drefFinalReportForm',

src/views/CountryPreparedness/index.tsx

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,37 @@ export function Component() {
210210
areaNum: groupedComponentList[0].area.area_num,
211211
title: groupedComponentList[0].area.title,
212212
value: getAverage(
213-
groupedComponentList.map((component) => component.rating?.value),
213+
groupedComponentList.map(
214+
(component) => (
215+
isDefined(component.rating)
216+
? component.rating.value
217+
: undefined
218+
),
219+
).filter(isDefined),
214220
),
215221
}),
216222
).filter(isDefined);
217223

218224
const averageRating = getAverage(
219-
filteredComponents.map((component) => component.rating?.value),
225+
filteredComponents.map(
226+
(component) => (
227+
isDefined(component.rating)
228+
? component.rating.value
229+
: undefined
230+
),
231+
).filter(isDefined),
220232
);
221233

222234
const ratingCounts = mapToList(
223235
listToGroupList(
224-
componentList.filter((component) => isDefined(component.rating)),
225-
(component) => component.rating?.value,
236+
componentList.map(
237+
(component) => (
238+
isDefined(component.rating)
239+
? { ...component, rating: component.rating }
240+
: undefined
241+
),
242+
).filter(isDefined),
243+
(component) => component.rating.value,
226244
),
227245
(ratingList) => ({
228246
id: ratingList[0].rating?.id,
@@ -540,7 +558,7 @@ export function Component() {
540558
</Heading>
541559
<ProgressBar
542560
className={styles.progressBar}
543-
value={priorityComponent.rating?.value}
561+
value={priorityComponent.rating?.value ?? 0}
544562
totalValue={5}
545563
/>
546564
</div>
@@ -568,7 +586,7 @@ export function Component() {
568586
})}
569587
</Heading>
570588
<ProgressBar
571-
value={component.rating?.value}
589+
value={component.rating?.value ?? 0}
572590
totalValue={5}
573591
title={(
574592
isDefined(component.rating)

0 commit comments

Comments
 (0)