Skip to content

Commit

Permalink
Hooking up new Notifications constants query.
Browse files Browse the repository at this point in the history
Notifications are now in same order as old mpdx
  • Loading branch information
dr-bizz committed Nov 14, 2023
1 parent 7343a90 commit 19ee973
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/components/Settings/notifications/GetNotifications.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ query getPreferencesNotifications($accountListId: ID!) {
app
email
notificationType {
id
descriptionTemplate
type
}
task
}
}
}

query getNotificationConstants {
constant {
notificationTranslatedHashes {
id
key
value
}
}
}
45 changes: 31 additions & 14 deletions src/components/Settings/notifications/NotificationsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as yup from 'yup';
import { Formik, FieldArray } from 'formik';
import React, { useState, useEffect, ReactElement } from 'react';
import { useTranslation } from 'react-i18next';
import { Formik, FieldArray } from 'formik';
import * as yup from 'yup';
import { v4 as uuidv4 } from 'uuid';
import { useSnackbar } from 'notistack';
import React, { useState, ReactElement } from 'react';
import { styled } from '@mui/material/styles';
import {
Box,
Expand All @@ -16,12 +17,15 @@ import {
Paper,
} from '@mui/material';
import { Email, Smartphone, Task } from '@mui/icons-material';
import * as Types from '../../../../graphql/types.generated';
import { useAccountListId } from 'src/hooks/useAccountListId';
import {
useGetPreferencesNotificationsQuery,
useGetNotificationConstantsQuery,
} from './GetNotifications.generated';
import { useUpdateNotificationPreferencesMutation } from './UpdateNotifications.generated';
import * as Types from '../../../../graphql/types.generated';
import { SubmitButton } from 'src/components/common/Modal/ActionButtons/ActionButtons';
import { useGetPreferencesNotificationsQuery } from './GetNotifications.generated';
import { NotificationsTableSkeleton } from './NotificationsTableSkeleton';
import { useUpdateNotificationPreferencesMutation } from './UpdateNotifications.generated';

export enum notificationsEnum {
App = 'app',
Expand Down Expand Up @@ -86,6 +90,7 @@ export const NotificationsTable: React.FC = () => {
const { t } = useTranslation();
const accountListId = useAccountListId();
const { enqueueSnackbar } = useSnackbar();
const [notifications, setNotifications] = useState([]);
const [appSelectAll, setAppSelectAll] = useState(false);
const [emailSelectAll, setEmailSelectAll] = useState(false);
const [isSetup, _] = useState(false);
Expand Down Expand Up @@ -117,6 +122,7 @@ export const NotificationsTable: React.FC = () => {
accountListId: accountListId ?? '',
},
});
const { data: notificationConstants } = useGetNotificationConstantsQuery();

const defaultIfInSetup = (
notificationPreference: any,
Expand All @@ -127,21 +133,32 @@ export const NotificationsTable: React.FC = () => {
return notificationPreference[type] || isSetup;
};

// TODO: We need order the notifications like we do on the old MPDX.
// PR is https://github.com/CruGlobal/mpdx_api/pull/2743
const notifications: notificationType =
data?.notificationPreferences?.nodes.reduce((result, notification) => {
useEffect(() => {
const notificationsData = data?.notificationPreferences?.nodes || [];
const notificationsOrder =
notificationConstants?.constant?.notificationTranslatedHashes || [];

if (!notificationsData.length || !notificationsOrder.length) return;

const notifications = notificationsOrder.reduce((result, notification) => {
const notificationPreference = notificationsData.find(
(object) => object.notificationType.id === notification.key,
);
return [
...result,
{
...notification,
app: defaultIfInSetup(notification, 'app'),
email: defaultIfInSetup(notification, 'email'),
task: defaultIfInSetup(notification, 'task'),
id: notificationPreference.id || uuidv4(),
notificationType: notificationPreference.notificationType,
app: defaultIfInSetup(notificationPreference, 'app'),
email: defaultIfInSetup(notificationPreference, 'email'),
task: defaultIfInSetup(notificationPreference, 'task'),
},
];
}, []);

setNotifications(notifications);
}, [data, notificationConstants]);

const selectAll = (
type,
notifications,
Expand Down

0 comments on commit 19ee973

Please sign in to comment.