mirror of
https://gitlab.massbug.com/massbug/judge4c.git
synced 2025-07-04 04:01:11 +00:00
feat(workspaces): add image upload functionality to workspace creation form
This commit is contained in:
parent
2e5f76953a
commit
5de0865bbd
@ -1,2 +1,4 @@
|
||||
export const DATABASE_ID = process.env.NEXT_PUBLIC_APPWRITE_DATABASE_ID!;
|
||||
export const WORKSPACES_ID = process.env.NEXT_PUBLIC_APPWRITE_WORKSPACES_ID!;
|
||||
export const IMAGES_BUCKET_ID =
|
||||
process.env.NEXT_PUBLIC_APPWRITE_IMAGES_BUCKET_ID!;
|
||||
|
@ -11,9 +11,9 @@ export const useCreateWorkspace = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation<ResponseType, Error, RequestType>({
|
||||
mutationFn: async ({ json }) => {
|
||||
mutationFn: async ({ form }) => {
|
||||
const response = await client.api.workspaces["$post"]({
|
||||
json,
|
||||
form,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
@ -9,6 +9,7 @@ import {
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
} from "@/components/ui/form";
|
||||
import { useRef } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@ -17,6 +18,9 @@ import { Separator } from "@/components/ui/separator";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useCreateWorkspace } from "../api/use-create-workspace";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import Image from "next/image";
|
||||
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
|
||||
import { ImageIcon } from "lucide-react";
|
||||
|
||||
interface CreateWorkspaceFormProps {
|
||||
onCancel?: () => void;
|
||||
@ -25,6 +29,8 @@ interface CreateWorkspaceFormProps {
|
||||
export const CreateWorkspaceForm = ({ onCancel }: CreateWorkspaceFormProps) => {
|
||||
const { mutate, isPending } = useCreateWorkspace();
|
||||
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
const form = useForm<z.infer<typeof createWorkspaceSchema>>({
|
||||
resolver: zodResolver(createWorkspaceSchema),
|
||||
defaultValues: {
|
||||
@ -33,7 +39,27 @@ export const CreateWorkspaceForm = ({ onCancel }: CreateWorkspaceFormProps) => {
|
||||
});
|
||||
|
||||
const onSubmit = (values: z.infer<typeof createWorkspaceSchema>) => {
|
||||
mutate({ json: values });
|
||||
const finalValues = {
|
||||
...values,
|
||||
image: values.image instanceof File ? values.image : "",
|
||||
};
|
||||
|
||||
mutate(
|
||||
{ form: finalValues },
|
||||
{
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
// TODO: Redirect to new workspace
|
||||
},
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const file = e.target.files?.[0];
|
||||
if (file) {
|
||||
form.setValue("image", file);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
@ -63,6 +89,60 @@ export const CreateWorkspaceForm = ({ onCancel }: CreateWorkspaceFormProps) => {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="image"
|
||||
render={({ field }) => (
|
||||
<div className="flex flex-col gap-y-2">
|
||||
<div className="flex items-center gap-x-5">
|
||||
{field.value ? (
|
||||
<div className="size-[72px] relative rounded-md overflow-hidden">
|
||||
<Image
|
||||
alt="Logo"
|
||||
fill
|
||||
className="object-cover"
|
||||
src={
|
||||
field.value instanceof File
|
||||
? URL.createObjectURL(field.value)
|
||||
: field.value
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
<Avatar className="size-[72px]">
|
||||
<AvatarFallback>
|
||||
<ImageIcon className="size-[36px]" />
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
)}
|
||||
<div className="flex flex-col">
|
||||
<p className="text-sm">Workspace Icon</p>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
JPG, PNG, SVG or JPEG, max 1mb
|
||||
</p>
|
||||
<input
|
||||
className="hidden"
|
||||
type="file"
|
||||
accept=".jpg, .png, .jpeg, .svg"
|
||||
ref={inputRef}
|
||||
onChange={handleImageChange}
|
||||
disabled={isPending}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
disabled={isPending}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="w-fit mt-2"
|
||||
onClick={() => inputRef.current?.click()}
|
||||
>
|
||||
Upload Image
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<Separator className="my-7" />
|
||||
<div className="flex items-center justify-between">
|
||||
|
@ -2,4 +2,10 @@ import { z } from "zod";
|
||||
|
||||
export const createWorkspaceSchema = z.object({
|
||||
name: z.string().trim().min(1, "Required"),
|
||||
image: z
|
||||
.union([
|
||||
z.instanceof(File),
|
||||
z.string().transform((value) => (value === "" ? undefined : value)),
|
||||
])
|
||||
.optional(),
|
||||
});
|
||||
|
@ -2,18 +2,38 @@ import { Hono } from "hono";
|
||||
import { ID } from "node-appwrite";
|
||||
import { zValidator } from "@hono/zod-validator";
|
||||
import { createWorkspaceSchema } from "../schemas";
|
||||
import { DATABASE_ID, WORKSPACES_ID } from "@/config";
|
||||
import { sessionMiddleware } from "@/lib/session-middleware";
|
||||
import { DATABASE_ID, IMAGES_BUCKET_ID, WORKSPACES_ID } from "@/config";
|
||||
|
||||
const app = new Hono().post(
|
||||
"/",
|
||||
zValidator("json", createWorkspaceSchema),
|
||||
zValidator("form", createWorkspaceSchema),
|
||||
sessionMiddleware,
|
||||
async (c) => {
|
||||
const databases = c.get("databases");
|
||||
const storage = c.get("storage");
|
||||
const user = c.get("user");
|
||||
|
||||
const { name } = c.req.valid("json");
|
||||
const { name, image } = c.req.valid("form");
|
||||
|
||||
let uploadImageUrl: string | undefined;
|
||||
|
||||
if (image instanceof File) {
|
||||
const file = await storage.createFile(
|
||||
IMAGES_BUCKET_ID,
|
||||
ID.unique(),
|
||||
image
|
||||
);
|
||||
|
||||
const arrayBuffer = await storage.getFilePreview(
|
||||
IMAGES_BUCKET_ID,
|
||||
file.$id
|
||||
);
|
||||
|
||||
uploadImageUrl = `data:image/png;base64,${Buffer.from(
|
||||
arrayBuffer
|
||||
).toString("base64")}`;
|
||||
}
|
||||
|
||||
const workspace = await databases.createDocument(
|
||||
DATABASE_ID,
|
||||
@ -22,6 +42,7 @@ const app = new Hono().post(
|
||||
{
|
||||
name,
|
||||
userId: user.$id,
|
||||
imageUrl: uploadImageUrl,
|
||||
}
|
||||
);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user