Skip to content

Commit

Permalink
Use current month as default value in country seasonal risk month sel…
Browse files Browse the repository at this point in the history
…ector
  • Loading branch information
frozenhelium committed Dec 19, 2024
1 parent 809141d commit 3b4f232
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
8 changes: 8 additions & 0 deletions app/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type components } from '#generated/types';

Check failure on line 1 in app/src/utils/constants.ts

View workflow job for this annotation

GitHub Actions / Lint JS

Run autofix to sort these imports!
import { listToMap } from '@togglecorp/fujs';

export const defaultChartMargin = {
top: 0,
Expand Down Expand Up @@ -199,3 +200,10 @@ type SupportedByOrganizationType = components<'read'>['schemas']['PerSupportedBy
export const NATIONAL_SOCIETY = 3 satisfies SupportedByOrganizationType;

export const MAX_PAGE_LIMIT = 9999;

export const monthKeyList = Array.from(Array(12).keys());
export const multiMonthSelectDefaultValue = listToMap(
monthKeyList,
(key) => key,
() => false,
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useMemo } from 'react';
import {
Fragment,
useMemo,
} from 'react';
import { TextOutput } from '@ifrc-go/ui';
import { useTranslation } from '@ifrc-go/ui/hooks';
import { resolveToComponent } from '@ifrc-go/ui/utils';
Expand All @@ -13,21 +16,25 @@ function CountryRiskSourcesOutput() {
const riskByMonthSources = useMemo(
() => [
{
key: 'inform',
link: 'https://drmkc.jrc.ec.europa.eu/inform-index/INFORM-Risk',
label: strings.inform,
description: strings.sourceINFORM,
},
{
key: 'undrr',
link: 'https://www.undrr.org/',
label: strings.undrr,
description: strings.sourceUNDRR,
},
{
key: 'idmc',
link: 'https://www.internal-displacement.org/',
label: strings.idmc,
description: strings.sourceIDMC,
},
{
key: 'ipc',
link: 'https://www.ipcinfo.org/',
label: strings.ipc,
description: strings.sourceIPC,
Expand All @@ -40,8 +47,8 @@ function CountryRiskSourcesOutput() {
<TextOutput
label={strings.source}
value={
riskByMonthSources.map((source) => (
<>
riskByMonthSources.map((source, i) => (
<Fragment key={source.key}>
{resolveToComponent(
source.description,
{
Expand All @@ -57,8 +64,8 @@ function CountryRiskSourcesOutput() {
),
},
)}
<br />
</>
{i < (riskByMonthSources.length - 1) && <br />}
</Fragment>
))
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import type { SetValueArg } from '@togglecorp/toggle-form';

import i18n from './i18n.json';
import styles from './styles.module.css';

const keyList = Array.from(Array(12).keys());
const defaultValue = listToMap(
keyList,
(key) => key,
() => false,
);
import { monthKeyList, multiMonthSelectDefaultValue } from '#utils/constants';

Check warning on line 17 in app/src/views/CountryProfileRiskWatch/MultiMonthSelectInput/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

Imports must be broken into multiple lines if there are more than 1 elements

Check failure on line 17 in app/src/views/CountryProfileRiskWatch/MultiMonthSelectInput/index.tsx

View workflow job for this annotation

GitHub Actions / Lint JS

`#utils/constants` import should occur before import of `./i18n.json`

interface Props<NAME> {
className?: string;
Expand Down Expand Up @@ -87,7 +81,7 @@ function MultiMonthSelectInput<NAME>(props: Props<NAME>) {
) {
// Selecting only single value
return {
...defaultValue,
...multiMonthSelectDefaultValue,
[month]: true,
};
}
Expand Down Expand Up @@ -127,7 +121,7 @@ function MultiMonthSelectInput<NAME>(props: Props<NAME>) {
return (
<div className={_cs(styles.multiMonthSelectInput, className)}>
<div className={styles.monthList}>
{keyList.map(
{monthKeyList.map(
(key) => {
const date = new Date();
date.setDate(1);
Expand Down
10 changes: 9 additions & 1 deletion app/src/views/CountryProfileRiskWatch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import RiskImminentEvents, { type ImminentEventSource } from '#components/domain
import Link from '#components/Link';
import WikiLink from '#components/WikiLink';
import useInputState from '#hooks/useInputState';
import { multiMonthSelectDefaultValue } from '#utils/constants';
import type { CountryOutletContext } from '#utils/outletContext';
import { useRiskRequest } from '#utils/restRequest';

Expand All @@ -29,6 +30,10 @@ import RiskTable from './RiskTable';
import i18n from './i18n.json';
import styles from './styles.module.css';

function getCurrentMonth() {
return new Date().getMonth();
}

/** @knipignore */
// eslint-disable-next-line import/prefer-default-export
export function Component() {
Expand All @@ -38,7 +43,10 @@ export function Component() {
const [
selectedMonths,
setSelectedMonths,
] = useInputState<Record<number, boolean> | undefined>({ 0: true });
] = useInputState<(typeof multiMonthSelectDefaultValue) | undefined>({
...multiMonthSelectDefaultValue,
[getCurrentMonth()]: true,
});

const {
pending: pendingCountryRiskResponse,
Expand Down

0 comments on commit 3b4f232

Please sign in to comment.