-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
425 additions
and
45 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,213 @@ | ||
import React from "react"; | ||
import { SafeAreaView } from "react-native-safe-area-context"; | ||
import React, { useEffect, useState } from "react"; | ||
import { ActivityIndicator } from "react-native"; | ||
import { | ||
AnimatedImage, | ||
BorderRadiuses, | ||
Button, | ||
KeyboardAwareScrollView, | ||
Text, | ||
View, | ||
} from "react-native-ui-lib"; | ||
import Constants from "expo-constants"; | ||
import * as FileSystem from "expo-file-system"; | ||
import * as ImagePicker from "expo-image-picker"; | ||
import { useRouter } from "expo-router"; | ||
import Input from "@/components/forms/Input"; | ||
import Picker from "@/components/forms/Picker"; | ||
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 { decode } from "base64-arraybuffer"; | ||
import { FormProvider, useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
|
||
export default function ListingScreen() { | ||
// const utils = api.useContext(); | ||
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(), | ||
}); | ||
|
||
// const postQuery = api.post.all.useQuery(); | ||
export default function UploadProductScreen() { | ||
const router = useRouter(); | ||
const [image, setImage] = useState<string | undefined>(); | ||
const [uploading, setUploading] = useState(false); | ||
const { user } = useUser(); | ||
|
||
// const deletePostMutation = api.post.delete.useMutation({ | ||
// onSettled: () => utils.post.all.invalidate(), | ||
// }); | ||
const { data } = api.category.getCategories.useQuery({ partial: true }); | ||
const { mutate } = api.product.addProduct.useMutation({}); | ||
|
||
const methods = useForm<z.infer<typeof schema>>({ | ||
resolver: zodResolver(schema), | ||
}); | ||
const { handleSubmit } = methods; | ||
const onSubmit = handleSubmit(async (data) => { | ||
setUploading(true); | ||
const filePath = `${user?.id}/${data.name}.png`; | ||
|
||
await onUpload(filePath); | ||
const imgUrl = storageClient.from("products").getPublicUrl(filePath); | ||
|
||
mutate( | ||
{ | ||
...data, | ||
image: imgUrl.data.publicUrl, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
setUploading(false); | ||
router.push("/home"); | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
const onUpload = async (filePath: string) => { | ||
if (!image) return; | ||
const base64 = await FileSystem.readAsStringAsync(image, { | ||
encoding: "base64", | ||
}); | ||
|
||
const { error } = await storageClient | ||
.from("products") | ||
.upload(filePath, decode(base64), { | ||
contentType: "image/png", | ||
}); | ||
if (error) { | ||
console.log(error); | ||
return; | ||
} | ||
}; | ||
|
||
const onSelectImage = async () => { | ||
const options: ImagePicker.ImagePickerOptions = { | ||
mediaTypes: ImagePicker.MediaTypeOptions.Images, | ||
allowsEditing: true, | ||
quality: 1, | ||
}; | ||
|
||
const result = await ImagePicker.launchImageLibraryAsync(options); | ||
if (!result.canceled) { | ||
setImage(result.assets[0]?.uri); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
void async function checkPermission() { | ||
if (Constants.platform?.ios) { | ||
const cameraRollStatus = | ||
await ImagePicker.requestMediaLibraryPermissionsAsync(); | ||
const cameraStatus = await ImagePicker.requestCameraPermissionsAsync(); | ||
if ( | ||
cameraRollStatus.status !== ImagePicker.PermissionStatus.GRANTED || | ||
cameraStatus.status !== ImagePicker.PermissionStatus.GRANTED | ||
) { | ||
alert("Sorry, we need these permissions to make this work!"); | ||
} | ||
} | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<SafeAreaView> | ||
{/* Changes page title visible on the header */} | ||
{/* <Stack.Screen options={{ title: "Home Page" }} /> */} | ||
</SafeAreaView> | ||
<View bg-white padding-s4 flex> | ||
<KeyboardAwareScrollView> | ||
<FormProvider {...methods}> | ||
<View | ||
marginB-s4 | ||
br40 | ||
className="border-primary border bg-white" | ||
padding-s4 | ||
flex | ||
> | ||
<Text text70 primary> | ||
Upload Gambar | ||
</Text> | ||
<View flex center paddingV-s6 className="space-y-2"> | ||
{image ? ( | ||
<> | ||
<AnimatedImage | ||
source={{ uri: image }} | ||
style={{ width: 200, height: 200 }} | ||
loader={ | ||
<ActivityIndicator | ||
color={colors.secondary} | ||
size="small" | ||
/> | ||
} | ||
/> | ||
<Button | ||
onPress={onSelectImage} | ||
label="Ganti Gambar" | ||
bg-primary | ||
/> | ||
</> | ||
) : ( | ||
<Button | ||
onPress={onSelectImage} | ||
bg-primary | ||
outline | ||
outlineColor={colors.primary} | ||
borderRadius={BorderRadiuses.br40} | ||
padding-s2 | ||
iconSource={() => ( | ||
<MaterialCommunityIcons | ||
name="file-image-plus" | ||
size={40} | ||
color={colors.primary} | ||
/> | ||
)} | ||
/> | ||
)} | ||
</View> | ||
</View> | ||
<Input | ||
id="name" | ||
label="Nama Produk" | ||
placeholder="Masukan nama produk" | ||
/> | ||
<Input | ||
id="description" | ||
label="Deskripsi Produk" | ||
multiline | ||
placeholder="Masukan deskripsi produk" | ||
/> | ||
<Input | ||
id="price" | ||
label="Harga Produk" | ||
placeholder="Masukan harga produk" | ||
inputMode="numeric" | ||
/> | ||
<Input | ||
id="stock" | ||
label="Stok Produk" | ||
placeholder="Masukan stok produk" | ||
inputMode="numeric" | ||
/> | ||
<Picker | ||
id="categoryId" | ||
label="Kategori Produk" | ||
placeholder="Pilih kategori produk" | ||
topBarProps={{ title: "Kategori" }} | ||
items={ | ||
data?.map((category) => ({ | ||
label: category.name, | ||
value: category.id, | ||
})) ?? [{ label: "Loading...", value: "loading" }] | ||
} | ||
/> | ||
</FormProvider> | ||
<Button | ||
label="Simpan" | ||
onPress={onSubmit} | ||
bg-primary | ||
br40 | ||
disabled={uploading} | ||
/> | ||
</KeyboardAwareScrollView> | ||
</View> | ||
); | ||
} |
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.