Skip to content

Commit

Permalink
Fix Historical alert query
Browse files Browse the repository at this point in the history
  • Loading branch information
roshni73 committed Nov 27, 2024
1 parent 9c124bb commit 04be9be
Showing 1 changed file with 67 additions and 68 deletions.
135 changes: 67 additions & 68 deletions src/views/HistoricalAlerts/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import {
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 @@ import {
AlertEnumsAndAllCountryListQueryVariables,
AlertEnumsQuery,
AlertFilter,
AlertInfoFilter,
AlertInformationsQuery,
AlertInformationsQueryVariables,
FilteredAdminListQuery,
Expand All @@ -60,9 +65,8 @@ 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 +75,6 @@ const ALERT_INFORMATIONS = gql`
alerts(
pagination: $pagination,
filters: $filters,
order:$order,
) {
limit
offset
Expand Down Expand Up @@ -134,7 +137,7 @@ query AlertEnumsAndAllCountryList {
`;

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 +153,11 @@ query FilteredAdminList($filters:Admin1Filter, $pagination: OffsetPaginationInpu
`;

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 +171,6 @@ const categoryKeySelector = (category: Category) => category.key;

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 +191,43 @@ export function Component() {
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,
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,
},
} : undefined,
},
}), [
order,
limit,
offset,
filter,
]);
const variables = useMemo<{
filters: AlertInfoFilter,
pagination: OffsetPaginationInput }>(() => ({
pagination: {
offset,
limit,
},
filters: {
DISTINCT: true,
infos: {
urgency: filter.infos?.urgency,

Check failure on line 204 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 205 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 206 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 207 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) ? {
range: {
end: filter.sent?.range?.end,
start: filter.sent?.range?.start,
},
} : undefined,
},
}), [
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 +274,7 @@ export function Component() {
{ 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 +357,11 @@ export function Component() {
setFilterField(countryId ? { pk: countryId } : undefined, 'country');
}, [setFilterField]);

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

Check failure on line 362 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 +386,9 @@ export function Component() {
</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 @@ -407,8 +406,8 @@ export function Component() {
options={alertEnumsResponse?.enums.AlertInfoUrgency}
keySelector={urgencyKeySelector}
labelSelector={labelSelector}
value={rawFilter.urgency}
onChange={setFilterField}
value={rawFilter.infos?.urgency}

Check failure on line 409 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 410 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}
Expand All @@ -417,8 +416,8 @@ export function Component() {
options={alertEnumsResponse?.enums.AlertInfoSeverity}
keySelector={severityKeySelector}
labelSelector={labelSelector}
value={rawFilter.severity}
onChange={setFilterField}
value={rawFilter.infos?.severity}

Check failure on line 419 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 +426,8 @@ export function Component() {
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 +436,21 @@ export function Component() {
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 +478,9 @@ export function Component() {
>
<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

0 comments on commit 04be9be

Please sign in to comment.