Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Historical alert query #176

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 47 additions & 52 deletions src/views/HistoricalAlerts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
isDefined,
isNotDefined,
} from '@togglecorp/fujs';
import {
PartialForm,
useFormObject,
} from '@togglecorp/toggle-form';

import Link from '#components/Link';
import Page from '#components/Page';
Expand All @@ -39,6 +43,7 @@
AlertEnumsAndAllCountryListQueryVariables,
AlertEnumsQuery,
AlertFilter,
AlertInfoFilter,
AlertInformationsQuery,
AlertInformationsQueryVariables,
FilteredAdminListQuery,
Expand All @@ -58,11 +63,8 @@
import i18n from './i18n.json';
import styles from './styles.module.css';

// TODO: Add Historical alert query here

const ALERT_INFORMATIONS = gql`
query AlertInformations(
$order:AlertOrder,
const HISTORICAL_ALERTS = gql`
query historicalAlerts(
$pagination: OffsetPaginationInput,
$filters: AlertFilter,
) {
Expand All @@ -71,7 +73,6 @@
alerts(
pagination: $pagination,
filters: $filters,
order:$order,
) {
limit
offset
Expand Down Expand Up @@ -134,7 +135,7 @@
`;

const ADMIN_LIST = gql`
query FilteredAdminList($filters:Admin1Filter, $pagination: OffsetPaginationInput) {
query FilteredAdminList($filters: Admin1Filter, $pagination: OffsetPaginationInput) {
public {
id
admin1s(filters: $filters, pagination: $pagination) {
Expand All @@ -150,11 +151,11 @@
`;

type AdminOption = NonNullable<NonNullable<NonNullable<FilteredAdminListQuery['public']>['admin1s']>['items']>[number];

type Urgency = NonNullable<AlertEnumsQuery['enums']['AlertInfoUrgency']>[number];
type Severity = NonNullable<AlertEnumsQuery['enums']['AlertInfoSeverity']>[number];
type Certainty = NonNullable<AlertEnumsQuery['enums']['AlertInfoCertainty']>[number];
type Category = NonNullable<AlertEnumsQuery['enums']['AlertInfoCategory']>[number];
type PartialFormFields = PartialForm<AlertInfoFilter>;

type AlertType = NonNullable<NonNullable<NonNullable<AlertInformationsQuery['public']>['alerts']>['items']>[number];
type Admin1 = AlertType['admin1s'][number];
Expand All @@ -168,8 +169,6 @@

const alertKeySelector = (item: AlertType) => item.id;
const PAGE_SIZE = 20;
const ASC = 'ASC';
const DESC = 'DESC';

// eslint-disable-next-line import/prefer-default-export
export function Component() {
Expand All @@ -190,50 +189,41 @@
filter: {},
});

const order = useMemo(() => {
if (isNotDefined(sortState.sorting)) {
return undefined;
}
return {
[sortState.sorting.name]: sortState.sorting.direction === 'asc' ? ASC : DESC,
};
}, [sortState.sorting]);

const variables = useMemo<{ filters: AlertFilter, pagination: OffsetPaginationInput }>(() => ({
pagination: {
offset,
limit,
},
order,
filters: {
urgency: filter.urgency,
severity: filter.severity,
certainty: filter.certainty,
category: filter.category,
DISTINCT: true,
infos: {
urgency: filter.infos?.urgency,

Check failure on line 200 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
severity: filter.infos?.severity,

Check failure on line 201 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
certainty: filter.infos?.certainty,

Check failure on line 202 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
category: filter.infos?.category,

Check failure on line 203 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
},
country: isDefined(filter.country?.pk) ? { pk: filter.country.pk } : undefined,
admin1: filter.admin1,
sent: isDefined(filter.sent) ? {
// TODO: Add start date & end date
range: {
end: filter.sent,
start: filter.sent,
end: filter.sent?.range?.end,
start: filter.sent?.range?.start,
},
} : undefined,
},
}), [
order,
limit,
offset,
filter,
]);

const {
loading: alertInfoLoading,
loading: historicalAlertsLoading,
previousData,
data: alertInfosResponse = previousData,
error: alertInfoError,
data: historicalAlertInfosResponse = previousData,
error: historicalAlertError,
} = useQuery<AlertInformationsQuery, AlertInformationsQueryVariables>(
ALERT_INFORMATIONS,
HISTORICAL_ALERTS,
{
skip: isNotDefined(variables),
variables,
Expand Down Expand Up @@ -280,7 +270,7 @@
{ variables: adminQueryVariables, skip: isNotDefined(filter.country) },
);

const data = alertInfosResponse?.public.alerts;
const data = historicalAlertInfosResponse?.public.alerts;

const columns = useMemo(
() => ([
Expand Down Expand Up @@ -363,6 +353,11 @@
setFilterField(countryId ? { pk: countryId } : undefined, 'country');
}, [setFilterField]);

const setFieldValue = useFormObject<'infos', NonNullable<PartialFormFields>>(
'infos' as const,
setFilterField,

Check failure on line 358 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Argument of type '(...args: EntriesAsList<AlertFilter>) => void' is not assignable to parameter of type '(value: SetValueArg<NonNullable<{ id?: ({ exact?: InputMaybe<string> | undefined; inList?: string[] | null | undefined; isNull?: InputMaybe<boolean> | undefined; } & Pick<IdBaseFilterLookup, never>) | null | undefined; AND?: (... & Pick<...>) | ... 1 more ... | undefined; DISTINCT?: InputMaybe<...> | undefined; NOT?...'.
{},
);
return (
<Page
className={styles.historicalAlerts}
Expand All @@ -387,9 +382,9 @@
</Link>
)}
overlayPending
pending={alertInfoLoading}
errored={isDefined(alertInfoError)}
errorMessage={alertInfoError?.message}
pending={historicalAlertsLoading}
errored={isDefined(historicalAlertError)}
errorMessage={historicalAlertError?.message}
footerActions={isDefined(data) && (
<Pager
activePage={page}
Expand All @@ -403,22 +398,22 @@
<MultiSelectInput
label={strings.filterUrgencyLabel}
placeholder={strings.filterUrgencyPlaceholder}
name="urgency"

Check failure on line 401 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Type '"urgency"' is not assignable to type '"id" | "AND" | "DISTINCT" | "NOT" | "OR"'.
options={alertEnumsResponse?.enums.AlertInfoUrgency}
keySelector={urgencyKeySelector}
labelSelector={labelSelector}
value={rawFilter.urgency}
onChange={setFilterField}
value={rawFilter.infos?.urgency}

Check failure on line 405 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
onChange={setFieldValue}

Check failure on line 406 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Type '(...entries: EntriesAsList<NonNullable<NonNullable<{ id?: ({ exact?: InputMaybe<string> | undefined; inList?: string[] | null | undefined; isNull?: InputMaybe<boolean> | undefined; } & Pick<IdBaseFilterLookup, never>) | null | undefined; AND?: (... & Pick<...>) | ... 1 more ... | undefined; DISTINCT?: InputMaybe<......' is not assignable to type '(newValue: AlertInfoUrgencyEnum[], name: "id" | "AND" | "DISTINCT" | "NOT" | "OR") => void'.
/>
<MultiSelectInput
label={strings.filterSeverityLabel}
placeholder={strings.filterSeverityPlaceholder}
name="severity"

Check failure on line 411 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Type '"severity"' is not assignable to type '"id" | "AND" | "DISTINCT" | "NOT" | "OR"'.
options={alertEnumsResponse?.enums.AlertInfoSeverity}
keySelector={severityKeySelector}
labelSelector={labelSelector}
value={rawFilter.severity}
onChange={setFilterField}
value={rawFilter.infos?.severity}

Check failure on line 415 in src/views/HistoricalAlerts/index.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'infos' does not exist on type 'AlertFilter'.
onChange={setFieldValue}
/>
<MultiSelectInput
label={strings.filterCertaintyLabel}
Expand All @@ -427,8 +422,8 @@
options={alertEnumsResponse?.enums.AlertInfoCertainty}
keySelector={certaintyKeySelector}
labelSelector={labelSelector}
value={rawFilter.certainty}
onChange={setFilterField}
value={rawFilter.infos?.certainty}
onChange={setFieldValue}
/>
<MultiSelectInput
label={strings.filterCategoriesLabel}
Expand All @@ -437,21 +432,21 @@
options={alertEnumsResponse?.enums.AlertInfoCategory}
keySelector={categoryKeySelector}
labelSelector={labelSelector}
value={rawFilter.category}
onChange={setFilterField}
value={rawFilter.infos?.category}
onChange={setFieldValue}
/>
{/* // TODO Add start date and end date filter */}
<DateInput
name="sent"
name="sentStart"
label={strings.filterStartDateFrom}
value={undefined}
onChange={() => { }}
value={filter.sent?.range?.start}
onChange={() => {}}
/>
<DateInput
name="sent"
name="sentEnd"
label={strings.filterStartDateTo}
value={undefined}
onChange={() => { }}
value={filter.sent?.range?.end}
onChange={() => {}}
/>
<SelectInput
label={strings.filterCountriesLabel}
Expand Down Expand Up @@ -479,9 +474,9 @@
>
<SortContext.Provider value={sortState}>
<Table
pending={alertInfoLoading}
pending={historicalAlertsLoading}
filtered={filtered}
errored={isDefined(alertInfoError)}
errored={isDefined(historicalAlertError)}
columns={columns}
keySelector={alertKeySelector}
data={data?.items}
Expand Down