From 7034bb64db2f387c0e2f4f35e9605ec7335f37df Mon Sep 17 00:00:00 2001 From: JATIN Date: Sun, 16 Mar 2025 15:55:46 +0530 Subject: [PATCH 1/2] Refactor Redux for Files in reducers,actions and components etc --- client/constants.js | 12 - client/modules/IDE/actions/files.js | 96 ++---- client/modules/IDE/actions/ide.js | 9 +- client/modules/IDE/actions/project.js | 14 +- client/modules/IDE/components/FileNode.jsx | 104 ++---- .../IDE/components/FileNode.unit.test.jsx | 112 ++++--- client/modules/IDE/reducers/files.js | 316 ++++++++---------- 7 files changed, 283 insertions(+), 380 deletions(-) diff --git a/client/constants.js b/client/constants.js index fa6010aed1..a50478804b 100644 --- a/client/constants.js +++ b/client/constants.js @@ -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'; @@ -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'; @@ -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'; @@ -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'; @@ -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'; diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 84b76b6f41..9516e0f515 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -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('.'); @@ -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 = { @@ -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(); @@ -129,7 +146,7 @@ export function submitFolder(formProps, files, parentId, projectId) { } const id = objectID().toHexString(); const file = { - type: ActionTypes.CREATE_FILE, + type: 'CREATE_FILE', name: createUniqueName(formProps.name, parentId, files), id, _id: id, @@ -168,17 +185,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(); @@ -192,11 +198,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(dispatch(DeleteFile(id, parentId))); }) .catch((error) => { const { response } = error; @@ -206,37 +208,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); diff --git a/client/modules/IDE/actions/ide.js b/client/modules/IDE/actions/ide.js index 80a43443bc..9e55b37ca4 100644 --- a/client/modules/IDE/actions/ide.js +++ b/client/modules/IDE/actions/ide.js @@ -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 @@ -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(); diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 1d8943336b..e38556f958 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -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, @@ -189,6 +183,8 @@ export function saveProject( }) .catch((error) => { const { response } = error; + console.log(response); + console.log(error); dispatch(endSavingProject()); dispatch(setToastText('Toast.SketchFailedSave')); dispatch(showToast(1500)); diff --git a/client/modules/IDE/components/FileNode.jsx b/client/modules/IDE/components/FileNode.jsx index 3c84d31196..264793b9ca 100644 --- a/client/modules/IDE/components/FileNode.jsx +++ b/client/modules/IDE/components/FileNode.jsx @@ -1,7 +1,7 @@ import PropTypes from 'prop-types'; import classNames from 'classnames'; import React, { useState, useRef } from 'react'; -import { connect } from 'react-redux'; +import { useDispatch, useSelector } from 'react-redux'; import { useTranslation } from 'react-i18next'; import * as IDEActions from '../actions/ide'; @@ -62,40 +62,35 @@ FileName.propTypes = { name: PropTypes.string.isRequired }; -const FileNode = ({ - id, - parentId, - children, - name, - fileType, - isSelectedFile, - isFolderClosed, - setSelectedFile, - deleteFile, - updateFileName, - resetSelectedFile, - newFile, - newFolder, - showFolderChildren, - hideFolderChildren, - canEdit, - openUploadFileModal, - authenticated, - onClickFile -}) => { +const FileNode = ({ id, canEdit, onClickFile }) => { + const dispatch = useDispatch(); + const { t } = useTranslation(); + + const fileNode = + useSelector((state) => state.files.find((file) => file.id === id)) || {}; + const authenticated = useSelector((state) => state.user.authenticated); + + const { + name = '', + parentId = null, + children = [], + fileType = 'file', + isSelectedFile = false, + isFolderClosed = false + } = fileNode; + const [isOptionsOpen, setIsOptionsOpen] = useState(false); const [isEditingName, setIsEditingName] = useState(false); const [isDeleting, setIsDeleting] = useState(false); const [updatedName, setUpdatedName] = useState(name); - const { t } = useTranslation(); const fileNameInput = useRef(null); const fileOptionsRef = useRef(null); const handleFileClick = (event) => { event.stopPropagation(); if (name !== 'root' && !isDeleting) { - setSelectedFile(id); + dispatch(IDEActions.setSelectedFile(id)); } if (onClickFile) { onClickFile(); @@ -122,17 +117,17 @@ const FileNode = ({ }; const handleClickAddFile = () => { - newFile(id); + dispatch(IDEActions.newFile(id)); setTimeout(() => hideFileOptions(), 0); }; const handleClickAddFolder = () => { - newFolder(id); + dispatch(IDEActions.newFolder(id)); setTimeout(() => hideFileOptions(), 0); }; const handleClickUploadFile = () => { - openUploadFileModal(id); + dispatch(IDEActions.openUploadFileModal(id)); setTimeout(hideFileOptions, 0); }; @@ -141,8 +136,8 @@ const FileNode = ({ if (window.confirm(prompt)) { setIsDeleting(true); - resetSelectedFile(id); - setTimeout(() => deleteFile(id, parentId), 100); + dispatch(IDEActions.resetSelectedFile(id)); + setTimeout(() => dispatch(FileActions.deleteFile(id, parentId), 100)); } }; @@ -158,7 +153,7 @@ const FileNode = ({ const saveUpdatedFileName = () => { if (updatedName !== name) { - updateFileName(id, updatedName); + dispatch(FileActions.updateFileName(id, updatedName)); } }; @@ -243,7 +238,7 @@ const FileNode = ({
@@ -354,7 +348,7 @@ const FileNode = ({