From 54ec737b9402b030b9e1358504213f37b8988fce Mon Sep 17 00:00:00 2001 From: Sanyam Jain Date: Wed, 10 Jan 2024 23:37:20 +0530 Subject: [PATCH] Create File Functionality added (#22) --- .../CreateFile/CreateFile.jsx | 249 +++++++++--------- .../FileComponent/CodeEditor.css | 47 ++++ .../FileComponent/CodeEditor.jsx | 138 +++++----- .../FileComponent/FileComponent.jsx | 73 +++-- .../FileComponent/FileComponentDelete.jsx | 7 +- .../FileComponent/Header.css | 152 +++++++++++ .../FileComponent/Header.jsx | 117 ++++---- .../FolderComponent/FolderComponent.jsx | 2 +- .../ShowItems/ShowItems.jsx | 4 +- .../DashboardComponents/SubBar/SubBar.css | 3 +- .../DashboardComponents/SubBar/SubBar.jsx | 9 +- .../UploadFile/UploadFile.jsx | 3 +- src/pages/DashboardPage/DashboardPage.css | 6 +- src/pages/DashboardPage/DashboardPage.jsx | 6 +- .../fileFoldersActionCreator.js | 4 +- 15 files changed, 528 insertions(+), 292 deletions(-) create mode 100644 src/components/DashboardComponents/FileComponent/Header.css diff --git a/src/components/DashboardComponents/CreateFile/CreateFile.jsx b/src/components/DashboardComponents/CreateFile/CreateFile.jsx index 51ab34c..bbb9073 100644 --- a/src/components/DashboardComponents/CreateFile/CreateFile.jsx +++ b/src/components/DashboardComponents/CreateFile/CreateFile.jsx @@ -1,145 +1,134 @@ - -import {useEffect, useState} from 'react' -import { faTimes } from '@fortawesome/free-solid-svg-icons' -import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' -import { shallowEqual, useSelector,useDispatch } from 'react-redux' -// import { createFolder } from '../../../redux/actionCreators/fileFoldersActionCreator' -import { createFile } from '../../../redux/actionCreators/fileFoldersActionCreator' -import { toast } from 'react-toastify'; -const CreateFile = ({setIsCreateFileOpen}) => { - - - const [fileName, setFileName] = useState('') - const [success, setSuccess] = useState(false) - - const{ userFiles , user, currentFolder, currentFolderData} = useSelector( - (state) => ({ - userFiles : state.filefolders.userFiles, - user : state.auth.user, - currentFolder : state.filefolders.currentFolder, - currentFolderData : state.filefolders.userFolders.find( - (folder)=> folder.docId === state.filefolders.currentFolder), +import { useState, useEffect } from "react"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faTimes } from "@fortawesome/free-solid-svg-icons"; +import { useSelector, shallowEqual, useDispatch } from "react-redux"; +import { toast } from "react-toastify"; + +import { createFile } from "../../../redux/actionCreators/fileFoldersActionCreator"; + +const CreateFile = ({ setIsCreateFileOpen }) => { + const [fileName, setFileName] = useState(""); + const [success, setSuccess] = useState(false); + + const { userFiles, user, currentFolder, currentFolderData } = useSelector( + (state) => ({ + userFiles: state.filefolders.userFiles, + user: state.auth.user, + currentFolder: state.filefolders.currentFolder, + currentFolderData: state.filefolders.userFolders.find( + (folder) => folder.docId === state.filefolders.currentFolder + ), }), shallowEqual - - ); - const dispatch = useDispatch(); - - useEffect(() => { - if(success){ - setIsCreateFileOpen(false) - setFileName('') - setSuccess(false) - } - }, [success]) - - - const checkFileAlreadyPresent = (name,extension) => { - if(!extension){ - name = `${name}.txt` - } - - - const filePresent = userFiles - .filter((file)=> - file.data.parent === currentFolder).find((fldr)=> fldr.data.name === name); - if(filePresent){ - return true; - } - else{ - return false; - } + ); + const dispatch = useDispatch(); + + useEffect(() => { + if (success) { + setFileName(""); + setSuccess(false); + setIsCreateFileOpen(false); } - const handleSubmit = (e) => { - - e.preventDefault() - if(fileName) { - - if(fileName.length > 3 ){ + }, [success]); - let extension =false; - if(fileName.split('.').length > 1){ + const checkFileAlreadyPresent = (name, ext) => { + if (!ext) { + name = name + ".txt"; + } + const filePresent = userFiles + .filter((file) => file.data.parent === currentFolder) + .find((fldr) => fldr.data.name === name); + if (filePresent) { + return true; + } else { + return false; + } + }; + + const handleSubmit = (e) => { + e.preventDefault(); + if (fileName) { + if (fileName.length > 3) { + //check file extension + let extension = false; + if (fileName.split(".").length > 1) { extension = true; - } - if(!checkFileAlreadyPresent(fileName,extension)){ - - - - const data = { - createdAt : new Date(), - name : extension ? fileName : `${fileName}.txt`, - userId : user.uid, - createdBy : user.displayName, - path : currentFolder === "root" ? [] : [ ...currentFolderData?.data.path,currentFolder], - parent : currentFolder , - lastAccessed : null, - updatedAt : new Date(), - extension : extension ? fileName.split('.')[1] : 'txt', - data : '', - url :null - - }; - - - dispatch(createFile(data,setSuccess)) - - } - else{ - toast.error('File already present') - } } - else{ - toast.error('File name must be greater than 3 characters') + if (!checkFileAlreadyPresent(fileName, extension)) { + const data = { + createdAt: new Date(), + name: extension ? fileName : `${fileName}.txt`, + userId: user.uid, + createdBy: user.displayName, + path: + currentFolder === "root" + ? [] + : [...currentFolderData?.data.path, currentFolder], + parent: currentFolder, + lastAccessed: null, + updatedAt: new Date(), + extension: extension ? fileName.split(".")[1] : "txt", + data: "", + url: null, + }; + dispatch(createFile(data, setSuccess)); + } else { + toast.info("File already present"); } - }else{ - toast.error('File name is required') + } else { + toast.info("File name must be at least 3 characters"); } + } else { + toast.info("File name cannot be empty"); } + }; + return (
-
-
-
-

Create File

- -
-
-
-
-
- setFileName(e.target.value)} - /> - - -
- -
- -
-
- +
+
+
+
+
+ setFileName(e.target.value)} + /> +
+ + +
+
- - ) -} + + + ); +}; -export default CreateFile \ No newline at end of file +export default CreateFile; \ No newline at end of file diff --git a/src/components/DashboardComponents/FileComponent/CodeEditor.css b/src/components/DashboardComponents/FileComponent/CodeEditor.css index e69de29..f6b8f13 100644 --- a/src/components/DashboardComponents/FileComponent/CodeEditor.css +++ b/src/components/DashboardComponents/FileComponent/CodeEditor.css @@ -0,0 +1,47 @@ +/* Code Editor Styles */ +.code-editor-row { + margin-right: 0px; + margin-left: 0px; + } + + .code-editor-container { + + + border-radius: 1rem; + box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); + margin-top: 1rem; + } + + .code-input { + border: 1px solid #ced4da; + border-radius: 0.25rem; + padding: 0.5rem; + width: 100%; + } + + .code-output { + margin-top: 1rem; + overflow-x: auto; + } + + /* Syntax Highlighter Styles */ + .code-output pre { + margin: 0; + white-space: pre-wrap; + } + + /* Responsive Styles */ + @media (max-width: 767px) { + .code-editor-container { + padding: 0.5rem; + } + + .code-input { + padding: 0.25rem; + } + + .code-output { + margin-top: 0.5rem; + } + } + \ No newline at end of file diff --git a/src/components/DashboardComponents/FileComponent/CodeEditor.jsx b/src/components/DashboardComponents/FileComponent/CodeEditor.jsx index 56b74bb..53e1456 100644 --- a/src/components/DashboardComponents/FileComponent/CodeEditor.jsx +++ b/src/components/DashboardComponents/FileComponent/CodeEditor.jsx @@ -1,75 +1,75 @@ import { useState } from "react"; - -import {Prism as SyntaxHighlighter} from 'react-syntax-highlighter'; -import {atomDark} from 'react-syntax-highlighter/dist/esm/styles/prism'; +import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; +import { duotoneLight } from "react-syntax-highlighter/dist/esm/styles/prism"; import "./CodeEditor.css"; -const CodeEditor = ({fileName = "index.txt",data,setData}) => { - - - - const codes = { - html : "xml", - php : "php", - js : "javascript", - jsx : "jsx", - txt:"textfile", - xml : "xml", - css : "css", - c : "c", - cpp : "cpp", - java : "java", - py : "python", - json : "json", - sql : "sql", - }; - let languageType = fileName && typeof fileName === 'string' ? codes[fileName.split(".")[1]] : "textfile"; - const handleKeyDown = (evt) => { - let value=content, - selStartPos = evt.currentTarget.selectionStart; - console.log(evt.currentTarget); - if(evt.key === "Tab"){ - value = - value.substring(0, selStartPos) + - " " + - value.substring(selStartPos, value.length); - evt.currentTarget.selectionStart = selStartPos + 3; - evt.currentTarget.selectionEnd = selStartPos + 3; - evt.preventDefault(); - setData(value); - } - } - return ( - -
-
-