Skip to content

Refactor Redux for Files in reducers,actions and components etc #3398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 0 additions & 12 deletions client/constants.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// TODO Organize this file by reducer type, to break this apart into
// multiple files
export const UPDATE_FILE_CONTENT = 'UPDATE_FILE_CONTENT';
export const TOGGLE_SKETCH = 'TOGGLE_SKETCH';
export const START_SKETCH = 'START_SKETCH';
export const STOP_SKETCH = 'STOP_SKETCH';
Expand All @@ -27,10 +26,7 @@ export const RENAME_PROJECT = 'RENAME_PROJECT';

export const PROJECT_SAVE_SUCCESS = 'PROJECT_SAVE_SUCCESS';
export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL';
export const NEW_PROJECT = 'NEW_PROJECT';
export const RESET_PROJECT = 'RESET_PROJECT';

export const SET_PROJECT = 'SET_PROJECT';
export const SET_PROJECTS = 'SET_PROJECTS';

export const SET_COLLECTIONS = 'SET_COLLECTIONS';
Expand All @@ -43,11 +39,8 @@ export const EDIT_COLLECTION = 'EDIT_COLLECTION';

export const DELETE_PROJECT = 'DELETE_PROJECT';

export const SET_SELECTED_FILE = 'SET_SELECTED_FILE';
export const SHOW_MODAL = 'SHOW_MODAL';
export const HIDE_MODAL = 'HIDE_MODAL';
export const CREATE_FILE = 'CREATE_FILE';
export const SET_BLOB_URL = 'SET_BLOB_URL';

export const EXPAND_SIDEBAR = 'EXPAND_SIDEBAR';
export const COLLAPSE_SIDEBAR = 'COLLAPSE_SIDEBAR';
Expand All @@ -57,9 +50,6 @@ export const COLLAPSE_CONSOLE = 'COLLAPSE_CONSOLE';

export const TOGGLE_FORCE_DESKTOP = 'TOGGLE_FORCE_DESKTOP';

export const UPDATE_FILE_NAME = 'UPDATE_FILE_NAME';
export const DELETE_FILE = 'DELETE_FILE';

export const SET_AUTOSAVE = 'SET_AUTOSAVE';
export const SET_LINEWRAP = 'SET_LINEWRAP';
export const SET_LINT_WARNING = 'SET_LINT_WARNING';
Expand All @@ -74,8 +64,6 @@ export const OPEN_PROJECT_OPTIONS = 'OPEN_PROJECT_OPTIONS';
export const CLOSE_PROJECT_OPTIONS = 'CLOSE_PROJECT_OPTIONS';
export const SHOW_NEW_FOLDER_MODAL = 'SHOW_NEW_FOLDER_MODAL';
export const CLOSE_NEW_FOLDER_MODAL = 'CLOSE_NEW_FOLDER_MODAL';
export const SHOW_FOLDER_CHILDREN = 'SHOW_FOLDER_CHILDREN';
export const HIDE_FOLDER_CHILDREN = 'HIDE_FOLDER_CHILDREN';
export const OPEN_UPLOAD_FILE_MODAL = 'OPEN_UPLOAD_FILE_MODAL';
export const CLOSE_UPLOAD_FILE_MODAL = 'CLOSE_UPLOAD_FILE_MODAL';

Expand Down
100 changes: 39 additions & 61 deletions client/modules/IDE/actions/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import {
} from './ide';
import { setProjectSavedTime } from './project';
import { createError } from './ide';
import {
updateFileContent,
setBlobURL,
newProject,
setProject,
resetProject,
createFile,
showFolderChildren,
hideFolderChildren,
DeleteFile
} from '../reducers/files';

export {
updateFileContent,
setBlobURL,
newProject,
setProject,
resetProject,
createFile,
showFolderChildren,
hideFolderChildren
};

export function appendToFilename(filename, string) {
const dotIndex = filename.lastIndexOf('.');
Expand Down Expand Up @@ -37,22 +59,6 @@ export function createUniqueName(name, parentId, files) {
return testName;
}

export function updateFileContent(id, content) {
return {
type: ActionTypes.UPDATE_FILE_CONTENT,
id,
content
};
}

export function createFile(file, parentId) {
return {
type: ActionTypes.CREATE_FILE,
...file,
parentId
};
}

export function submitFile(formProps, files, parentId, projectId) {
if (projectId) {
const postParams = {
Expand Down Expand Up @@ -83,6 +89,17 @@ export function submitFile(formProps, files, parentId, projectId) {
});
}

export function updateFileName(id, name) {
return (dispatch) => {
dispatch(setUnsavedChanges(true));
dispatch({
type: ActionTypes.UPDATE_FILE_NAME,
id,
name
});
};
}

export function handleCreateFile(formProps, setSelected = true) {
return (dispatch, getState) => {
const state = getState();
Expand Down Expand Up @@ -129,15 +146,17 @@ export function submitFolder(formProps, files, parentId, projectId) {
}
const id = objectID().toHexString();
const file = {
type: ActionTypes.CREATE_FILE,
name: createUniqueName(formProps.name, parentId, files),
id,
_id: id,
content: '',
// TODO pass parent id from File Tree
fileType: 'folder',
parentId,
children: []
};

// Dispatch local folder creation
return Promise.resolve({
file
});
Expand All @@ -153,7 +172,7 @@ export function handleCreateFolder(formProps) {
submitFolder(formProps, files, parentId, projectId)
.then((response) => {
const { file, updatedAt } = response;
dispatch(createFile(file, parentId));
dispatch(createFile({ ...file, parentId }));
if (updatedAt) dispatch(setProjectSavedTime(updatedAt));
dispatch(closeNewFolderModal());
dispatch(setUnsavedChanges(true));
Expand All @@ -168,17 +187,6 @@ export function handleCreateFolder(formProps) {
};
}

export function updateFileName(id, name) {
return (dispatch) => {
dispatch(setUnsavedChanges(true));
dispatch({
type: ActionTypes.UPDATE_FILE_NAME,
id,
name
});
};
}

export function deleteFile(id, parentId) {
return (dispatch, getState) => {
const state = getState();
Expand All @@ -192,11 +200,7 @@ export function deleteFile(id, parentId) {
.delete(`/projects/${state.project.id}/files/${id}`, deleteConfig)
.then((response) => {
dispatch(setProjectSavedTime(response.data.project.updatedAt));
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
dispatch(DeleteFile(id, parentId));
})
.catch((error) => {
const { response } = error;
Expand All @@ -206,37 +210,11 @@ export function deleteFile(id, parentId) {
});
});
} else {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
dispatch(DeleteFile(id, parentId));
}
};
}

export function showFolderChildren(id) {
return {
type: ActionTypes.SHOW_FOLDER_CHILDREN,
id
};
}

export function hideFolderChildren(id) {
return {
type: ActionTypes.HIDE_FOLDER_CHILDREN,
id
};
}

export function setBlobUrl(file, blobURL) {
return {
type: ActionTypes.SET_BLOB_URL,
id: file.id,
blobURL
};
}

export function getBlobUrl(file) {
if (file.blobUrl) {
blobUtil.revokeObjectURL(file.blobUrl);
Expand Down
9 changes: 2 additions & 7 deletions client/modules/IDE/actions/ide.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as ActionTypes from '../../../constants';
import { clearConsole } from './console';
import { dispatchMessage, MessageTypes } from '../../../utils/dispatcher';
import { setSelectedFile } from '../reducers/files';

export { setSelectedFile };
export function startVisualSketch() {
return {
type: ActionTypes.START_SKETCH
Expand Down Expand Up @@ -45,13 +47,6 @@ export function stopAccessibleOutput() {
};
}

export function setSelectedFile(fileId) {
return {
type: ActionTypes.SET_SELECTED_FILE,
selectedFile: fileId
};
}

export function resetSelectedFile(previousId) {
return (dispatch, getState) => {
const state = getState();
Expand Down
12 changes: 3 additions & 9 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ import {
setPreviousPath
} from './ide';
import { clearState, saveState } from '../../../persistState';
import { setProject } from './files';

export { setProject };

const ROOT_URL = getConfig('API_URL');
const S3_BUCKET_URL_BASE = getConfig('S3_BUCKET_URL_BASE');
const S3_BUCKET = getConfig('S3_BUCKET');

export function setProject(project) {
return {
type: ActionTypes.SET_PROJECT,
project,
files: project.files,
owner: project.user
};
}

export function setProjectName(name) {
return {
type: ActionTypes.SET_PROJECT_NAME,
Expand Down
Loading