Skip to content

Commit 521d9d8

Browse files
arthurdennerSaraVieira
authored andcommitted
feat(dashboard): add option to delete template (codesandbox#3134)
* feat(dashboard): add option to delete template * feat(dashboard): refetch templates query after successful deletion * fix(dashboard): fix source param to track delete template action
1 parent 00b0d0b commit 521d9d8

File tree

2 files changed

+38
-4
lines changed
  • packages/app/src/app

2 files changed

+38
-4
lines changed

packages/app/src/app/overmind/namespaces/dashboard/actions.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ export const createSandboxClicked: AsyncAction<{
6969
sandboxId: string;
7070
}> = ({ actions }, { body, sandboxId }) =>
7171
actions.editor.forkExternalSandbox({ body, sandboxId });
72+
73+
export const deleteTemplate: AsyncAction<{
74+
sandboxId: string;
75+
templateId: string;
76+
}> = async ({ actions, effects }, { sandboxId, templateId }) => {
77+
try {
78+
effects.analytics.track('Template - Removed', { source: 'Context Menu' });
79+
await effects.api.deleteTemplate(sandboxId, templateId);
80+
actions.modalClosed();
81+
effects.notificationToast.success('Template Deleted');
82+
} catch (error) {
83+
effects.notificationToast.error('Could not delete custom template');
84+
}
85+
};

packages/app/src/app/pages/Dashboard/Content/routes/Templates/Mine/index.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { useQuery } from '@apollo/react-hooks';
55

66
import { DelayedAnimation } from 'app/components/DelayedAnimation';
77
import { ContextMenu } from 'app/components/ContextMenu';
8+
import { useOvermind } from 'app/overmind';
89
import history from 'app/utils/history';
910
import track from '@codesandbox/common/lib/utils/analytics';
11+
import theme from '@codesandbox/common/lib/theme';
1012
import { sandboxUrl } from '@codesandbox/common/lib/utils/url-generator';
1113
import CustomTemplate from '@codesandbox/common/lib/components/CustomTemplate';
1214
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
@@ -25,8 +27,13 @@ import { Navigation } from '../Navigation';
2527
type TemplatesProps = RouteComponentProps<{ teamId: string }> & {};
2628

2729
export const Templates = (props: TemplatesProps) => {
30+
const {
31+
actions: {
32+
dashboard: { deleteTemplate },
33+
},
34+
} = useOvermind();
2835
const { teamId } = props.match.params;
29-
const { loading, error, data } = useQuery<
36+
const { loading, error, data, refetch } = useQuery<
3037
ListTemplatesQuery,
3138
ListTemplatesQueryVariables
3239
>(LIST_OWNED_TEMPLATES, {
@@ -84,13 +91,26 @@ export const Templates = (props: TemplatesProps) => {
8491
{sortedTemplates.map((template, i) => (
8592
<ContextMenu
8693
items={[
94+
[
95+
{
96+
title: 'Convert to Sandbox',
97+
action: () => {
98+
track('Template - Removed', { source: 'Context Menu' });
99+
unmakeTemplates([template.sandbox.id], teamId);
100+
return true;
101+
},
102+
},
103+
],
87104
{
88-
title: 'Convert to Sandbox',
105+
title: `Delete Template`,
89106
action: () => {
90-
track('Template - Removed', { source: 'Context Menu' });
91-
unmakeTemplates([template.sandbox.id], teamId);
107+
deleteTemplate({
108+
sandboxId: template.sandbox.id,
109+
templateId: template.id,
110+
}).then(() => refetch());
92111
return true;
93112
},
113+
color: theme.red.darken(0.2)(),
94114
},
95115
]}
96116
key={template.id}

0 commit comments

Comments
 (0)