Skip to content

Commit ea38451

Browse files
committed
fix folder auto-expand on save
1 parent 0497096 commit ea38451

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

client/modules/IDE/actions/files.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function submitFile(formProps, files, parentId, projectId) {
6363
children: []
6464
};
6565
return apiClient
66-
.post(`/projects/${projectId}/files`, postParams)
66+
.post(`/projects/${projectId}/files`, postParams) //
6767
.then((response) => ({
6868
file: response.data.updatedFile,
6969
updatedAt: response.data.project.updatedAt
@@ -93,6 +93,7 @@ export function handleCreateFile(formProps, setSelected = true) {
9393
submitFile(formProps, files, parentId, projectId)
9494
.then((response) => {
9595
const { file, updatedAt } = response;
96+
file.fileType = 'file';
9697
dispatch(createFile(file, parentId));
9798
if (updatedAt) dispatch(setProjectSavedTime(updatedAt));
9899
dispatch(closeNewFileModal());
@@ -153,6 +154,7 @@ export function handleCreateFolder(formProps) {
153154
submitFolder(formProps, files, parentId, projectId)
154155
.then((response) => {
155156
const { file, updatedAt } = response;
157+
file.isFolderClosed = false;
156158
dispatch(createFile(file, parentId));
157159
if (updatedAt) dispatch(setProjectSavedTime(updatedAt));
158160
dispatch(closeNewFolderModal());

client/modules/IDE/reducers/files.js

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export const initialState = () => {
1818
_id: r,
1919
children: [b, a, c],
2020
fileType: 'folder',
21-
content: ''
21+
content: '',
22+
isFolderClosed: false //
2223
},
2324
{
2425
name: 'sketch.js',
@@ -159,8 +160,22 @@ const files = (state, action) => {
159160
return Object.assign({}, file, { blobURL: action.blobURL });
160161
});
161162
case ActionTypes.NEW_PROJECT:
163+
action.files = action.files.map((file) => {
164+
const corrospondingObj = state.find((obj) => obj.id === file.id);
165+
if (corrospondingObj && corrospondingObj.fileType === 'folder') {
166+
file.isFolderClosed = corrospondingObj.isFolderClosed;
167+
}
168+
return file;
169+
});
162170
return setFilePaths(action.files);
163171
case ActionTypes.SET_PROJECT:
172+
action.files = action.files.map((file) => {
173+
const corrospondingObj = state.find((obj) => obj.id === file.id);
174+
if (corrospondingObj && corrospondingObj.fileType === 'folder') {
175+
file.isFolderClosed = corrospondingObj.isFolderClosed;
176+
}
177+
return file;
178+
});
164179
return setFilePaths(action.files);
165180
case ActionTypes.RESET_PROJECT:
166181
return initialState();
@@ -175,19 +190,53 @@ const files = (state, action) => {
175190
parentFile.name === 'root'
176191
? ''
177192
: `${parentFile.filePath}/${parentFile.name}`;
178-
const newState = [
179-
...updateParent(state, action),
180-
{
181-
name: action.name,
182-
id: action.id,
183-
_id: action._id,
184-
content: action.content,
185-
url: action.url,
186-
children: action.children,
187-
fileType: action.fileType || 'file',
188-
filePath
189-
}
190-
];
193+
// const newState = [
194+
// ...updateParent(state, action),
195+
// {
196+
// name: action.name,
197+
// id: action.id,
198+
// _id: action._id,
199+
// content: action.content,
200+
// url: action.url,
201+
// children: action.children,
202+
// fileType: action.fileType || 'file',
203+
// filePath,
204+
// isExpanded: action.isExpanded
205+
// }
206+
// ];
207+
208+
let newState = null;
209+
if (action.fileType === 'folder') {
210+
newState = [
211+
...updateParent(state, action),
212+
{
213+
name: action.name,
214+
id: action.id,
215+
_id: action._id,
216+
content: action.content,
217+
url: action.url,
218+
children: action.children,
219+
fileType: 'folder',
220+
filePath,
221+
isFolderClosed: false
222+
}
223+
];
224+
} else {
225+
newState = [
226+
...updateParent(state, action),
227+
{
228+
name: action.name,
229+
id: action.id,
230+
_id: action._id,
231+
content: action.content,
232+
url: action.url,
233+
children: action.children,
234+
fileType: 'file',
235+
filePath
236+
}
237+
];
238+
}
239+
191240
return newState.map((file) => {
192241
if (file.id === action.parentId) {
193242
file.children = sortedChildrenId(newState, file.children);
@@ -236,14 +285,18 @@ const files = (state, action) => {
236285
case ActionTypes.SHOW_FOLDER_CHILDREN:
237286
return state.map((file) => {
238287
if (file.id === action.id) {
239-
return Object.assign({}, file, { isFolderClosed: false });
288+
return Object.assign({}, file, {
289+
isFolderClosed: false
290+
});
240291
}
241292
return file;
242293
});
243294
case ActionTypes.HIDE_FOLDER_CHILDREN:
244295
return state.map((file) => {
245296
if (file.id === action.id) {
246-
return Object.assign({}, file, { isFolderClosed: true });
297+
return Object.assign({}, file, {
298+
isFolderClosed: true
299+
});
247300
}
248301
return file;
249302
});

client/modules/IDE/reducers/project.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const project = (state, action) => {
3333
name: action.project.name,
3434
updatedAt: action.project.updatedAt,
3535
owner: action.owner,
36-
isSaving: false
36+
isSaving: false,
37+
files: action.project.files
3738
};
3839
case ActionTypes.RESET_PROJECT:
3940
return initialState();

0 commit comments

Comments
 (0)