Skip to content

Commit a07a0af

Browse files
committed
DELETE implemented
1 parent db78c67 commit a07a0af

6 files changed

Lines changed: 52 additions & 47 deletions

File tree

app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func main() {
7878
apiRouter.HandleFunc("/contents", content.ContentUpdateAPIHandler).Methods("PUT")
7979

8080
apiRouter.HandleFunc("/contents/{path}", content.ContentRenameAPIHandler).Methods("PATCH")
81-
apiRouter.HandleFunc("/contents/{path}", content.ContentDeleteAPIHandler).Methods("DELETE")
81+
apiRouter.HandleFunc("/contents", content.ContentDeleteAPIHandler).Methods("DELETE")
8282

8383
// kernelspecs
8484
apiRouter.HandleFunc("/kernelspecs", kernelspec.KernelspecAPIHandler).Methods("GET")

content/content_api_handler.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ func ContentUpdateAPIHandler(w http.ResponseWriter, req *http.Request) {
7474

7575
UpdateContent(body.Path, body.Type, body.Format, body.Content)
7676

77+
w.Header().Set("Content-Type", "application/json")
78+
w.WriteHeader(http.StatusOK)
79+
}
80+
81+
func ContentDeleteAPIHandler(w http.ResponseWriter, req *http.Request) {
82+
log.Info().Msg("DELETE request received")
83+
84+
var body ContentRequestBody
85+
err := json.NewDecoder(req.Body).Decode(&body)
86+
87+
log.Info().Msgf("%s", body)
88+
if err != nil {
89+
http.Error(w, err.Error(), http.StatusBadRequest)
90+
return
91+
}
92+
93+
deleteFile(body.Path)
94+
7795
w.Header().Set("Content-Type", "application/json")
7896
w.WriteHeader(http.StatusOK)
7997

@@ -107,18 +125,6 @@ func ContentRenameAPIHandler(w http.ResponseWriter, req *http.Request) {
107125
w.WriteHeader(http.StatusOK)
108126
}
109127

110-
func ContentDeleteAPIHandler(w http.ResponseWriter, req *http.Request) {
111-
log.Info().Msg("Delete request received")
112-
vars := mux.Vars(req)
113-
path := vars["path"]
114-
log.Info().Msgf("path : %s", path)
115-
116-
deleteFile(path)
117-
118-
w.Header().Set("Content-Type", "application/json")
119-
w.WriteHeader(http.StatusOK)
120-
}
121-
122128
func NewErrorResponse(w http.ResponseWriter, i int, s string) {
123129
panic("unimplemented")
124130
}

ui/src/ide/NavigationPanel.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const NavigationPanel = () => {
55

66
const [menuPosition, setMenuPosition] = useState<{ xPos: number; yPos: number } | null>(null);
77
const [isMenuVisible, setIsMenuVisible] = useState<boolean>(false);
8+
const [contextPath, setContextPath] = useState<string>("");
89

910
const menuBarClickHandler = (e) => {
1011
e.preventDefault(); // prevent the default behaviour when right clicked
@@ -67,6 +68,7 @@ const NavigationPanel = () => {
6768
xPos={menuPosition.xPos}
6869
yPos={menuPosition.yPos}
6970
items={menuItems}
71+
path={contextPath}
7072
onClose={handleCloseMenu}
7173
/>
7274
)}

ui/src/ide/editor/FileEditor.tsx

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,6 @@ export default function FileEditor(props) {
7979
});
8080
}
8181

82-
const onSave = async () => {
83-
let path = "abc.py";
84-
alert("Saving file")
85-
const res = await fetch(BaseApiUrl + "/api/contents/abc.py", {
86-
method: 'PUT',
87-
body: JSON.stringify({
88-
content: fileContents,
89-
type: 'file',
90-
format: 'text'
91-
})
92-
});
93-
}
94-
95-
const deleteFile = async () => {
96-
let path = "abc.py";
97-
const res = await fetch(BaseApiUrl + "/api/contents/untitled1", {
98-
method: 'DELETE'
99-
});
100-
}
101-
10282
const getExtensionToLoad = () => {
10383
switch(props.data.extension){
10484
case "go":

ui/src/ide/sidebar/ContextMenu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ import './ContextMenu.scss'; // Optional: for styling
44

55
interface MenuItem {
66
label: string;
7-
action: () => void;
7+
action: (path: string) => void;
88
}
99

1010
interface ContextMenuProps {
1111
xPos: number;
1212
yPos: number;
1313
items: MenuItem[];
1414
onClose: () => void;
15+
path: string
1516
}
1617

17-
const ContextMenu: React.FC<ContextMenuProps> = ({ xPos, yPos, items, onClose }) => {
18-
const handleClick = (action: () => void) => {
19-
action();
18+
const ContextMenu: React.FC<ContextMenuProps> = ({ xPos, yPos, items, onClose, path }) => {
19+
const handleClick = (action: (path) => void) => {
20+
action(path);
2021
onClose();
2122
};
2223

ui/src/ide/sidebar/FileBrowser.tsx

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ export default function FileBrowser({ sendDataToParent }) {
1313

1414
const [menuPosition, setMenuPosition] = useState<{ xPos: number; yPos: number } | null>(null);
1515
const [isMenuVisible, setIsMenuVisible] = useState<boolean>(false);
16+
const [contextPath, setContextPath] = useState<string>("");
1617

17-
const handleRightClick = (event: React.MouseEvent) => {
18-
event.preventDefault();
19-
setMenuPosition({ xPos: event.pageX, yPos: event.pageY });
20-
setIsMenuVisible(true);
21-
};
18+
// const handleRightClick = (event: React.MouseEvent, path:string) => {
19+
// event.preventDefault();
20+
// setMenuPosition({ xPos: event.pageX, yPos: event.pageY });
21+
// setContextPath(path)
22+
// setIsMenuVisible(true);
23+
// };
2224

2325
const handleCloseMenu = () => {
2426
setIsMenuVisible(false);
@@ -31,18 +33,19 @@ export default function FileBrowser({ sendDataToParent }) {
3133
const [cwd, setCwd] = useState<String>("");
3234

3335

34-
const directoryRightClickHandler = (e) => {
36+
const directoryRightClickHandler = (e, path: string) => {
3537
e.preventDefault(); // prevent the default behaviour when right clicked
3638
console.log("Right Click");
3739

3840
setMenuPosition({ xPos: e.pageX, yPos: e.pageY });
41+
setContextPath(path);
3942
setIsMenuVisible(true);
4043
}
4144

4245

4346
const menuItems = [
44-
{ label: 'Rename', action: () => alert('Rename') },
45-
{ label: 'Delete', action: () => alert('Delete') },
47+
{ label: 'Rename', action: (contextPath: string) => alert('Rename' + contextPath) },
48+
{ label: 'Delete', action: (contextPath: string) => deleteFile(contextPath) },
4649
];
4750

4851

@@ -91,6 +94,18 @@ export default function FileBrowser({ sendDataToParent }) {
9194
FetchData();
9295
}
9396

97+
const deleteFile = async (path: string) => {
98+
99+
console.log('Deleting file');
100+
101+
const res = await fetch(BaseApiUrl + "/api/contents", {
102+
method: 'DELETE',
103+
body: JSON.stringify({
104+
path : path
105+
})
106+
});
107+
}
108+
94109
useEffect(() => {
95110
FetchData();
96111
}, [cwd])
@@ -129,6 +144,7 @@ export default function FileBrowser({ sendDataToParent }) {
129144
xPos={menuPosition.xPos}
130145
yPos={menuPosition.yPos}
131146
items={menuItems}
147+
path={contextPath}
132148
onClose={handleCloseMenu}
133149
/>
134150
)}
@@ -140,7 +156,7 @@ export default function FileBrowser({ sendDataToParent }) {
140156
function FileItem({directoryRightClickHandler, handleFileClick, content}){
141157
return (
142158
<li className='fileItem'>
143-
<a onContextMenu={(e) => directoryRightClickHandler(e)} onClick={() => handleFileClick(content.name, content.path, content.type)}>
159+
<a onContextMenu={(e) => directoryRightClickHandler(e, content.path)} onClick={() => handleFileClick(content.name, content.path, content.type)}>
144160
<img src="./images/editor/py-icon.svg" alt="" />
145161
{content.name}
146162
</a>
@@ -177,7 +193,7 @@ function DirectoryItem({directoryRightClickHandler, data, sendDataToParent}){
177193

178194
return (
179195
<li className='fileItem'>
180-
<a onContextMenu={(e) => directoryRightClickHandler(e)} onClick={() => handleDirectoryClick(content.path, content.type)}>
196+
<a onContextMenu={(e) => directoryRightClickHandler(e, content.path)} onClick={() => handleDirectoryClick(content.path, content.type)}>
181197
<img src="./images/editor/directory.svg" alt="" />
182198
{content.name}
183199
</a>

0 commit comments

Comments
 (0)