Skip to content

Commit

Permalink
delete and rename working as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorham committed Oct 6, 2021
1 parent b74fbd0 commit 76c9cd4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/assets/images/icon-pencil.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions src/components/Explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ const CloseButton = styled(MyButton)`
margin-right: -1.1rem;
margin-left: -0.3rem;
`;
const ConfirmText = styled.p`
margin-bottom: 2rem;
`;

interface ExplorerProps {
state: WorkplaceState;
Expand Down Expand Up @@ -98,13 +101,14 @@ export function Explorer({ state, ttl, logger }: ExplorerProps) {
},
});
}
function onDeleteFile(path) {
function onDeleteFile() {
dispatch({
type: "deleteFile",
payload: {
path,
path: fileToModify,
},
});
setFileToModify("");
}

const onCanister = async (selectedCanister: string, action: string) => {
Expand Down Expand Up @@ -229,7 +233,9 @@ export function Explorer({ state, ttl, logger }: ExplorerProps) {
setFileToModify("");
}}
>
Are you sure you want to delete the file <code>{fileToModify}</code>?
<ConfirmText>
Are you sure you want to delete the file <code>{fileToModify}</code>?
</ConfirmText>
</Confirm>
<CategoryTitle>
Files
Expand Down
36 changes: 22 additions & 14 deletions src/components/FileModal.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import styled from "styled-components";
import { useState, useContext } from "react";
import { useState, useContext, useEffect } from "react";
import { Modal } from "./shared/Modal";
import { Button } from "./shared/Button";

Expand All @@ -26,8 +26,21 @@ const ButtonContainer = styled.div`
margin-top: 3rem;
`;

const MyButton = styled(Button)`
width: 14rem;
`;

export function FileModal({ isOpen, close, filename: initialFilename = "" }) {
const [fileName, setFileName] = useState(initialFilename);

// Make sure local fileName stays in sync with whatever was passed in
useEffect(() => {
if (initialFilename !== fileName) {
setFileName(initialFilename);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [initialFilename]);

const worker = useContext(WorkerContext);
const dispatch = useContext(WorkplaceDispatchContext);

Expand All @@ -38,23 +51,18 @@ export function FileModal({ isOpen, close, filename: initialFilename = "" }) {
await worker.Moc({ type: "save", file: name, content: "" });
await dispatch({ type: "saveFile", payload: { path: name, contents: "" } });
await dispatch({ type: "selectFile", payload: { path: name } });
await close();
close();
}

async function renameFile() {
if (initialFilename !== fileName) {
await worker.Moc({
type: "renameFile",
file: initialFilename,
newName: name,
});
await dispatch({
type: "renameFile",
payload: { path: initialFilename, newPath: name },
});
await dispatch({ type: "selectFile", payload: { path: name } });
}
await close();
close();
}

return (
Expand All @@ -78,21 +86,21 @@ export function FileModal({ isOpen, close, filename: initialFilename = "" }) {
<Field
type="text"
labelText="Filename"
value={fileName || initialFilename}
value={fileName}
onChange={(e) => setFileName(e.target.value)}
autoFocus
/>
<ButtonContainer>
{isRename ? (
<Button variant="primary" onClick={renameFile}>
<MyButton variant="primary" onClick={renameFile}>
Rename
</Button>
</MyButton>
) : (
<Button variant="primary" onClick={addFile}>
<MyButton variant="primary" onClick={addFile}>
Add
</Button>
</MyButton>
)}
<Button onClick={close}>Cancel</Button>
<MyButton onClick={close}>Cancel</MyButton>
</ButtonContainer>
</ModalContainer>
</Modal>
Expand Down
5 changes: 3 additions & 2 deletions src/components/FolderStructure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import styled, { css } from "styled-components";
import { ListButton } from "./shared/SelectList";
import folderIcon from "../assets/images/icon-folder.svg";
import fileIcon from "../assets/images/icon-file.svg";
import pencilIcon from "../assets/images/icon-pencil.svg";
import closeIcon from "../assets/images/icon-close.svg";

interface FoldersJson {
Expand Down Expand Up @@ -162,7 +163,7 @@ function renderFolderStructure(options: RenderOptions) {
<p>{fileName}</p>
<FileFunctions>
<FileFunctionButton onClick={(e) => onRenameFile(e, filePath)}>
<Icon src={fileIcon} alt="Rename file" title="Rename file" />
<Icon src={pencilIcon} alt="Rename file" title="Rename file" />
</FileFunctionButton>
<FileFunctionButton onClick={(e) => onDeleteFile(e, filePath)}>
<Icon src={closeIcon} alt="Delete file" title="Delete file" />
Expand All @@ -182,5 +183,5 @@ type Folders = SharedProps & {
export function FolderStructure({ filePaths, ...passProps }: Folders) {
const folderStructure = structureFolders(filePaths);

return <>{renderFolderStructure({ folderStructure, ...passProps })}</>;
return renderFolderStructure({ folderStructure, ...passProps });
}
18 changes: 16 additions & 2 deletions src/contexts/WorkplaceState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,29 @@ export const workplaceReducer = {
const fileContents = files[action.payload.path];
delete files[action.payload.path];
files[action.payload.newPath] = fileContents;
return { ...state, files };
return {
...state,
files,
selectedFile:
state.selectedFile === action.payload.path
? action.payload.newPath
: state.selectedFile,
};
}
case "deleteFile": {
const files = {
...state.files,
};

delete files[action.payload.path];
return { ...state, files };
return {
...state,
files,
selectedFile:
state.selectedFile === action.payload.path
? selectFirstFile(state.files)
: state.selectedFile,
};
}
case "deployWorkplace": {
const name = action.payload.canister.name!;
Expand Down

0 comments on commit 76c9cd4

Please sign in to comment.