Skip to content

Commit

Permalink
Refactor notification resoruce to use react-query (#11476)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamakase authored Mar 30, 2022
1 parent 9f21fae commit 6db7a28
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AirbyteRequestService } from "core/request/AirbyteRequestService";
import { NotificationStatus } from "./types";

class NotificationService extends AirbyteRequestService {
get url(): string {
return "notifications";
}

public try(payload: {
notificationType: "slack";
sendOnSuccess: boolean;
sendOnFailure: boolean;
slackConfiguration: {
webhook: string;
};
}): Promise<NotificationStatus> {
return this.fetch<NotificationStatus>(`${this.url}/try`, payload);
}
}

export { NotificationService };
4 changes: 4 additions & 0 deletions airbyte-webapp/src/core/domain/notification/types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface NotificationStatus {
status: string;
message: string;
}
31 changes: 0 additions & 31 deletions airbyte-webapp/src/core/resources/Notifications.ts

This file was deleted.

51 changes: 32 additions & 19 deletions airbyte-webapp/src/hooks/services/useWorkspace.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import { useFetcher } from "rest-hooks";
import { useMutation } from "react-query";

import WorkspaceResource from "core/resources/Workspace";
import NotificationsResource, {
Notifications,
} from "core/resources/Notifications";

import { useAnalyticsService } from "hooks/services/Analytics";
import { useCurrentWorkspace } from "services/workspaces/WorkspacesService";
import { Destination, Source } from "core/domain/connector";
import { Workspace } from "core/domain/workspace/Workspace";
import { NotificationStatus } from "core/domain/notification/types";
import { useConfig } from "config";
import { useDefaultRequestMiddlewares } from "services/useDefaultRequestMiddlewares";
import { useInitService } from "services/useInitService";
import { NotificationService } from "core/domain/notification/NotificationService";

export type WebhookPayload = {
webhook: string;
sendOnSuccess: boolean;
sendOnFailure: boolean;
};

function useGetNotificationService(): NotificationService {
const { apiUrl } = useConfig();

const requestAuthMiddleware = useDefaultRequestMiddlewares();

return useInitService(
() => new NotificationService(apiUrl, requestAuthMiddleware),
[apiUrl, requestAuthMiddleware]
);
}

const useWorkspace = (): {
workspace: Workspace;
updatePreferences: (data: {
Expand All @@ -25,7 +39,7 @@ const useWorkspace = (): {
securityUpdates: boolean;
}) => Promise<Workspace>;
updateWebhook: (data: WebhookPayload) => Promise<Workspace>;
testWebhook: (data: WebhookPayload) => Promise<Notifications>;
testWebhook: (data: WebhookPayload) => Promise<NotificationStatus>;
setInitialSetupConfig: (data: {
email: string;
anonymousDataCollection: boolean;
Expand All @@ -43,8 +57,9 @@ const useWorkspace = (): {
destination: Destination;
}) => Promise<void>;
} => {
const notificationService = useGetNotificationService();
const updateWorkspace = useFetcher(WorkspaceResource.updateShape());
const tryWebhookUrl = useFetcher(NotificationsResource.tryShape());

const workspace = useCurrentWorkspace();

const analyticsService = useAnalyticsService();
Expand Down Expand Up @@ -130,19 +145,6 @@ const useWorkspace = (): {
}
);

const testWebhook = async (data: WebhookPayload) =>
await tryWebhookUrl(
{
notificationType: "slack",
sendOnSuccess: data.sendOnSuccess,
sendOnFailure: data.sendOnFailure,
slackConfiguration: {
webhook: data.webhook,
},
},
{}
);

const updateWebhook = async (data: WebhookPayload) =>
await updateWorkspace(
{},
Expand All @@ -166,13 +168,24 @@ const useWorkspace = (): {
}
);

const tryWebhookUrl = useMutation((data: WebhookPayload) =>
notificationService.try({
notificationType: "slack",
sendOnSuccess: data.sendOnSuccess,
sendOnFailure: data.sendOnFailure,
slackConfiguration: {
webhook: data.webhook,
},
})
);

return {
workspace,
finishOnboarding,
setInitialSetupConfig,
updatePreferences,
updateWebhook,
testWebhook,
testWebhook: tryWebhookUrl.mutateAsync,
sendFeedback,
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ const NotificationPage: React.FC = () => {
successMessage,
} = useAsyncWithTimeout(async (data: WebhookPayload) => updateWebhook(data));

const onTestWebhook = async (data: WebhookPayload) => {
await testWebhook(data);
};

const firstNotification = workspace.notifications?.[0];

const initialValues = useMemo(
Expand All @@ -74,7 +70,7 @@ const NotificationPage: React.FC = () => {
<WebHookForm
webhook={initialValues}
onSubmit={onSubmitWebhook}
onTest={onTestWebhook}
onTest={testWebhook}
errorMessage={errorMessage}
successMessage={successMessage}
/>
Expand Down

0 comments on commit 6db7a28

Please sign in to comment.