-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from edinstance/main
- Loading branch information
Showing
10 changed files
with
418 additions
and
71 deletions.
There are no files selected for viewing
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
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,84 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { Input } from "@/components/ui/input"; | ||
import { api } from "@/convex/_generated/api"; | ||
import { useMutation, useQuery } from "convex/react"; | ||
import Link from "next/link"; | ||
import { useRouter } from "next/navigation"; | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function editPromtPage({ | ||
params, | ||
}: { | ||
params: { promptId: string }; | ||
}) { | ||
const prompt = useQuery(api.prompts.getPromptById, { | ||
promptId: params.promptId, | ||
}); | ||
console.log(prompt); | ||
const updatePrompt = useMutation(api.prompts.updatePrompt); | ||
const isAdmin = useQuery(api.users.isAdmin); | ||
|
||
const [promptName, setPromptName] = useState(""); | ||
const [promptText, setPromptText] = useState(""); | ||
|
||
useEffect(() => { | ||
if (prompt) { | ||
setPromptName(prompt.promptName); | ||
setPromptText(prompt.prompt); | ||
} | ||
}, [prompt]); | ||
|
||
const handlePromptNameChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setPromptName(e.target.value); | ||
}; | ||
|
||
const handlePromptTextChange = ( | ||
e: React.ChangeEvent<HTMLTextAreaElement>, | ||
) => { | ||
setPromptText(e.target.value); | ||
}; | ||
|
||
const router = useRouter(); | ||
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (!prompt) return; | ||
|
||
updatePrompt({ promptId: prompt._id, promptName, prompt: promptText }); | ||
router.push("/prompts"); | ||
}; | ||
|
||
if (!isAdmin) { | ||
redirect("/"); | ||
} | ||
|
||
return ( | ||
<div className="container mx-auto min-h-screen pb-24 py-12 gap-8"> | ||
<form className="py-6 px-6 h-full" onSubmit={handleSubmit}> | ||
<div className="flex justify-between mb-4"> | ||
<h1 className="font-semibold mb-4">Edit your prompt</h1> | ||
<div className="flex space-x-4"> | ||
<Button variant="default">Save</Button> | ||
<Link href="/prompts"> | ||
<Button variant="default">Return</Button> | ||
</Link> | ||
</div> | ||
</div> | ||
<div className="flex flex-col items-start space-y-4 min-h-screen h-screen"> | ||
<Input | ||
placeholder="Prompt Name" | ||
value={promptName} | ||
onChange={handlePromptNameChange} | ||
/> | ||
<textarea | ||
placeholder="Prompt" | ||
className=" h-full w-full rounded-md border border-input bg-transparent px-3 py-1" | ||
value={promptText} | ||
onChange={handlePromptTextChange} | ||
/> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
} |
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,96 @@ | ||
"use client"; | ||
|
||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table"; | ||
import { api } from "@/convex/_generated/api"; | ||
import { useMutation, useQuery } from "convex/react"; | ||
import Link from "next/link"; | ||
import { useRouter, redirect } from "next/navigation"; | ||
|
||
const Page = () => { | ||
const prompts = useQuery(api.prompts.getAllPrompts); | ||
const enablePrompt = useMutation(api.prompts.enablePrompt); | ||
const deletePrompt = useMutation(api.prompts.deletePrompt); | ||
const createPrompt = useMutation(api.prompts.createPrompt); | ||
|
||
const isAdmin = useQuery(api.users.isAdmin); | ||
|
||
const router = useRouter(); | ||
|
||
const handleCreatePrompt = async () => { | ||
const newPromptId = await createPrompt({ | ||
promptName: "", | ||
prompt: "", | ||
}); | ||
router.push(`/prompts/${newPromptId}`); | ||
}; | ||
|
||
if (!isAdmin) { | ||
redirect("/"); | ||
} | ||
|
||
return ( | ||
<div className="container mx-auto min-h-screen py-12 pb-24 gap-8"> | ||
<div className="flex justify-between mb-4"> | ||
<h1 className="font-semibold mb-4">Review Prompts</h1> | ||
<Button variant="default" onClick={handleCreatePrompt}> | ||
Create Prompt | ||
</Button> | ||
</div> | ||
<div className="flex items-center justify-around"> | ||
<Table> | ||
<TableHeader> | ||
<TableRow> | ||
<TableHead>Prompt Name </TableHead> | ||
<TableHead>ID</TableHead> | ||
<TableHead>Active</TableHead> | ||
<TableHead>Actions</TableHead> | ||
</TableRow> | ||
</TableHeader> | ||
<TableBody> | ||
{prompts?.map((row) => ( | ||
<TableRow key={row._id}> | ||
<TableCell>{row.promptName}</TableCell> | ||
<TableCell>{row._id}</TableCell> | ||
<TableCell>{row.isActive ? "Yes" : "No"}</TableCell> | ||
<TableCell className="space-x-4 flex"> | ||
<Button | ||
variant="default" | ||
onClick={async () => { | ||
await enablePrompt({ promptId: row._id }); | ||
}} | ||
> | ||
Activate | ||
</Button> | ||
<Link | ||
href={`/prompts/${row._id}`} | ||
className="flex items-center" | ||
> | ||
<Button variant="secondary">Edit</Button> | ||
</Link> | ||
<Button | ||
variant="destructive" | ||
onClick={async () => { | ||
await deletePrompt({ promptId: row._id }); | ||
}} | ||
> | ||
Delete | ||
</Button> | ||
</TableCell> | ||
</TableRow> | ||
))} | ||
</TableBody> | ||
</Table> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Page; |
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
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
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
Oops, something went wrong.