-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5143 from novuhq/nv-2995-reusable-email-preview-c…
…omponent Nv 2995 reusable email preview component
- Loading branch information
Showing
52 changed files
with
1,327 additions
and
517 deletions.
There are no files selected for viewing
Submodule .source
updated
from ec6ad1 to 5121f6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
apps/api/src/app/testing/translations/get-locales-from-content.e2e-ee.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { UserSession } from '@novu/testing'; | ||
import { expect } from 'chai'; | ||
|
||
const createTranslationGroup = { | ||
name: 'test', | ||
identifier: 'test', | ||
locales: ['hi_IN', 'en_US'], | ||
}; | ||
|
||
const content = 'Hello {{i18n "test.key1"}}, {{i18n "test.key2"}}, {{i18n "test.key3"}}'; | ||
|
||
describe('Get locales from content - /translations/groups/:identifier/locales/:locale (PATCH)', async () => { | ||
let session: UserSession; | ||
|
||
before(async () => { | ||
session = new UserSession(); | ||
await session.initialize(); | ||
await session.testAgent.put(`/v1/organizations/language`).send({ | ||
locale: createTranslationGroup.locales[0], | ||
}); | ||
|
||
await session.testAgent.post('/v1/translations/groups').send(createTranslationGroup); | ||
}); | ||
|
||
it('should get locales from the content', async () => { | ||
const { body } = await session.testAgent.post('/v1/translations/groups/preview/locales').send({ | ||
content, | ||
}); | ||
|
||
const locales = body.data; | ||
|
||
expect(locales.length).to.equal(2); | ||
expect(locales[0].langIso).to.equal(createTranslationGroup.locales[0]); | ||
expect(locales[1].langIso).to.equal(createTranslationGroup.locales[1]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { errorMessage } from '@novu/design-system'; | ||
import { 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 { | ||
name: string; | ||
officialName: string | null; | ||
numeric: string; | ||
alpha2: string; | ||
alpha3: string; | ||
currencyName: string | null; | ||
currencyAlphabeticCode: string | null; | ||
langName: string; | ||
langIso: string; | ||
} | ||
|
||
type Payload = { | ||
content: string | IEmailBlock[]; | ||
}; | ||
|
||
export const useGetLocalesFromContent = () => { | ||
const { | ||
mutateAsync: getLocalesFromContentMutation, | ||
isLoading, | ||
data, | ||
} = useMutation<ILocale[], { error: string; message: string; statusCode: number }, Payload>( | ||
({ content }) => getLocalesFromContent({ content }), | ||
{ | ||
onError: (e: any) => { | ||
errorMessage(e.message || 'Unexpected error'); | ||
}, | ||
} | ||
); | ||
|
||
const getLocalesFromContentCallback = useCallback( | ||
async ({ content }: Payload) => { | ||
if (IS_DOCKER_HOSTED) { | ||
return; | ||
} | ||
|
||
await getLocalesFromContentMutation({ | ||
content, | ||
}); | ||
}, | ||
[getLocalesFromContentMutation] | ||
); | ||
|
||
return { | ||
getLocalesFromContent: getLocalesFromContentCallback, | ||
isLoading, | ||
data: data || [], | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { errorMessage } from '@novu/design-system'; | ||
import { IEmailBlock, MessageTemplateContentType } from '@novu/shared'; | ||
import { IS_DOCKER_HOSTED } from '@novu/shared-web'; | ||
import { useMutation, UseMutationOptions } from '@tanstack/react-query'; | ||
import { useCallback } from 'react'; | ||
import { previewEmail } from '../content-templates'; | ||
|
||
export type PayloadType = { | ||
content?: string | IEmailBlock[]; | ||
contentType?: MessageTemplateContentType; | ||
payload: string; | ||
subject?: string; | ||
layoutId?: string; | ||
locale?: string; | ||
}; | ||
|
||
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>( | ||
({ content, payload, contentType, layoutId, locale, subject }) => | ||
previewEmail({ content, payload, contentType, layoutId, locale, subject }), | ||
|
||
{ | ||
onError: (e: any) => { | ||
errorMessage(e.message || 'Unexpected error'); | ||
}, | ||
onSuccess: (result, variables, context) => { | ||
options?.onSuccess?.(result, variables, context); | ||
}, | ||
} | ||
); | ||
|
||
const getEmailPreviewCallback = useCallback( | ||
async ({ content, payload, contentType, layoutId, locale, subject }: PayloadType) => { | ||
if (IS_DOCKER_HOSTED) { | ||
return; | ||
} | ||
|
||
await mutateAsync({ | ||
content, | ||
payload, | ||
contentType, | ||
layoutId, | ||
locale, | ||
subject, | ||
}); | ||
}, | ||
[mutateAsync] | ||
); | ||
|
||
return { | ||
getEmailPreview: getEmailPreviewCallback, | ||
isLoading, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { api } from './api.client'; | ||
|
||
export async function getLocalesFromContent({ content }) { | ||
return api.post('/v1/translations/groups/preview/locales', { content }); | ||
} |
Oops, something went wrong.