isMainImage przekazywane na Backend
This commit is contained in:
@@ -39,8 +39,14 @@ export async function createNotice(notice) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (response.data.noticeId !== null) {
|
if (response.data.noticeId !== null) {
|
||||||
for (const imageUri of notice.image) {
|
for (const image of notice.image) {
|
||||||
await uploadImage(response.data.noticeId, imageUri);
|
console.log("image", image);
|
||||||
|
if(notice.image.indexOf(image) === 0) {
|
||||||
|
console.log("Image is first:", image);
|
||||||
|
await uploadImage(response.data.noticeId, image, true);
|
||||||
|
}
|
||||||
|
console.log("image", image);
|
||||||
|
await uploadImage(response.data.noticeId, image, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,7 +100,7 @@ export async function getAllImagesByNoticeId(noticeId) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const uploadImage = async (noticeId, imageUri) => {
|
export const uploadImage = async (noticeId, imageObj, isFirst) => {
|
||||||
const { token } = useAuthStore.getState();
|
const { token } = useAuthStore.getState();
|
||||||
const headers = {
|
const headers = {
|
||||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||||
@@ -102,20 +108,20 @@ export const uploadImage = async (noticeId, imageUri) => {
|
|||||||
};
|
};
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
||||||
const filename = imageUri.split("/").pop();
|
const filename = imageObj.split("/").pop();
|
||||||
|
|
||||||
const match = /\.(\w+)$/.exec(filename);
|
const match = /\.(\w+)$/.exec(filename);
|
||||||
const type = match ? `image/${match[1]}` : "image/jpeg";
|
const type = match ? `image/${match[1]}` : "image/jpeg";
|
||||||
|
|
||||||
formData.append("file", {
|
formData.append("file", {
|
||||||
uri: imageUri,
|
uri: imageObj,
|
||||||
name: filename,
|
name: filename,
|
||||||
type: type,
|
type: type,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`${API_URL}/images/upload/${noticeId}`,
|
`${API_URL}/images/upload/${noticeId}?isMainImage=${isFirst}`,
|
||||||
formData,
|
formData,
|
||||||
{
|
{
|
||||||
headers: headers,
|
headers: headers,
|
||||||
@@ -124,7 +130,7 @@ export const uploadImage = async (noticeId, imageUri) => {
|
|||||||
console.info("Upload successful:", response.data);
|
console.info("Upload successful:", response.data);
|
||||||
return response.data;
|
return response.data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("imageURI:", imageUri);
|
console.log("imageURI:", imageObj);
|
||||||
|
|
||||||
console.error(
|
console.error(
|
||||||
"Error uploading image:",
|
"Error uploading image:",
|
||||||
|
|||||||
Reference in New Issue
Block a user