refactor: fetch notifications over graphql query

This commit is contained in:
Ali BARIN
2023-08-28 20:44:55 +00:00
parent 4d4091adcc
commit df24bac913
13 changed files with 71 additions and 33 deletions

View File

@@ -2,7 +2,6 @@ type Config = {
[key: string]: string;
baseUrl: string;
graphqlUrl: string;
notificationsUrl: string;
chatwootBaseUrl: string;
supportEmailAddress: string;
};
@@ -10,7 +9,6 @@ type Config = {
const config: Config = {
baseUrl: process.env.REACT_APP_BASE_URL as string,
graphqlUrl: process.env.REACT_APP_GRAPHQL_URL as string,
notificationsUrl: process.env.REACT_APP_NOTIFICATIONS_URL as string,
chatwootBaseUrl: 'https://app.chatwoot.com',
supportEmailAddress: 'support@automatisch.io',
};

View File

@@ -0,0 +1,12 @@
import { gql } from '@apollo/client';
export const GET_NOTIFICATIONS = gql`
query GetNotifications {
getNotifications {
name
createdAt
documentationUrl
description
}
}
`;

View File

@@ -1,26 +1,20 @@
import * as React from 'react';
import appConfig from 'config/app';
import { useQuery } from '@apollo/client';
import type { Notification } from '@automatisch/types';
interface INotification {
name: string;
createdAt: string;
documentationUrl: string;
description: string;
import { GET_NOTIFICATIONS } from 'graphql/queries/get-notifications';
type UseNotificationsReturn = {
notifications: Notification[];
loading: boolean;
}
export default function useNotifications(): INotification[] {
const [notifications, setNotifications] = React.useState<INotification[]>([]);
export default function useNotifications(): UseNotificationsReturn {
const { data, loading } = useQuery(GET_NOTIFICATIONS);
React.useEffect(() => {
fetch(`${appConfig.notificationsUrl}/notifications.json`)
.then((response) => response.json())
.then((notifications) => {
if (Array.isArray(notifications) && notifications.length) {
setNotifications(notifications);
}
})
.catch(console.error);
}, []);
const notifications = data?.getNotifications || [];
return notifications;
return {
loading,
notifications,
};
}

View File

@@ -10,7 +10,7 @@ type TVersionInfo = {
};
export default function useVersion(): TVersionInfo {
const notifications = useNotifications();
const { notifications } = useNotifications();
const { data } = useQuery(HEALTHCHECK, { fetchPolicy: 'cache-and-network' });
const version = data?.healthcheck.version;

View File

@@ -17,7 +17,7 @@ interface INotification {
export default function Updates(): React.ReactElement {
const formatMessage = useFormatMessage();
const notifications = useNotifications();
const { notifications } = useNotifications();
return (
<Box sx={{ py: 3 }}>