Skip to content

Commit 0ad895b

Browse files
author
Ives van Hoorne
committed
Fix forking bug where modules are not updated after fork
1 parent 3e13423 commit 0ad895b

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/app/store/entities/sandboxes/actions/files.js

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,14 @@ const renameModule = (id: string, moduleId: string, title: string) => async (
102102
dispatch: Function,
103103
getState: Function,
104104
) => {
105-
const modules = modulesSelector(getState());
105+
let modules = modulesSelector(getState());
106106
const module = modules[moduleId];
107107
const sandboxId = await dispatch(maybeForkSandbox(id));
108108
const isForked = sandboxId !== id;
109109

110+
// Modules have updated after fork
111+
modules = modulesSelector(getState());
112+
110113
const newModule = isForked
111114
? getEquivalentModule(module, values(modules)) || module
112115
: module;
@@ -135,12 +138,14 @@ const renameDirectory = (
135138
directoryId: string,
136139
title: string,
137140
) => async (dispatch: Function, getState: Function) => {
138-
const directories = directoriesSelector(getState());
141+
let directories = directoriesSelector(getState());
139142
const directory = directories[directoryId];
140143
const sandboxId = await dispatch(maybeForkSandbox(id));
141-
142144
const isForked = id !== sandboxId;
143145

146+
// Directories have updated after fork
147+
directories = directoriesSelector(getState());
148+
144149
const newDirectory = isForked
145150
? getEquivalentDirectory(directory, values(directories)) || directory
146151
: directory;
@@ -169,11 +174,14 @@ const moveModuleToDirectory = (
169174
moduleId: string,
170175
directoryShortid: string,
171176
) => async (dispatch: Function, getState: Function) => {
172-
const modules = modulesSelector(getState());
177+
let modules = modulesSelector(getState());
173178
const module = modules[moduleId];
174179
const sandboxId = await dispatch(maybeForkSandbox(id));
175180
const isForked = sandboxId !== id;
176181

182+
// Modules have updated after fork
183+
modules = modulesSelector(getState());
184+
177185
const newModule = isForked
178186
? getEquivalentModule(module, values(modules)) || module
179187
: module;
@@ -202,12 +210,14 @@ const moveDirectoryToDirectory = (
202210
directoryId: string,
203211
parentId: string,
204212
) => async (dispatch: Function, getState: Function) => {
205-
const directories = directoriesSelector(getState());
213+
let directories = directoriesSelector(getState());
206214
const directory = directories[directoryId];
207215
const sandboxId = await dispatch(maybeForkSandbox(id));
208-
209216
const isForked = id !== sandboxId;
210217

218+
// Directories have updated after fork
219+
directories = directoriesSelector(getState());
220+
211221
const newDirectory = isForked
212222
? getEquivalentDirectory(directory, values(directories)) || directory
213223
: directory;
@@ -237,11 +247,14 @@ const deleteModule = (id: string, moduleId: string) => async (
237247
dispatch: Function,
238248
getState: Function,
239249
) => {
240-
const modules = modulesSelector(getState());
250+
let modules = modulesSelector(getState());
241251
const module = modules[moduleId];
242252
const sandboxId = await dispatch(maybeForkSandbox(id));
243253
const isForked = sandboxId !== id;
244254

255+
// Modules have updated after fork
256+
modules = modulesSelector(getState());
257+
245258
const newModule = isForked
246259
? getEquivalentModule(module, values(modules)) || module
247260
: module;
@@ -268,12 +281,14 @@ const deleteDirectory = (id: string, directoryId: string) => async (
268281
dispatch: Function,
269282
getState: Function,
270283
) => {
271-
const directories = directoriesSelector(getState());
284+
let directories = directoriesSelector(getState());
272285
const directory = directories[directoryId];
273286
const sandboxId = await dispatch(maybeForkSandbox(id));
274-
275287
const isForked = id !== sandboxId;
276288

289+
// Directories have updated after fork
290+
directories = directoriesSelector(getState());
291+
277292
const newDirectory = isForked
278293
? getEquivalentDirectory(directory, values(directories)) || directory
279294
: directory;
@@ -362,11 +377,14 @@ const saveModuleCode = (id: string, moduleId: string) => async (
362377
dispatch: Function,
363378
getState: Function,
364379
) => {
365-
const modules = modulesSelector(getState());
380+
let modules = modulesSelector(getState());
366381
const module = modules[moduleId];
367382
const sandboxId = await dispatch(maybeForkSandbox(id));
368383
const isForked = sandboxId !== id;
369384

385+
// Modules have updated after fork
386+
modules = modulesSelector(getState());
387+
370388
const newModule = isForked
371389
? getEquivalentModule(module, values(modules)) || module
372390
: module;

src/app/store/entities/sandboxes/actions/fork.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ describe('equivalent', () => {
1010
expect(getEquivalentModule(module, modules)).toEqual(eqModule);
1111
});
1212

13+
it('finds the equivalent module if order is the other way around', () => {
14+
const module = createModule();
15+
const eqModule = createModule(0, { ...module, id: 'newid' });
16+
const modules = [eqModule, eqModule];
17+
18+
expect(getEquivalentModule(module, modules)).toEqual(eqModule);
19+
});
20+
1321
it('returns undefined when equivalent module not found', () => {
1422
const module = createModule();
1523

0 commit comments

Comments
 (0)