Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 111 additions & 4 deletions client/modules/IDE/actions/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function createUniqueName(name, parentId, files) {
index += 1;
existingName = siblingFiles.find((file) => testName === file.name); // eslint-disable-line
}
console.log(testName);
return testName;
}

Expand All @@ -43,14 +44,68 @@ export function createFile(formProps) {
const selectedFile = state.files.find(file => file.isSelectedFile);
const rootFile = state.files.find(file => file.name === 'root');
let parentId;
const relativePath = formProps.name;
const relativePathSplit = relativePath.split('/');
const parentFolders = relativePath.length === 1 ? [] : relativePathSplit.slice(0, -1);
const fileName = relativePathSplit.slice(-1)[0];
if (selectedFile.fileType === 'folder') {
parentId = selectedFile.id;
} else {
parentId = rootFile.id;
}
parentId = parentFolders.reduce((parentFolderId, currentFolderName) => {
const { files } = state;
const parentFolderObject = files.find(file => file.id === parentFolderId);
const childFoldersObject = parentFolderObject.children
.map(childFileId => files.find(file => file.id === childFileId));
const currentFolderObject = childFoldersObject.find(childFile => childFile.name === currentFolderName);
const currentFolderId = currentFolderObject ? currentFolderObject.id : undefined;

if (!currentFolderId) {
if (state.project.id) {
const postParams = {
name: createUniqueName(currentFolderName, parentFolderId, getState().files),
content: '',
children: [],
parentId: parentFolderId,
fileType: 'folder'
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then((response) => {
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data,
parentId: parentFolderId
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
/* return __; */
} else {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(currentFolderName, parentFolderId, getState().files),
/* name: currentFolderName, */
id,
_id: id,
content: '',
// TODO pass parent id from File Tree
parentId: parentFolderId,
fileType: 'folder',
children: []
});
return id;
}
}
return currentFolderId;
}, parentId);
console.log(parentId);
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
name: createUniqueName(fileName, parentId, getState().files),
url: formProps.url,
content: formProps.content || '',
parentId,
Expand All @@ -77,7 +132,7 @@ export function createFile(formProps) {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
name: createUniqueName(fileName, parentId, getState().files),
id,
_id: id,
url: formProps.url,
Expand All @@ -100,14 +155,66 @@ export function createFolder(formProps) {
const selectedFile = state.files.find(file => file.isSelectedFile);
const rootFile = state.files.find(file => file.name === 'root');
let parentId;
const relativePath = formProps.name;
const relativePathSplit = relativePath.split('/');
const parentFolders = relativePath.length === 1 ? [] : relativePathSplit.slice(0, -1);
const fileName = relativePathSplit.slice(-1)[0];
if (selectedFile.fileType === 'folder') {
parentId = selectedFile.id;
} else {
parentId = rootFile.id;
}
parentId = parentFolders.reduce((parentFolderId, currentFolderName) => {
const { files } = state;
const parentFolderObject = files.find(file => file.id === parentFolderId);
const childFoldersObject = parentFolderObject.children
.map(childFileId => files.find(file => file.id === childFileId));
const currentFolderObject = childFoldersObject.find(childFile => childFile.name === currentFolderName);
const currentFolderId = currentFolderObject ? currentFolderObject.id : undefined;

if (!currentFolderId) {
if (state.project.id) {
const postParams = {
name: createUniqueName(currentFolderName, parentFolderId, getState().files),
content: '',
children: [],
parentId: parentFolderId,
fileType: 'folder'
};
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
.then((response) => {
dispatch({
type: ActionTypes.CREATE_FILE,
...response.data,
parentId: parentFolderId
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
}));
/* return __; */
} else {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(currentFolderName, parentFolderId, getState().files),
id,
_id: id,
content: '',
// TODO pass parent id from File Tree
parentId: parentFolderId,
fileType: 'folder',
children: []
});
return id;
}
}
return currentFolderId;
}, parentId);
if (state.project.id) {
const postParams = {
name: createUniqueName(formProps.name, parentId, state.files),
name: createUniqueName(fileName, parentId, getState().files),
content: '',
children: [],
parentId,
Expand All @@ -132,7 +239,7 @@ export function createFolder(formProps) {
const id = objectID().toHexString();
dispatch({
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, state.files),
name: createUniqueName(fileName, parentId, getState().files),
id,
_id: id,
content: '',
Expand Down