API upload image
Add: - utils/pickImage.ts - service/upload-service.ts - constants/directory-id.ts - constants/base-url-api-strorage.ts ### No Issue
This commit is contained in:
74
app/(application)/coba/screen-upload.tsx
Normal file
74
app/(application)/coba/screen-upload.tsx
Normal file
@@ -0,0 +1,74 @@
|
||||
import {
|
||||
AvatarCustom,
|
||||
BoxButtonOnFooter,
|
||||
ButtonCustom,
|
||||
StackCustom,
|
||||
ViewWrapper,
|
||||
} from "@/components";
|
||||
import DIRECTORY_ID from "@/constants/directory-id";
|
||||
import DUMMY_IMAGE from "@/constants/dummy-image-value";
|
||||
import { uploadImageService } from "@/service/upload-service";
|
||||
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||
import { router } from "expo-router";
|
||||
import { useState } from "react";
|
||||
import { View } from "react-native";
|
||||
import ButtonUpload from "./button-upload";
|
||||
|
||||
export default function ScreenUpload() {
|
||||
const [imageUri, setImageUri] = useState<string | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
|
||||
async function onUpload() {
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const response = await uploadImageService({
|
||||
imageUri,
|
||||
dirId: DIRECTORY_ID.profile_foto,
|
||||
});
|
||||
|
||||
console.log("Response", JSON.stringify(response, null, 2));
|
||||
if (response.success) {
|
||||
await AsyncStorage.setItem("idImage", response.data.id);
|
||||
router.back();
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Error", error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function buttonUpload() {
|
||||
return (
|
||||
<>
|
||||
<BoxButtonOnFooter>
|
||||
<ButtonCustom isLoading={isLoading} onPress={() => onUpload()}>
|
||||
Simpan
|
||||
</ButtonCustom>
|
||||
</BoxButtonOnFooter>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<ViewWrapper footerComponent={buttonUpload()}>
|
||||
<StackCustom>
|
||||
<View
|
||||
style={{
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
<AvatarCustom
|
||||
source={imageUri ? { uri: imageUri } : DUMMY_IMAGE.avatar}
|
||||
size="xl"
|
||||
/>
|
||||
</View>
|
||||
|
||||
<ButtonUpload setImageUri={setImageUri} />
|
||||
</StackCustom>
|
||||
</ViewWrapper>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user