-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added in delete workspace functionality and tested it works [20…
…25-01-19]
- Loading branch information
1 parent
c1517a7
commit 4442529
Showing
3 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
gridwalk-ui/src/app/workspace/[workspaceId]/actions/workspace/delete_workspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
"use server"; | ||
import { getAuthToken } from "../lib/auth"; | ||
import { revalidatePath } from "next/cache"; | ||
|
||
export async function deleteWorkspace(workspaceId: string): Promise<void> { | ||
const token = await getAuthToken(); | ||
if (!workspaceId) throw new Error("Workspace ID is required"); | ||
|
||
const response = await fetch( | ||
`${process.env.GRIDWALK_API}/workspace/${workspaceId}`, | ||
{ | ||
method: "DELETE", | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
} | ||
); | ||
|
||
if (!response.ok) { | ||
const errorText = await response.text(); | ||
if (response.status === 401) throw new Error("Authentication failed"); | ||
if (response.status === 403) throw new Error("Insufficient permissions"); | ||
if (response.status === 404) throw new Error("Workspace not found"); | ||
throw new Error(errorText || "Failed to delete workspace"); | ||
} | ||
|
||
// Revalidate the projects list page to reflect the deletion | ||
revalidatePath(`/workspaces`); | ||
} |
132 changes: 132 additions & 0 deletions
132
gridwalk-ui/src/app/workspace/[workspaceId]/deleteWorkspaceModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
"use client"; | ||
import React, { useState } from "react"; | ||
|
||
interface DeleteWorkspaceModalProps { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
workspaceName: string; | ||
onConfirm: () => Promise<void>; | ||
} | ||
|
||
export const DeleteWorkspaceModal: React.FC<DeleteWorkspaceModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
workspaceName, | ||
onConfirm, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<string | null>(null); | ||
const [confirmText, setConfirmText] = useState(""); | ||
|
||
const isDeleteEnabled = confirmText === "DELETE"; | ||
|
||
const handleDelete = async (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
if (!isDeleteEnabled) return; | ||
|
||
setIsLoading(true); | ||
setError(null); | ||
|
||
try { | ||
await onConfirm(); | ||
onClose(); | ||
setConfirmText(""); | ||
} catch (err) { | ||
setError(err instanceof Error ? err.message : "An error occurred"); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
if (!isOpen) return null; | ||
|
||
return ( | ||
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"> | ||
<div className="bg-white rounded-lg p-6 w-full max-w-md relative"> | ||
<div className="flex justify-between items-center mb-4"> | ||
<h2 className="text-xl font-bold text-gray-900">Delete Workspace</h2> | ||
<button | ||
onClick={onClose} | ||
className="text-gray-500 hover:text-gray-700" | ||
> | ||
✕ | ||
</button> | ||
</div> | ||
|
||
{error && ( | ||
<div className="bg-red-50 text-red-600 p-3 rounded-md mb-4"> | ||
{error} | ||
</div> | ||
)} | ||
|
||
<div className="mb-6"> | ||
<p className="text-gray-700 mb-2"> | ||
Are you sure you want to delete{" "} | ||
<span className="font-semibold">{workspaceName}</span>? | ||
</p> | ||
<p className="text-gray-600 text-sm mb-4"> | ||
This action cannot be undone. The workspace and all associated data | ||
will be permanently removed. | ||
</p> | ||
<p className="text-gray-700 text-sm mb-2"> | ||
Type <span className="font-mono font-bold">DELETE</span> to confirm: | ||
</p> | ||
<input | ||
type="text" | ||
value={confirmText} | ||
onChange={(e) => setConfirmText(e.target.value)} | ||
className="w-full px-3 py-2 border border-gray-300 rounded-md text-gray-900 placeholder:text-gray-500 focus:outline-none focus:ring-2 focus:ring-red-500" | ||
placeholder="Enter Text..." | ||
disabled={isLoading} | ||
/> | ||
</div> | ||
|
||
<form onSubmit={handleDelete}> | ||
<div className="flex justify-end gap-2"> | ||
<button | ||
type="button" | ||
onClick={onClose} | ||
className="px-4 py-2 text-gray-700 bg-gray-100 rounded-md hover:bg-gray-200" | ||
disabled={isLoading} | ||
> | ||
Cancel | ||
</button> | ||
<button | ||
type="submit" | ||
disabled={isLoading || !isDeleteEnabled} | ||
className="px-4 py-2 bg-red-500 text-white rounded-md hover:bg-red-600 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2" | ||
> | ||
{isLoading ? ( | ||
<> | ||
<LoadingSpinner /> | ||
Deleting... | ||
</> | ||
) : ( | ||
"Delete Workspace" | ||
)} | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export const LoadingSpinner: React.FC = () => ( | ||
<svg className="animate-spin h-4 w-4" viewBox="0 0 24 24"> | ||
<circle | ||
className="opacity-25" | ||
cx="12" | ||
cy="12" | ||
r="10" | ||
stroke="currentColor" | ||
strokeWidth="4" | ||
fill="none" | ||
/> | ||
<path | ||
className="opacity-75" | ||
fill="currentColor" | ||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" | ||
/> | ||
</svg> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters