Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): reusable sms preview component #5173

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/web/src/api/content-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ export async function previewChat({
}) {
return api.post('/v1/content-templates/preview/chat', { content, payload, locale });
}

export async function previewSms({
content,
payload,
locale,
}: {
content?: string | IEmailBlock[];
payload: string;
locale?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❔ question: Do we have a more restricted type for Locale that could be used here?‏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately, we don't :/ might be hard to introduce now without refactoring

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}): Promise<{ content: string }> {
return api.post('/v1/content-templates/preview/sms', { content, payload, locale });
}
1 change: 1 addition & 0 deletions apps/web/src/api/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useWebhookSupportStatus';
export * from './notification-templates';
export * from './useGetLocalesFromContent';
export * from './usePreviewEmail';
export * from './usePreviewSms';
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useCallback } from 'react';
import { useMutation } from '@tanstack/react-query';
import { useNavigate } from 'react-router-dom';
import { ICreateNotificationTemplateDto, INotificationTemplate, StepTypeEnum } from '@novu/shared';
import { StepTypeEnum } from '@novu/shared';
import type { IResponseError, ICreateNotificationTemplateDto, INotificationTemplate } from '@novu/shared';

import { createTemplate } from '../../notification-templates';
import { parseUrl } from '../../../utils/routeUtils';
Expand All @@ -16,7 +17,7 @@ export const useCreateDigestDemoWorkflow = () => {
const { groups, loading: areNotificationGroupLoading } = useNotificationGroup();
const { mutateAsync: createNotificationTemplate, isLoading: isCreating } = useMutation<
INotificationTemplate & { __source?: string },
{ error: string; message: string; statusCode: number },
IResponseError,
{ template: ICreateNotificationTemplateDto; params: { __source?: string } }
>((data) => createTemplate(data.template, data.params), {
onSuccess: (template) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { useMutation, UseMutationOptions, useQueryClient } from '@tanstack/react-query';
import { INotificationTemplate, IUpdateNotificationTemplateDto } from '@novu/shared';
import type { IResponseError, INotificationTemplate, IUpdateNotificationTemplateDto } from '@novu/shared';

import { updateTemplate } from '../../notification-templates';
import { QueryKeys } from '../../query.keys';

export const useUpdateTemplate = (
options: UseMutationOptions<
INotificationTemplate,
{ error: string; message: string; statusCode: number },
IResponseError,
{ id: string; data: Partial<IUpdateNotificationTemplateDto> }
> = {}
) => {
const client = useQueryClient();

const { mutateAsync: updateTemplateMutation, ...rest } = useMutation<
INotificationTemplate,
{ error: string; message: string; statusCode: number },
IResponseError,
{ id: string; data: Partial<IUpdateNotificationTemplateDto> }
>(({ id, data }) => updateTemplate(id, data), {
...options,
Expand Down
38 changes: 14 additions & 24 deletions apps/web/src/api/hooks/useDeleteIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
import { MutationOptions, useMutation, useQueryClient } from '@tanstack/react-query';
import type { IResponseError } from '@novu/shared';

import { deleteIntegration } from '../integration';
import { QueryKeys } from '../query.keys';

export const useDeleteIntegration = (
options: MutationOptions<
{},
{ error: string; message: string; statusCode: number },
{
id: string;
name: string;
}
> = {}
options: MutationOptions<{}, IResponseError, { id: string; name: string }> = {}
) => {
const queryClient = useQueryClient();

const { mutate: deleteIntegrationMutate, ...rest } = useMutation<
{},
{ error: string; message: string; statusCode: number },
const { mutate: deleteIntegrationMutate, ...rest } = useMutation<{}, IResponseError, { id: string; name: string }>(
({ id }) => deleteIntegration(id),
{
id: string;
name: string;
}
>(({ id }) => deleteIntegration(id), {
...options,
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context);
...options,
onSuccess: async (data, variables, context) => {
options?.onSuccess?.(data, variables, context);

await queryClient.refetchQueries({
predicate: ({ queryKey }) =>
queryKey.includes(QueryKeys.integrationsList) || queryKey.includes(QueryKeys.activeIntegrations),
});
},
});
await queryClient.refetchQueries({
predicate: ({ queryKey }) =>
queryKey.includes(QueryKeys.integrationsList) || queryKey.includes(QueryKeys.activeIntegrations),
});
},
}
);

return {
deleteIntegration: deleteIntegrationMutate,
Expand Down
16 changes: 7 additions & 9 deletions apps/web/src/api/hooks/useGetLocalesFromContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { errorMessage } from '@novu/design-system';
import { IEmailBlock } from '@novu/shared';
import type { IResponseError, IEmailBlock } from '@novu/shared';
import { IS_DOCKER_HOSTED } from '@novu/shared-web';
import { useMutation } from '@tanstack/react-query';
import { useCallback } from 'react';

import { getLocalesFromContent } from '../translations';

export interface ILocale {
Expand All @@ -26,14 +27,11 @@ export const useGetLocalesFromContent = () => {
mutateAsync: getLocalesFromContentMutation,
isLoading,
data,
} = useMutation<ILocale[], { error: string; message: string; statusCode: number }, Payload>(
({ content }) => getLocalesFromContent({ content }),
{
onError: (e: any) => {
errorMessage(e.message || 'Unexpected error');
},
}
);
} = useMutation<ILocale[], IResponseError, Payload>(({ content }) => getLocalesFromContent({ content }), {
onError: (e: any) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤓 nitpick: unknown is technically recommended by TypeScript here (introduced first in 4.4) ‏

Suggested change
onError: (e: any) => {
onError: (e: unknown) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be defined tho IResponseError, I will remove that :)

errorMessage(e.message || 'Unexpected error');
},
});

const getLocalesFromContentCallback = useCallback(
async ({ content }: Payload) => {
Expand Down
44 changes: 18 additions & 26 deletions apps/web/src/api/hooks/useMakePrimaryIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useMutation, useQueryClient, UseMutationOptions } from '@tanstack/react-query';
import type { IResponseError } from '@novu/shared';

import { errorMessage } from '../../utils/notifications';
import type { IntegrationEntity } from '../../pages/integrations/types';
Expand All @@ -7,37 +8,28 @@ import { QueryKeys } from '../query.keys';
import { setIntegrationAsPrimary } from '../integration';

export const useMakePrimaryIntegration = (
options: UseMutationOptions<
IntegrationEntity,
{ error: string; message: string; statusCode: number },
{
id: string;
}
> = {}
options: UseMutationOptions<IntegrationEntity, IResponseError, { id: string }> = {}
) => {
const queryClient = useQueryClient();

const { mutate: makePrimaryIntegration, ...rest } = useMutation<
IntegrationEntity,
{ error: string; message: string; statusCode: number },
const { mutate: makePrimaryIntegration, ...rest } = useMutation<IntegrationEntity, IResponseError, { id: string }>(
({ id }) => setIntegrationAsPrimary(id),
{
id: string;
...options,
onSuccess: (integration, variables, context) => {
successMessage(`${integration.name} provider instance is activated and marked as the primary instance`);
queryClient.refetchQueries({
predicate: ({ queryKey }) =>
queryKey.includes(QueryKeys.integrationsList) || queryKey.includes(QueryKeys.activeIntegrations),
});
options?.onSuccess?.(integration, variables, context);
},
onError: (e: any, variables, context) => {
errorMessage(e.message || 'Unexpected error');
options?.onError?.(e, variables, context);
},
}
>(({ id }) => setIntegrationAsPrimary(id), {
...options,
onSuccess: (integration, variables, context) => {
successMessage(`${integration.name} provider instance is activated and marked as the primary instance`);
queryClient.refetchQueries({
predicate: ({ queryKey }) =>
queryKey.includes(QueryKeys.integrationsList) || queryKey.includes(QueryKeys.activeIntegrations),
});
options?.onSuccess?.(integration, variables, context);
},
onError: (e: any, variables, context) => {
errorMessage(e.message || 'Unexpected error');
options?.onError?.(e, variables, context);
},
});
);

return {
makePrimaryIntegration,
Expand Down
8 changes: 3 additions & 5 deletions apps/web/src/api/hooks/usePreviewEmail.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { errorMessage } from '@novu/design-system';
import { IEmailBlock, MessageTemplateContentType } from '@novu/shared';
import type { IResponseError, IEmailBlock, MessageTemplateContentType } from '@novu/shared';
import { IS_DOCKER_HOSTED } from '@novu/shared-web';
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useCallback } from 'react';
Expand All @@ -16,10 +16,8 @@ export type PayloadType = {

export type ResultType = { html: string; subject: string };

type ErrorType = { error: string; message: string; statusCode: number };

export const usePreviewEmail = (options: UseMutationOptions<ResultType, ErrorType, PayloadType> = {}) => {
const { mutateAsync, isLoading } = useMutation<ResultType, ErrorType, PayloadType>(
export const usePreviewEmail = (options: UseMutationOptions<ResultType, IResponseError, PayloadType> = {}) => {
const { mutateAsync, isLoading } = useMutation<ResultType, IResponseError, PayloadType>(
({ content, payload, contentType, layoutId, locale, subject }) =>
previewEmail({ content, payload, contentType, layoutId, locale, subject }),

Expand Down
49 changes: 49 additions & 0 deletions apps/web/src/api/hooks/usePreviewSms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
import { useCallback } from 'react';
import { errorMessage } from '@novu/design-system';
import type { IEmailBlock, IResponseError } from '@novu/shared';
import { IS_DOCKER_HOSTED } from '@novu/shared-web';

import { previewSms } from '../content-templates';

type PayloadType = {
content?: string | IEmailBlock[];
payload: string;
locale?: string;
};

type ResultType = { content: string };

export const usePreviewSms = (options: UseMutationOptions<ResultType, IResponseError, PayloadType> = {}) => {
const { mutateAsync, isLoading } = useMutation<ResultType, IResponseError, PayloadType>(
({ content, payload, locale }) => previewSms({ content, payload, locale }),
{
onError: (e) => {
errorMessage(e.message || 'Unexpected error');
},
onSuccess: (result, variables, context) => {
options?.onSuccess?.(result, variables, context);
},
}
);

const getSmsPreview = useCallback(
({ content, payload, locale }: PayloadType) => {
if (IS_DOCKER_HOSTED) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 suggestion: Could you please add a comment in-code explaining why we do this?‏

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a general pattern we use when something should not be available for the self-hosted users

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it reads logically, it might be good to extract this structure to a single helper function to ensure we apply it consistently and with documentation / comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's have a tech debt ticket for this as we have many places around the code ;)

return;
}

return mutateAsync({
content,
payload,
locale,
});
},
[mutateAsync]
);

return {
getSmsPreview,
isLoading,
};
};
4 changes: 2 additions & 2 deletions apps/web/src/api/hooks/useUpdateIntegration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { IUpdateIntegrationBodyDto } from '@novu/shared';
import type { IResponseError, IUpdateIntegrationBodyDto } from '@novu/shared';

import { errorMessage } from '../../utils/notifications';
import { updateIntegration } from '../integration';
Expand All @@ -12,7 +12,7 @@ export const useUpdateIntegration = (integrationId: string) => {

const { mutateAsync: updateIntegrationMutation, isLoading: isLoadingUpdate } = useMutation<
IntegrationEntity,
{ error: string; message: string; statusCode: number },
IResponseError,
{
id: string;
data: IUpdateIntegrationBodyDto;
Expand Down
12 changes: 5 additions & 7 deletions apps/web/src/components/layout/components/OrganizationSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import * as capitalize from 'lodash.capitalize';
import styled from '@emotion/styled';
import { IOrganizationEntity } from '@novu/shared';
import type { IResponseError, IOrganizationEntity } from '@novu/shared';

import { Select } from '@novu/design-system';
import { addOrganization, switchOrganization } from '../../../api/organization';
Expand All @@ -20,15 +20,13 @@ export default function OrganizationSelect() {

const { isLoading: loadingAddOrganization, mutateAsync: createOrganization } = useMutation<
IOrganizationEntity,
{ error: string; message: string; statusCode: number },
IResponseError,
string
>((name) => addOrganization(name));

const { mutateAsync: changeOrganization } = useMutation<
string,
{ error: string; message: string; statusCode: number },
string
>((id) => switchOrganization(id));
const { mutateAsync: changeOrganization } = useMutation<string, IResponseError, string>((id) =>
switchOrganization(id)
);

const switchOrgCallback = useCallback(
async (organizationId: string | string[] | null) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Handle, Position } from 'react-flow-renderer';

import { Button, colors, shadows, Text, Title, BoltOutlinedGradient, Playground } from '@novu/design-system';

import styled from '@emotion/styled';
import { createStyles, Group, Popover, Stack, useMantineColorScheme } from '@mantine/core';
import { ActorTypeEnum, INotificationTemplate, StepTypeEnum, SystemAvatarIconEnum } from '@novu/shared';
import { ActorTypeEnum, StepTypeEnum, SystemAvatarIconEnum } from '@novu/shared';
import type { IResponseError, INotificationTemplate } from '@novu/shared';
import { useMutation } from '@tanstack/react-query';
import { useEffect, useState } from 'react';
import { useParams } from 'react-router-dom';

import { createTemplate, testTrigger } from '../../../api/notification-templates';
import { useEffectOnce, useNotificationGroup, useTemplates } from '../../../hooks';
import {
Expand Down Expand Up @@ -65,7 +65,7 @@ function TriggerButton({ setOpened }: { setOpened: (value: boolean) => void }) {

const { mutate: createNotificationTemplate, isLoading: createTemplateLoading } = useMutation<
INotificationTemplate & { __source?: string },
{ error: string; message: string; statusCode: number },
IResponseError,
{ template: ICreateNotificationTemplateDto; params: { __source?: string } }
>((data) => createTemplate(data.template, data.params), {
onError: (error) => {
Expand Down
Loading
Loading