Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Jay Mehta
/
zango-frontend
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Registry
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit b714835d
authored
2024-05-31 16:42:46 +0530
by
jaymehta
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
img upload
1 parent
2c642d47
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
165 additions
and
73 deletions
components/vendor/ActivityDetails.js
components/vendor/ImageUploadPopUp.js
components/vendor/UploadImageComponent.js
components/vendor/ActivityDetails.js
View file @
b714835
...
...
@@ -417,6 +417,11 @@ const ActivityDetails = ({ isUpdate }) => {
toDate
=
endDate
;
}
if
(
imagesArrayComponent
.
length
<
5
)
{
toast
.
error
(
"You need to upload 5 images!"
);
return
;
}
handleSubmit
();
values
=
{
...
values
,
...
...
@@ -1105,13 +1110,56 @@ const ActivityDetails = ({ isUpdate }) => {
<
/div
>
<
/div
>
<
/div
>
{
/* <UploadImageComponent /> */
}
<
div
className
=
"row d-flex "
>
{
/* <UploadImageComponent setimagesArrayComponent={setimagesArrayComponent}
imagesArrayComponent={imagesArrayComponent}
populatedImages={activityById?.data.attributes.imagesComponent}/> */
}
<
div
className
=
"col-2"
>
<
ImageUploadPopUp
// className="col-2"
isUpdate
=
{
isUpdate
}
setimagesArrayComponent
=
{
setimagesArrayComponent
}
imagesArrayComponent
=
{
imagesArrayComponent
}
populatedImages
=
{
activityById
?.
data
.
attributes
.
imagesComponent
}
/
>
<
/div
>
<
div
className
=
"col-2"
>
<
ImageUploadPopUp
// className="col-2"
isUpdate
=
{
isUpdate
}
setimagesArrayComponent
=
{
setimagesArrayComponent
}
imagesArrayComponent
=
{
imagesArrayComponent
}
populatedImages
=
{
activityById
?.
data
.
attributes
.
imagesComponent
}
/
>
<
/div
>
<
div
className
=
"col-2"
>
<
ImageUploadPopUp
// className="col-2"
isUpdate
=
{
isUpdate
}
setimagesArrayComponent
=
{
setimagesArrayComponent
}
imagesArrayComponent
=
{
imagesArrayComponent
}
populatedImages
=
{
activityById
?.
data
.
attributes
.
imagesComponent
}
/
>
<
/div
>
<
div
className
=
"col-2"
>
<
ImageUploadPopUp
// className="col-2"
isUpdate
=
{
isUpdate
}
setimagesArrayComponent
=
{
setimagesArrayComponent
}
imagesArrayComponent
=
{
imagesArrayComponent
}
populatedImages
=
{
activityById
?.
data
.
attributes
.
imagesComponent
}
/
>
<
/div
>
<
div
className
=
"col-2"
>
<
ImageUploadPopUp
// className="col-2"
isUpdate
=
{
isUpdate
}
setimagesArrayComponent
=
{
setimagesArrayComponent
}
imagesArrayComponent
=
{
imagesArrayComponent
}
populatedImages
=
{
activityById
?.
data
.
attributes
.
imagesComponent
}
/
>
<
/div
>
<
/div
>
{
console
.
log
(
"setimagesArrayComponent"
,
imagesArrayComponent
)}
<
br
/>
<>
...
...
components/vendor/ImageUploadPopUp.js
View file @
b714835
...
...
@@ -29,58 +29,10 @@ const ImageUploadPopUp = ({ isUpdate, setimagesArrayComponent, populatedImages,
// dispatch(getLoggedInVendor());
},
[]);
// useEffect(() => {
// setFileList(
// populatedImages &&
// populatedImages.map(item => {
// return {
// uid: item.id,
// name: "image.png",
// status: "done",
// url: item.imageUrl
// };
// })
// );
// }, []);
console
.
log
(
"fileList"
,
fileList
);
let
formData
=
new
FormData
();
// const handleUpload = async () => {
// fileList.forEach(file => {
// console.log("file", file.originFileObj);
// formData.append("files", file.originFileObj);
// });
// console.log("fileList", fileList);
// console.log("formData", formData);
// setUploading(true);
let
finalId
;
// try {
// const response = await axios.post(`${process.env.NEXT_PUBLIC_BACKEND_API_URL}/api/upload`, formData, {
// headers: {
// "Content-Type": "multipart/form-data",
// Authorization: `Bearer ${session.jwt}`
// }
// });
// console.log("response image", response);
// setimagesArrayComponent(
// response.data.map(item => {
// return { imageUrl: `${process.env.NEXT_PUBLIC_BACKEND_API_URL}${item.url}` };
// })
// );
// // eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNzE2MzY5MzQyLCJleHAiOjE3MTg5NjEzNDJ9.dHTpIvuUi4r4Eh7n-74WKDXUlSaqvhjppGJ-1G4GvHk
// // if (response.status === 200) {
// // message.success("upload successfully.");
// // console.log("Uploaded file URL:", response.data[0].url); // The URL of the uploaded file
// // } else {
// // message.error("upload failed.");
// // }
// } catch (error) {
// message.error("upload failed.");
// } finally {
// setUploading(false);
// }
// };
const
handlePreview
=
async
file
=>
{
if
(
!
file
.
url
&&
!
file
.
preview
)
{
file
.
preview
=
await
getBase64
(
file
.
originFileObj
);
...
...
@@ -88,12 +40,21 @@ const ImageUploadPopUp = ({ isUpdate, setimagesArrayComponent, populatedImages,
setPreviewImage
(
file
.
url
||
file
.
preview
);
setPreviewOpen
(
true
);
};
const
handleImageChange
=
async
info
=>
{
console
.
log
(
"info"
,
info
);
setFileList
(
info
.
fileList
);
const
newFileList
=
[...
info
.
fileList
];
if
(
info
.
file
.
status
===
"uploading"
)
{
setUploading
(
true
);
setFileList
(
newFileList
);
return
;
}
if
(
info
.
file
.
status
===
"done"
)
{
let
formData
=
new
FormData
();
formData
.
append
(
"files"
,
info
.
file
.
originFileObj
);
console
.
log
(
"foles"
,
formData
);
try
{
const
response
=
await
axios
.
post
(
`
${
process
.
env
.
NEXT_PUBLIC_BACKEND_API_URL
}
/api/upload`
,
formData
,
{
headers
:
{
...
...
@@ -101,37 +62,74 @@ const ImageUploadPopUp = ({ isUpdate, setimagesArrayComponent, populatedImages,
Authorization
:
`Bearer
${
session
.
jwt
}
`
}
});
console
.
log
(
"response image"
,
response
);
// if (response.status == 200) {
// toast.success("Uploaded successfully");
// }
setimagesArrayComponent
([...
imagesArrayComponent
,
response
.
data
[
0
].
id
]);
// setimagesArrayComponent(
// response.data.map(item => {
// return { imageUrl: `${process.env.NEXT_PUBLIC_BACKEND_API_URL}${item.url}` };
// })
// );
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNzE2MzY5MzQyLCJleHAiOjE3MTg5NjEzNDJ9.dHTpIvuUi4r4Eh7n-74WKDXUlSaqvhjppGJ-1G4GvHk
// if (response.status === 200) {
// message.success("upload successfully.");
// console.log("Uploaded file URL:", response.data[0].url); // The URL of the uploaded file
// } else {
// message.error("upload failed.");
// }
if
(
response
.
status
===
200
)
{
const
newFileId
=
response
.
data
[
0
].
id
;
// Update the file list with the new file ID
const
updatedFileList
=
newFileList
.
map
(
file
=>
{
if
(
file
.
uid
===
info
.
file
.
uid
)
{
return
{
...
file
,
id
:
newFileId
,
url
:
response
.
data
[
0
].
url
// You can add any other response data you need here
};
}
return
file
;
});
setFileList
(
updatedFileList
);
setimagesArrayComponent
([...
imagesArrayComponent
,
newFileId
]);
message
.
success
(
`
${
info
.
file
.
name
}
uploaded successfully.`
);
}
}
catch
(
error
)
{
message
.
error
(
"upload failed."
);
message
.
error
(
`
${
info
.
file
.
name
}
upload failed.`
);
}
finally
{
setUploading
(
false
);
}
}
else
if
(
info
.
file
.
status
===
"removed"
)
{
// Handle file removal if needed
const
updatedFileList
=
newFileList
.
filter
(
file
=>
file
.
uid
!==
info
.
file
.
uid
);
setFileList
(
updatedFileList
);
}
};
const
handleCustomRequest
=
({
file
,
onSuccess
,
onError
})
=>
{
setTimeout
(()
=>
{
onSuccess
({
id
:
finalId
},
file
);
},
2000
);
};
const
handleRemove
=
async
file
=>
{
console
.
log
(
file
);
const
imageId
=
file
.
id
;
if
(
!
imageId
)
{
message
.
error
(
`Failed to remove
${
file
.
name
}
. Image ID not found.`
);
return
;
}
try
{
await
axios
.
delete
(
`
${
process
.
env
.
NEXT_PUBLIC_BACKEND_API_URL
}
/api/upload/files/
${
imageId
}
`
,
{
headers
:
{
"Content-Type"
:
"multipart/form-data"
,
Authorization
:
`Bearer
${
session
.
jwt
}
`
}
});
// Replace with your Strapi delete endpoint
message
.
success
(
`
${
file
.
name
}
file removed successfully.`
);
setFileList
(
prevFileList
=>
prevFileList
.
filter
(
item
=>
item
.
uid
!==
file
.
uid
));
}
catch
(
error
)
{
message
.
error
(
`Failed to remove
${
file
.
name
}
`
);
}
};
return
(
<
div
>
<
Upload
// customRequest={handleCustomRequest}
// enterButton={true}
// showUploadList={{ showRemoveIcon: false }}
listType
=
"picture-card"
fileList
=
{
fileList
}
// multiple={false}
onChange
=
{
handleImageChange
}
onRemove
=
{
handleRemove
}
onPreview
=
{
handlePreview
}
>
{
fileList
?.
length
>=
1
?
null
:
(
...
...
@@ -141,7 +139,7 @@ const ImageUploadPopUp = ({ isUpdate, setimagesArrayComponent, populatedImages,
<
/div
>
)}
<
/Upload
>
{
/* {
previewImage && (
{
previewImage
&&
(
<
Image
wrapperStyle
=
{{
display
:
"none"
...
...
@@ -154,7 +152,7 @@ const ImageUploadPopUp = ({ isUpdate, setimagesArrayComponent, populatedImages,
}}
src
=
{
previewImage
}
/
>
)}
*/
}
)}
{
/* <button type="button" onClick={handleUpload} disabled={fileList ? fileList.length === 0 : true} loading={uploading} style={{ marginTop: 16 }}>
{uploading ? "Uploading" : "Start Upload"}
</button> */
}
...
...
components/vendor/UploadImageComponent.js
View file @
b714835
import
{
PlusOutlined
}
from
"@ant-design/icons"
;
import
axios
from
"axios"
;
import
React
from
"react"
;
const
UploadImageComponent
=
()
=>
{
const
handleImageChange
=
async
info
=>
{
console
.
log
(
"info"
,
info
);
// setFileList(info.fileList);
let
formData
=
new
FormData
();
formData
.
append
(
"files"
,
info
.
file
.
originFileObj
);
console
.
log
(
"foles"
,
formData
);
try
{
const
response
=
await
axios
.
post
(
`
${
process
.
env
.
NEXT_PUBLIC_BACKEND_API_URL
}
/api/upload`
,
formData
,
{
headers
:
{
"Content-Type"
:
"multipart/form-data"
,
Authorization
:
`Bearer
${
session
.
jwt
}
`
}
});
console
.
log
(
"response image"
,
response
);
// if (response.status == 200) {
// toast.success("Uploaded successfully");
// }
setimagesArrayComponent
([...
imagesArrayComponent
,
response
.
data
[
0
].
id
]);
// setimagesArrayComponent(
// response.data.map(item => {
// return { imageUrl: `${process.env.NEXT_PUBLIC_BACKEND_API_URL}${item.url}` };
// })
// );
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MywiaWF0IjoxNzE2MzY5MzQyLCJleHAiOjE3MTg5NjEzNDJ9.dHTpIvuUi4r4Eh7n-74WKDXUlSaqvhjppGJ-1G4GvHk
// if (response.status === 200) {
// message.success("upload successfully.");
// console.log("Uploaded file URL:", response.data[0].url); // The URL of the uploaded file
// } else {
// message.error("upload failed.");
// }
}
catch
(
error
)
{
message
.
error
(
"upload failed."
);
}
finally
{
setUploading
(
false
);
}
};
return
(
<
div
>
<
div
className
=
"upload-file"
>
{
/* <button> */
}
<
input
className
=
"upload-file-input"
type
=
"file"
id
=
{
`termsConditionsFile`
}
name
=
{
`termsConditionsFile`
}
/
>
<
input
className
=
"upload-file-input"
type
=
"file"
id
=
{
`termsConditionsFile`
}
name
=
{
`termsConditionsFile`
}
onInput
=
{
e
=>
{
console
.
log
(
e
);
}}
/
>
{
/* <PlusOutlined /> */
}
{
/* </button> */
}
{
/* <label className="custom-file-label" htmlFor={`termsConditionsFile`}>
...
...
Write
Preview
Styling with
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment