Skip to content

Commit 953dd0e

Browse files
authored
Make all mutations optimistic (codesandbox#3999)
1 parent d6facf7 commit 953dd0e

File tree

2 files changed

+52
-35
lines changed
  • packages/app/src/app

2 files changed

+52
-35
lines changed

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

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -362,28 +362,39 @@ export const deleteSandbox: AsyncAction<string[]> = async (
362362
) => {
363363
const { user } = state;
364364
if (!user) return;
365+
const oldSandboxes = state.dashboard.sandboxes;
366+
actions.dashboard.deleteSandboxFromState(ids);
367+
365368
try {
366369
await effects.gql.mutations.deleteSandboxes({
367370
sandboxIds: ids,
368371
});
369-
actions.dashboard.deleteSandboxFromState(ids);
370372
} catch (error) {
373+
state.dashboard.sandboxes = { ...oldSandboxes };
371374
effects.notificationToast.error(
372375
'There was a problem deleting your Sandbox'
373376
);
374377
}
375378
};
376379

377380
export const unmakeTemplate: AsyncAction<string[]> = async (
378-
{ effects, actions },
381+
{ effects, actions, state },
379382
ids
380383
) => {
384+
const oldTemplates = {
385+
TEMPLATE_START_PAGE: state.dashboard.sandboxes.TEMPLATE_START_PAGE,
386+
TEMPLATES: state.dashboard.sandboxes.TEMPLATES,
387+
};
388+
actions.dashboard.deleteTemplateFromState(ids);
381389
try {
382-
await effects.gql.mutations.unmakeSandboxesTemplate({
383-
sandboxIds: ids,
384-
});
385-
actions.dashboard.deleteTemplateFromState(ids);
390+
await effects.gql.mutations.unmakeSandboxesTemplate({ sandboxIds: ids });
386391
} catch (error) {
392+
state.dashboard.sandboxes.TEMPLATES = oldTemplates.TEMPLATES
393+
? [...oldTemplates.TEMPLATES]
394+
: null;
395+
state.dashboard.sandboxes.TEMPLATE_START_PAGE = oldTemplates.TEMPLATE_START_PAGE
396+
? [...oldTemplates.TEMPLATE_START_PAGE]
397+
: null;
387398
effects.notificationToast.error(
388399
'There was a problem reverting your template'
389400
);
@@ -420,6 +431,24 @@ export const renameSandboxInState: Action<{
420431
);
421432
};
422433

434+
export const renameFolderInState: Action<{ path: string; newPath: string }> = (
435+
{ state: { dashboard } },
436+
{ path, newPath }
437+
) => {
438+
if (!dashboard.allCollections) return;
439+
dashboard.allCollections = dashboard.allCollections.map(folder => {
440+
if (folder.path === path) {
441+
return {
442+
...folder,
443+
path: newPath,
444+
name,
445+
};
446+
}
447+
448+
return folder;
449+
});
450+
};
451+
423452
export const renameSandbox: AsyncAction<{
424453
id: string;
425454
title: string;
@@ -444,38 +473,24 @@ export const renameSandbox: AsyncAction<{
444473
};
445474

446475
export const renameFolder: AsyncAction<{
447-
name: string;
448476
path: string;
449477
newPath: string;
450-
}> = async ({ state: { dashboard }, effects }, { name, path, newPath }) => {
478+
}> = async ({ state: { dashboard }, effects, actions }, { path, newPath }) => {
451479
if (!dashboard.allCollections) return;
452-
dashboard.allCollections = dashboard.allCollections.map(folder => {
453-
if (folder.path === path) {
454-
return {
455-
...folder,
456-
path: newPath,
457-
name,
458-
};
459-
}
460-
461-
return folder;
480+
actions.dashboard.renameFolderInState({
481+
path,
482+
newPath,
462483
});
484+
463485
try {
464486
await effects.gql.mutations.renameFolder({
465487
newPath,
466488
path,
467489
});
468490
} catch {
469-
dashboard.allCollections = dashboard.allCollections.map(folder => {
470-
if (folder.path === newPath) {
471-
return {
472-
...folder,
473-
path,
474-
name,
475-
};
476-
}
477-
478-
return folder;
491+
actions.dashboard.renameFolderInState({
492+
path: newPath,
493+
newPath: path,
479494
});
480495
effects.notificationToast.error('There was a problem renaming you folder');
481496
}
@@ -485,30 +500,33 @@ export const deleteFolder: AsyncAction<{
485500
path: string;
486501
}> = async ({ state: { dashboard }, effects }, { path }) => {
487502
if (!dashboard.allCollections) return;
503+
const oldCollections = dashboard.allCollections;
504+
dashboard.allCollections = dashboard.allCollections.filter(
505+
folder => folder.path !== path
506+
);
488507
try {
489508
await effects.gql.mutations.deleteFolder({
490509
path,
491510
teamId: dashboard.activeTeam,
492511
});
493-
494-
dashboard.allCollections = dashboard.allCollections.filter(
495-
folder => folder.path !== path
496-
);
497512
} catch {
513+
dashboard.allCollections = oldCollections;
498514
effects.notificationToast.error('There was a problem deleting you folder');
499515
}
500516
};
501517

502518
export const makeTemplate: AsyncAction<string[]> = async (
503-
{ effects, actions },
519+
{ effects, state, actions },
504520
ids
505521
) => {
522+
const oldSandboxes = state.dashboard.sandboxes;
523+
actions.dashboard.deleteSandboxFromState(ids);
506524
try {
507525
await effects.gql.mutations.makeSandboxesTemplate({
508526
sandboxIds: ids,
509527
});
510-
actions.dashboard.deleteSandboxFromState(ids);
511528
} catch (error) {
529+
state.dashboard.sandboxes = { ...oldSandboxes };
512530
effects.notificationToast.error('There was a problem making your template');
513531
}
514532
};

packages/app/src/app/pages/NewDashboard/Components/FolderCard/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export const FolderCard = ({ name, path, ...props }: Props) => {
2121
await actions.dashboard.renameFolder({
2222
path,
2323
newPath: join(dirname(path), newName),
24-
name: newName,
2524
});
2625
setEdit(false);
2726
};

0 commit comments

Comments
 (0)