From b70d56c2e8cb0279b0df4811138d9009c885f12d Mon Sep 17 00:00:00 2001 From: mrevanzak Date: Sun, 26 Nov 2023 14:26:02 +0700 Subject: [PATCH] feat: edit product --- apps/expo/package.json | 3 +- apps/expo/src/app/(app)/(tabs)/listing.tsx | 167 +---------------- apps/expo/src/app/(app)/[productId].tsx | 36 ++-- apps/expo/src/app/(app)/_layout.tsx | 6 + apps/expo/src/app/(app)/edit-product.tsx | 10 + apps/expo/src/components/ProductForm.tsx | 203 +++++++++++++++++++++ apps/expo/src/components/forms/Input.tsx | 2 +- apps/expo/src/lib/hooks/useSelectImage.ts | 3 + packages/api/src/router/product.ts | 14 +- packages/db/schema/products.ts | 24 +-- pnpm-lock.yaml | 3 + 11 files changed, 278 insertions(+), 193 deletions(-) create mode 100644 apps/expo/src/app/(app)/edit-product.tsx create mode 100644 apps/expo/src/components/ProductForm.tsx diff --git a/apps/expo/package.json b/apps/expo/package.json index dbdde35..055adb2 100644 --- a/apps/expo/package.json +++ b/apps/expo/package.json @@ -51,6 +51,7 @@ "superjson": "1.13.1", "tus-js-client": "^3.1.1", "use-tus": "^0.7.3", + "uuid": "^9.0.1", "zod": "^3.21.4", "zustand": "^4.4.6" }, @@ -79,4 +80,4 @@ ] }, "prettier": "@vivat/prettier-config" -} +} \ No newline at end of file diff --git a/apps/expo/src/app/(app)/(tabs)/listing.tsx b/apps/expo/src/app/(app)/(tabs)/listing.tsx index e050e8a..c77891a 100644 --- a/apps/expo/src/app/(app)/(tabs)/listing.tsx +++ b/apps/expo/src/app/(app)/(tabs)/listing.tsx @@ -1,172 +1,11 @@ import React from "react"; -import { ActivityIndicator, Alert } from "react-native"; -import { - AnimatedImage, - AnimatedScanner, - BorderRadiuses, - Button, - KeyboardAwareScrollView, - Text, - View, -} from "react-native-ui-lib"; -import { useRouter } from "expo-router"; -import Input from "@/components/forms/Input"; -import Picker from "@/components/forms/Picker"; -import { useSelectImage } from "@/lib/hooks/useSelectImage"; -import { api } from "@/utils/api"; -import colors from "@/utils/colors"; -import { storageClient } from "@/utils/supabase"; -import { useUser } from "@clerk/clerk-expo"; -import { MaterialCommunityIcons } from "@expo/vector-icons"; -import { zodResolver } from "@hookform/resolvers/zod"; -import { FormProvider, useForm } from "react-hook-form"; -import { z } from "zod"; - -const schema = z.object({ - name: z.string().min(3), - description: z.string().min(3), - price: z.coerce.number(), - stock: z.coerce.number(), - categoryId: z.string(), -}); +import { View } from "react-native-ui-lib"; +import ProductForm from "@/components/ProductForm"; export default function UploadProductScreen() { - const router = useRouter(); - const { user } = useUser(); - - const { data } = api.category.getCategories.useQuery({ partial: true }); - const { mutate, isPending } = api.product.addProduct.useMutation(); - - const { image, onSelectImage, onUpload, uploadProggres } = useSelectImage(); - - const methods = useForm>({ - resolver: zodResolver(schema), - }); - const { handleSubmit, reset } = methods; - const onSubmit = handleSubmit(async (data) => { - const filePath = `${user?.id}/${data.name}.png`; - - const { error } = await onUpload("products", filePath); - if (error) { - Alert.alert("Gagal mengupload gambar"); - return; - } - - const imgUrl = storageClient.from("products").getPublicUrl(filePath); - mutate( - { - ...data, - image: imgUrl.data.publicUrl, - }, - { - onSuccess: () => { - reset(); - router.push("/home"); - }, - }, - ); - }); - return ( - - - - - Upload Gambar - - - {image?.assets ? ( - <> - - } - /> - {!!uploadProggres && ( - - )} -