Skip to content

Commit 221ffb3

Browse files
christianalfoniCompuIves
authored andcommitted
fix templates (codesandbox#2456)
* fix templates * Update packages/app/src/app/overmind/namespaces/workspace/actions.ts
1 parent 98505d6 commit 221ffb3

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

packages/app/src/app/overmind/effects/api/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
PaymentDetails,
1717
Profile,
1818
UserSandbox,
19+
CustomTemplate,
1920
} from '@codesandbox/common/lib/types';
2021
import { TemplateType } from '@codesandbox/common/lib/templates';
2122
import { client } from 'app/graphql/client';
@@ -394,4 +395,33 @@ export default {
394395
preloadTemplates() {
395396
client.query({ query: LIST_TEMPLATES, variables: { showAll: true } });
396397
},
398+
deleteTemplate(
399+
sandboxId: string,
400+
templateId: string
401+
): Promise<CustomTemplate> {
402+
return api.delete(`/sandboxes/${sandboxId}/templates/${templateId}`);
403+
},
404+
updateTemplate(
405+
sandboxId: string,
406+
template: CustomTemplate
407+
): Promise<CustomTemplate> {
408+
return api
409+
.put<{ template: CustomTemplate }>(
410+
`/sandboxes/${sandboxId}/templates/${template.id}`,
411+
{
412+
template,
413+
}
414+
)
415+
.then(data => data.template);
416+
},
417+
createTemplate(
418+
sandboxId: string,
419+
template: CustomTemplate
420+
): Promise<CustomTemplate> {
421+
return api
422+
.post<{ template: CustomTemplate }>(`/sandboxes/${sandboxId}/templates`, {
423+
template,
424+
})
425+
.then(data => data.template);
426+
},
397427
};

packages/app/src/app/overmind/namespaces/editor/state.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ type State = {
7878
devToolIndex: number;
7979
tabPosition: number;
8080
};
81+
sessionFrozen: boolean;
8182
};
8283

8384
export const state: State = {
@@ -94,6 +95,7 @@ export const state: State = {
9495
currentTabId: null,
9596
tabs: [],
9697
errors: [],
98+
sessionFrozen: true,
9799
corrections: [],
98100
pendingOperations: {},
99101
pendingUserSelections: [],

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Action, AsyncAction } from 'app/overmind';
22
import getTemplate from '@codesandbox/common/lib/templates';
33
import slugify from '@codesandbox/common/lib/utils/slugify';
44
import { withOwnedSandbox } from 'app/overmind/factories';
5+
import { CustomTemplate } from '@codesandbox/common/lib/types';
56

67
export const valueChanged: Action<{
78
field: string;
@@ -194,3 +195,77 @@ export const setWorkspaceHidden: Action<{ hidden: boolean }> = (
194195
) => {
195196
state.workspace.workspaceHidden = hidden;
196197
};
198+
199+
export const deleteTemplate: AsyncAction = async ({
200+
state,
201+
actions,
202+
effects,
203+
}) => {
204+
effects.analytics.track('Template - Removed', { source: 'editor' });
205+
const sandboxId = state.editor.currentId;
206+
const templateId = state.editor.currentSandbox.customTemplate.id;
207+
208+
try {
209+
await effects.api.deleteTemplate(sandboxId, templateId);
210+
const sandbox = state.editor.sandboxes[sandboxId];
211+
212+
sandbox.isFrozen = false;
213+
sandbox.customTemplate = null;
214+
actions.modalClosed();
215+
effects.notificationToast.success('Template Deleted');
216+
} catch (error) {
217+
effects.notificationToast.error('Could not delete custom template');
218+
}
219+
};
220+
221+
export const editTemplate: AsyncAction = async ({
222+
state,
223+
actions,
224+
effects,
225+
}) => {
226+
effects.analytics.track('Template - Edited', { source: 'editor' });
227+
228+
const sandboxId = state.editor.currentId;
229+
const template = state.editor.currentSandbox.customTemplate;
230+
231+
try {
232+
const updatedTemplate = await effects.api.updateTemplate(
233+
sandboxId,
234+
template
235+
);
236+
237+
actions.modalClosed();
238+
state.editor.currentSandbox.customTemplate = updatedTemplate;
239+
effects.notificationToast.success('Templated Edited');
240+
} catch (error) {
241+
effects.notificationToast.error('Could not edit custom template');
242+
}
243+
};
244+
245+
export const addedTemplate: AsyncAction<{
246+
template: CustomTemplate;
247+
}> = async ({ state, actions, effects }, { template }) => {
248+
effects.analytics.track('Template - Created', { source: 'editor' });
249+
250+
const sandboxId = state.editor.currentId;
251+
252+
try {
253+
const newTemplate = await effects.api.createTemplate(sandboxId, template);
254+
const sandbox = state.editor.sandboxes[sandboxId];
255+
sandbox.isFrozen = true;
256+
sandbox.customTemplate = newTemplate;
257+
actions.modalClosed();
258+
effects.notificationToast.success('Templated Created');
259+
} catch (error) {
260+
effects.notificationToast.error(
261+
'Could not create template, please try again later'
262+
);
263+
}
264+
};
265+
266+
export const sessionFreezeOverride: Action<{ frozen: boolean }> = (
267+
{ state },
268+
{ frozen }
269+
) => {
270+
state.editor.sessionFrozen = frozen;
271+
};

0 commit comments

Comments
 (0)