✨(app-impress) can duplicate template
Template with mode "is_public" are not editable, except for the owner. We give the possibility to duplicate a template, so that the user can edit the new one.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export * from './useCreateTemplate';
|
||||
@@ -3,21 +3,14 @@ import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { APIError, errorCauses, fetchAPI } from '@/api';
|
||||
import { KEY_LIST_TEMPLATE, Template } from '@/features/templates';
|
||||
|
||||
type CreateTemplateParam = {
|
||||
title: string;
|
||||
is_public: boolean;
|
||||
};
|
||||
type CreateTemplateParam = Partial<Template>;
|
||||
|
||||
export const createTemplate = async ({
|
||||
title,
|
||||
is_public,
|
||||
}: CreateTemplateParam): Promise<Template> => {
|
||||
export const createTemplate = async (
|
||||
props: CreateTemplateParam,
|
||||
): Promise<Template> => {
|
||||
const response = await fetchAPI(`templates/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
is_public,
|
||||
}),
|
||||
body: JSON.stringify(props),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from './components';
|
||||
export * from './api';
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
import { Box, Text } from '@/components';
|
||||
import { useCreateTemplate } from '@/features/templates/template-create';
|
||||
|
||||
import { useUpdateTemplate } from '../api/useUpdateTemplate';
|
||||
import { Template } from '../types';
|
||||
@@ -29,6 +30,12 @@ export const TemplateTools = ({
|
||||
},
|
||||
});
|
||||
|
||||
const { mutate: duplicateTemplate } = useCreateTemplate({
|
||||
onSuccess: () => {
|
||||
toast(t('Template duplicated successfully'), VariantType.SUCCESS);
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<Box
|
||||
className="m-b mb-t mt-t"
|
||||
@@ -39,18 +46,33 @@ export const TemplateTools = ({
|
||||
<Text as="h2" $align="center">
|
||||
{template.title}
|
||||
</Text>
|
||||
<Button
|
||||
onClick={() => {
|
||||
updateTemplate({
|
||||
id: template.id,
|
||||
css: cssStyle,
|
||||
html,
|
||||
});
|
||||
}}
|
||||
disabled={!template.abilities.partial_update}
|
||||
>
|
||||
{t('Save template')}
|
||||
</Button>
|
||||
<Box $direction="row" $gap="2rem">
|
||||
<Button
|
||||
onClick={() => {
|
||||
duplicateTemplate({
|
||||
title: `${template.title} - ${t('Copy')}`,
|
||||
code_editor: template.code_editor,
|
||||
css: template.css,
|
||||
code: template.code,
|
||||
});
|
||||
}}
|
||||
color="secondary"
|
||||
>
|
||||
{t('Duplicate template')}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
updateTemplate({
|
||||
id: template.id,
|
||||
css: cssStyle,
|
||||
html,
|
||||
});
|
||||
}}
|
||||
disabled={!template.abilities.partial_update}
|
||||
>
|
||||
{t('Save template')}
|
||||
</Button>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -32,4 +32,7 @@ export interface Template {
|
||||
accesses: Access[];
|
||||
code_editor: ProjectData;
|
||||
title: string;
|
||||
is_public: boolean;
|
||||
css: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user