Skip to content

Commit

Permalink
Merge pull request #23 from kartik-gupta-ij/new_branch
Browse files Browse the repository at this point in the history
Saved Code Feature added
  • Loading branch information
generall authored Apr 4, 2023
2 parents 1d37a67 + 020f990 commit 3501bf6
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 34 deletions.
46 changes: 23 additions & 23 deletions src/components/CodeEditorWindow/Menu/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import PropTypes from "prop-types";
function History({ state, code, handleEditorChange, toggleDrawer }) {

const [viewCode, setViewCode] = React.useState("//Selected Code");
const [currentSavedCodes, setCurrentSavedCodes] = useState(localStorage.getItem("currentSavedCodes") ? JSON.parse(localStorage.getItem("currentSavedCodes")) : []);
const [history, setHistory] = useState(localStorage.getItem("history") ? JSON.parse(localStorage.getItem("history")) : []);


useEffect(() => {
setCurrentSavedCodes(localStorage.getItem("currentSavedCodes") ? JSON.parse(localStorage.getItem("currentSavedCodes")) : []);
setHistory(localStorage.getItem("history") ? JSON.parse(localStorage.getItem("history")) : []);
}, [state])

function formatJSON(val = {}) {
Expand Down Expand Up @@ -77,11 +77,11 @@ function History({ state, code, handleEditorChange, toggleDrawer }) {
color="error"
onClick={
() => {
const index = currentSavedCodes.indexOf(data)
const updateCode = [...currentSavedCodes]
const index = history.indexOf(data)
const updateCode = [...history]
updateCode.splice(index, 1);
localStorage.setItem("currentSavedCodes", JSON.stringify(updateCode));
setCurrentSavedCodes(JSON.parse(localStorage.getItem("currentSavedCodes")))
localStorage.setItem("history", JSON.stringify(updateCode));
setHistory(JSON.parse(localStorage.getItem("history")))
return;
}}
>
Expand All @@ -94,8 +94,8 @@ function History({ state, code, handleEditorChange, toggleDrawer }) {
<SwipeableDrawer
anchor="top"
open={state}
onClose={toggleDrawer(false)}
onOpen={toggleDrawer(true)}
onClose={toggleDrawer("history", false)}
onOpen={toggleDrawer("history", true)}
>
<Box
sx={{
Expand All @@ -116,7 +116,7 @@ function History({ state, code, handleEditorChange, toggleDrawer }) {
History Mode
</Typography>
</Stack>
{currentSavedCodes.length === 0 &&
{history.length === 0 &&
(
<Stack direction="row" spacing={2} >
<Typography variant="h6" m={2} gutterBottom>
Expand All @@ -132,19 +132,19 @@ function History({ state, code, handleEditorChange, toggleDrawer }) {
spacing={2}
m={2}
>
{currentSavedCodes.length > 0 &&
<div style={{ height: "375px" , width: '100%' }}>
{history.length > 0 &&
<div style={{ height: "375px", width: '100%' }}>
<DataGrid
sx={{
"&.MuiDataGrid-root .MuiDataGrid-cell:focus-within": {
outline: "none !important",
},
"& .MuiDataGrid-columnHeader:focus-within, & .MuiDataGrid-columnHeader:focus":
{
outline: "none !important",
},
}}
rows={currentSavedCodes}
sx={{
"&.MuiDataGrid-root .MuiDataGrid-cell:focus-within": {
outline: "none !important",
},
"& .MuiDataGrid-columnHeader:focus-within, & .MuiDataGrid-columnHeader:focus":
{
outline: "none !important",
},
}}
rows={history}
columns={columns}
initialState={{
pagination: {
Expand Down Expand Up @@ -193,15 +193,15 @@ function History({ state, code, handleEditorChange, toggleDrawer }) {
onClick={
() => {
handleEditorChange("code", `${viewCode} \n${code}`)
toggleDrawer(false)();
toggleDrawer("history", false)();
}}>
Apply Code
</Button>
<Button
key={"close"}
variant="outlined"
color="error"
onClick={toggleDrawer(false)}
onClick={toggleDrawer("history", false)}
>
Close
</Button>
Expand Down
27 changes: 19 additions & 8 deletions src/components/CodeEditorWindow/Menu/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import React from 'react';
import { Button, Stack } from '@mui/material'
import History from './history';
import { Button, Stack } from '@mui/material'
import History from './history';
import PropTypes from "prop-types";
import SavedCode from './savedCode';


function Menu({ code, handleEditorChange }) {
const [state, setState] = React.useState(false);

const toggleDrawer = (open) => () => {
setState(open);
const [state, setState] = React.useState({
history: false,
savedCode: false,
});

const toggleDrawer = (name, open) => () => {
setState({ ...state, [name]: open });
};

return (
Expand All @@ -21,10 +25,17 @@ function Menu({ code, handleEditorChange }) {
mb: 1,
borderBottom: (theme) => `1px solid ${theme.palette.divider}`,
}}>
<Button onClick={toggleDrawer(true)}>History</Button>
<Button onClick={toggleDrawer("history", true)}>History</Button>
<Button onClick={toggleDrawer("savedCode", true)}>Saved Code</Button>
</Stack>
<History
state={state}
state={state.history}
code={code}
handleEditorChange={handleEditorChange}
toggleDrawer={toggleDrawer}
/>
<SavedCode
state={state.savedCode}
code={code}
handleEditorChange={handleEditorChange}
toggleDrawer={toggleDrawer}
Expand Down
224 changes: 224 additions & 0 deletions src/components/CodeEditorWindow/Menu/savedCode.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
import React, { useEffect, useState } from 'react';
import { SwipeableDrawer, Button, Box, Stack, TextField, Typography } from '@mui/material'
import { DataGrid } from '@mui/x-data-grid';
import DeleteIcon from '@mui/icons-material/Delete';
import Editor from "@monaco-editor/react";
import PropTypes from "prop-types";



function SavedCode({ state, code, handleEditorChange, toggleDrawer }) {

const [viewCode, setViewCode] = React.useState(`//Current Editor Code: \n${code}`);
const [saveNameText, setSaveNameText] = useState("");
const [savedCodes, setSavedCodes] = useState(localStorage.getItem("savedCodes") ? JSON.parse(localStorage.getItem("savedCodes")) : []);


useEffect(() => {
setSavedCodes(localStorage.getItem("savedCodes") ? JSON.parse(localStorage.getItem("savedCodes")) : []);
setViewCode(`//Current Editor Code: \n${code}`);
}, [state])

function saveCode() {
if (saveNameText !== "") {
const data = [...savedCodes, { name: saveNameText, code: code, time: new Date().toLocaleTimeString(), date: new Date().toLocaleDateString() }]
localStorage.setItem("savedCodes", JSON.stringify(data));
setSavedCodes(JSON.parse(localStorage.getItem("savedCodes")))
setSaveNameText("");
return;
}
}

const columns = [
{
field: "name",
headerName: 'Name',
minWidth: 100,
valueGetter: (params) => params.row.name,
flex: 1,
},
{
field: "time",
headerName: 'Time',
width: 100,
valueGetter: (params) => params.row.time,
},
{
field: "date",
headerName: 'Date',
width: 100,
valueGetter: (params) => params.row.date,
},
{
field: "delete",
headerName: "Delete",
width: 100,
align: "center",
headerAlign: "center",
renderCell: (params) => deleteIcon(params.row)
},


];
function deleteIcon(data) {
return (
<Button
color="error"
onClick={
() => {
const index = savedCodes.indexOf(data)
const updateCode = [...savedCodes]
updateCode.splice(index, 1);
localStorage.setItem("savedCodes", JSON.stringify(updateCode));
setSavedCodes(JSON.parse(localStorage.getItem("savedCodes")))
return;
}}
>
<DeleteIcon />
</Button>
);
}
return (
<React.Fragment key={"SavedCode"}>
<SwipeableDrawer
anchor="top"
open={state}
onClose={toggleDrawer("savedCode", false)}
onOpen={toggleDrawer("savedCode", true)}
>
<Box
sx={{
width: "100%",
height: "100%",
display: "flex",
}}
>
<Box
sx={{
width: "60%",
height: "100%",
overflow: "hidden",
overflowY: "scroll",
}}>
<Typography variant="h5" m={2} gutterBottom>
Saved Code
</Typography>
<Stack direction="row" spacing={2}>

<Typography m={2} gutterBottom>
<TextField
placeholder=" Name (Required)*"
variant="standard"
value={saveNameText}
onChange={(e) => {
setSaveNameText(e.target.value);
}}
/>
<Button onClick={saveCode}>Save</Button>
</Typography>
</Stack>
{savedCodes.length === 0 &&
(
<Stack direction="row" spacing={2} >
<Typography variant="h6" m={2} gutterBottom>
No save code found
</Typography>
</Stack>
)
}
<Stack
direction="column"
justifyContent="center"
alignItems="stretch"
spacing={2}
m={2}
>
{savedCodes.length > 0 &&
<div style={{ height: "375px", width: '100%' }}>
<DataGrid
sx={{
"&.MuiDataGrid-root .MuiDataGrid-cell:focus-within": {
outline: "none !important",
},
"& .MuiDataGrid-columnHeader:focus-within, & .MuiDataGrid-columnHeader:focus":
{
outline: "none !important",
},
}}
rows={savedCodes}
columns={columns}
initialState={{
pagination: {
paginationModel: {
pageSize: 5,
},
},
}}
pageSizeOptions={[5, 10, 15]}
rowsPerPageOptions={[5, 10]}
getRowId={(row) => `${row.time} ${row.date}`}
onRowClick={(params) => {
setViewCode(params.row.code)
}}
/>
</div>
}
</Stack>

</Box>
<Box
sx={{
width: "40%",
height: "100%",
}}
m={2}>
<Editor
height="475px"
value={(viewCode)}
options={{
scrollBeyondLastLine: false,
fontSize: 12,
wordWrap: "on",
minimap: { enabled: false },
automaticLayout: true,
readOnly: true,
mouseWheelZoom: true,
}}
/>

<Stack direction="row" spacing={2}>
<Button
key={"apply"}
variant="outlined"
color="success"
onClick={
() => {
handleEditorChange("code", `${viewCode} \n${code}`)
toggleDrawer("savedCode", false)();
}}>
Apply Code
</Button>
<Button
key={"close"}
variant="outlined"
color="error"
onClick={toggleDrawer("savedCode", false)}
>
Close
</Button>
</Stack>
</Box>
</Box>
</SwipeableDrawer>

</React.Fragment>
);
}
SavedCode.propTypes = {
state: PropTypes.bool,
toggleDrawer: PropTypes.func,
handleEditorChange: PropTypes.func,
code: PropTypes.string,
}

export default SavedCode;
6 changes: 3 additions & 3 deletions src/components/CodeEditorWindow/config/RequesFromCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export function RequestFromCode(text) {
data: data.reqBody
})
.then((response) => {
const currentSavedCodes = localStorage.getItem("currentSavedCodes") ? JSON.parse(localStorage.getItem("currentSavedCodes")) : []
currentSavedCodes.push({ code: data, time: new Date().toLocaleTimeString(), date: new Date().toLocaleDateString() })
localStorage.setItem("currentSavedCodes", JSON.stringify(currentSavedCodes));
const history = localStorage.getItem("history") ? JSON.parse(localStorage.getItem("history")) : []
history.push({ code: data, time: new Date().toLocaleTimeString(), date: new Date().toLocaleDateString() })
localStorage.setItem("history", JSON.stringify(history));
return response.data;
})
.catch((err) => {
Expand Down

0 comments on commit 3501bf6

Please sign in to comment.