Files
hipmi-mobile/service/upload-service.ts
Bagasbanuna02 d3c4f04e07 Profile
Add:
- Api upload background
- /api-client/api-validation.ts

### No Issue
2025-08-27 17:41:42 +08:00

79 lines
1.8 KiB
TypeScript

/* eslint-disable @typescript-eslint/no-unused-vars */
import { API_BASE_URL, apiConfig } from "@/service/api-config";
import AsyncStorage from "@react-native-async-storage/async-storage";
import axios from "axios";
import Toast from "react-native-toast-message";
export async function uploadImageService({
dirId,
imageUri,
}: {
dirId: string;
imageUri: string | null;
}) {
const token = await AsyncStorage.getItem("authToken");
const url = `${API_BASE_URL}/mobile/upload`;
console.log("url >>", url);
if (!imageUri) {
Toast.show({
type: "error",
text1: "Gagal",
text2: "Harap pilih gambar terlebih dahulu",
});
return;
}
try {
const uri = imageUri;
const filename = uri.split("/").pop();
const match = /\.(\w+)$/.exec(filename || "");
const type = match ? `image/${match[1]}` : "image";
const formData = new FormData();
// @ts-ignore: React Native tidak mengenal Blob secara langsung
formData.append("file", {
uri,
name: filename,
type,
});
formData.append("dirId", dirId);
console.log("Form data >>", JSON.stringify(formData, null, 2));
const response = await axios.post(url, formData, {
headers: {
"Content-Type": "multipart/form-data",
Authorization: `Bearer ${token}`,
},
// timeout: 30000,
});
const { data } = response;
console.log("response upload >>", JSON.stringify(data, null, 2));
if (!data.success) {
Toast.show({
type: "error",
text1: "Gagal",
text2: data.message,
});
return;
}
// Toast.show({
// type: "success",
// text1: "File berhasil diunggah",
// });
return data;
} catch (error) {
Toast.show({
type: "error",
text1: "File gagal diunggah",
});
}
}