Skip to content

Commit df24bac

Browse files
committed
refactor: fetch notifications over graphql query
1 parent 4d4091a commit df24bac

File tree

13 files changed

+71
-33
lines changed

13 files changed

+71
-33
lines changed

.devcontainer/boot.sh

-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ rm -rf .env
2929
echo "
3030
PORT=$WEB_PORT
3131
REACT_APP_GRAPHQL_URL=http://localhost:$BACKEND_PORT/graphql
32-
REACT_APP_NOTIFICATIONS_URL=https://notifications.automatisch.io
3332
" >> .env
3433
cd $CURRENT_DIR
3534

.vscode/settings.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"editor.formatOnSave": true,
3-
"editor.defaultFormatter": "esbenp.prettier-vscode"
3+
"editor.defaultFormatter": "esbenp.prettier-vscode",
4+
"[javascript]": {
5+
"editor.defaultFormatter": "esbenp.prettier-vscode"
6+
}
47
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import axios from '../../helpers/axios-with-proxy';
2+
3+
const NOTIFICATIONS_URL = 'https://notifications.automatisch.io/notifications.json';
4+
5+
const getNotifications = async () => {
6+
try {
7+
const { data: notifications = [] } = await axios.get(NOTIFICATIONS_URL);
8+
9+
return notifications;
10+
} catch (err) {
11+
return [];
12+
}
13+
};
14+
15+
export default getNotifications;

packages/backend/src/graphql/query-resolvers.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ import getExecutions from './queries/get-executions';
1616
import getFlow from './queries/get-flow';
1717
import getFlows from './queries/get-flows';
1818
import getInvoices from './queries/get-invoices.ee';
19+
import getNotifications from './queries/get-notifications';
1920
import getPaddleInfo from './queries/get-paddle-info.ee';
2021
import getPaymentPlans from './queries/get-payment-plans.ee';
2122
import getPermissionCatalog from './queries/get-permission-catalog.ee';
2223
import getRole from './queries/get-role.ee';
2324
import getRoles from './queries/get-roles.ee';
24-
import getSamlAuthProvider from './queries/get-saml-auth-provider.ee';
2525
import getSamlAuthProviderRoleMappings from './queries/get-saml-auth-provider-role-mappings.ee';
26+
import getSamlAuthProvider from './queries/get-saml-auth-provider.ee';
2627
import getStepWithTestExecutions from './queries/get-step-with-test-executions';
2728
import getSubscriptionStatus from './queries/get-subscription-status.ee';
2829
import getTrialStatus from './queries/get-trial-status.ee';
@@ -51,6 +52,7 @@ const queryResolvers = {
5152
getFlow,
5253
getFlows,
5354
getInvoices,
55+
getNotifications,
5456
getPaddleInfo,
5557
getPaymentPlans,
5658
getPermissionCatalog,

packages/backend/src/graphql/schema.graphql

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type Query {
4646
getPermissionCatalog: PermissionCatalog
4747
getRole(id: String!): Role
4848
getRoles: [Role]
49+
getNotifications: [Notification]
4950
getSamlAuthProvider: SamlAuthProvider
5051
getSamlAuthProviderRoleMappings(id: String!): [SamlAuthProvidersRoleMapping]
5152
getSubscriptionStatus: GetSubscriptionStatus
@@ -787,6 +788,13 @@ input UpdateAppAuthClientInput {
787788
active: Boolean
788789
}
789790

791+
type Notification {
792+
name: String
793+
createdAt: String
794+
documentationUrl: String
795+
description: String
796+
}
797+
790798
schema {
791799
query: Query
792800
mutation: Mutation

packages/backend/src/helpers/authentication.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { rule, shield, allow } from 'graphql-shield';
1+
import { allow, rule, shield } from 'graphql-shield';
22
import jwt from 'jsonwebtoken';
3-
import User from '../models/user';
43
import appConfig from '../config/app';
4+
import User from '../models/user';
55

66
const isAuthenticated = rule()(async (_parent, _args, req) => {
77
const token = req.headers['authorization'];
@@ -34,15 +34,16 @@ const authentication = shield(
3434
Query: {
3535
'*': isAuthenticated,
3636
getAutomatischInfo: allow,
37-
listSamlAuthProviders: allow,
38-
healthcheck: allow,
3937
getConfig: allow,
38+
getNotifications: allow,
39+
healthcheck: allow,
40+
listSamlAuthProviders: allow,
4041
},
4142
Mutation: {
4243
'*': isAuthenticated,
43-
registerUser: allow,
4444
forgotPassword: allow,
4545
login: allow,
46+
registerUser: allow,
4647
resetPassword: allow,
4748
},
4849
},

packages/types/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,13 @@ type AppAuthClient = {
448448
formattedAuthDefaults: IJSONObject;
449449
};
450450

451+
type Notification = {
452+
name: string;
453+
createdAt: string;
454+
documentationUrl: string;
455+
description: string;
456+
}
457+
451458
declare module 'axios' {
452459
interface AxiosResponse {
453460
httpError?: IJSONObject;

packages/web/.env-example

-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ PORT=3001
22
REACT_APP_GRAPHQL_URL=http://localhost:3000/graphql
33
# HTTPS=true
44
REACT_APP_BASE_URL=http://localhost:3001
5-
REACT_APP_NOTIFICATIONS_URL=https://notifications.automatisch.io

packages/web/src/config/app.ts

-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ type Config = {
22
[key: string]: string;
33
baseUrl: string;
44
graphqlUrl: string;
5-
notificationsUrl: string;
65
chatwootBaseUrl: string;
76
supportEmailAddress: string;
87
};
98

109
const config: Config = {
1110
baseUrl: process.env.REACT_APP_BASE_URL as string,
1211
graphqlUrl: process.env.REACT_APP_GRAPHQL_URL as string,
13-
notificationsUrl: process.env.REACT_APP_NOTIFICATIONS_URL as string,
1412
chatwootBaseUrl: 'https://app.chatwoot.com',
1513
supportEmailAddress: '[email protected]',
1614
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { gql } from '@apollo/client';
2+
3+
export const GET_NOTIFICATIONS = gql`
4+
query GetNotifications {
5+
getNotifications {
6+
name
7+
createdAt
8+
documentationUrl
9+
description
10+
}
11+
}
12+
`;
+14-20
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
import * as React from 'react';
2-
import appConfig from 'config/app';
1+
import { useQuery } from '@apollo/client';
2+
import type { Notification } from '@automatisch/types';
33

4-
interface INotification {
5-
name: string;
6-
createdAt: string;
7-
documentationUrl: string;
8-
description: string;
4+
import { GET_NOTIFICATIONS } from 'graphql/queries/get-notifications';
5+
6+
type UseNotificationsReturn = {
7+
notifications: Notification[];
8+
loading: boolean;
99
}
1010

11-
export default function useNotifications(): INotification[] {
12-
const [notifications, setNotifications] = React.useState<INotification[]>([]);
11+
export default function useNotifications(): UseNotificationsReturn {
12+
const { data, loading } = useQuery(GET_NOTIFICATIONS);
1313

14-
React.useEffect(() => {
15-
fetch(`${appConfig.notificationsUrl}/notifications.json`)
16-
.then((response) => response.json())
17-
.then((notifications) => {
18-
if (Array.isArray(notifications) && notifications.length) {
19-
setNotifications(notifications);
20-
}
21-
})
22-
.catch(console.error);
23-
}, []);
14+
const notifications = data?.getNotifications || [];
2415

25-
return notifications;
16+
return {
17+
loading,
18+
notifications,
19+
};
2620
}

packages/web/src/hooks/useVersion.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type TVersionInfo = {
1010
};
1111

1212
export default function useVersion(): TVersionInfo {
13-
const notifications = useNotifications();
13+
const { notifications } = useNotifications();
1414
const { data } = useQuery(HEALTHCHECK, { fetchPolicy: 'cache-and-network' });
1515
const version = data?.healthcheck.version;
1616

packages/web/src/pages/Notifications/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface INotification {
1717

1818
export default function Updates(): React.ReactElement {
1919
const formatMessage = useFormatMessage();
20-
const notifications = useNotifications();
20+
const { notifications } = useNotifications();
2121

2222
return (
2323
<Box sx={{ py: 3 }}>

0 commit comments

Comments
 (0)