Skip to content

Commit

Permalink
update functionality admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayamRana808 committed May 19, 2024
1 parent 1452f36 commit dcb03a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions backend/app/routes/admin/updateAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ module.exports =async (req, res) => {
return res.status(404).json({ error: 'Admin not found' });
}

// Delete previous image if it exists
if (admin.image) {
fs.unlinkSync(path.join(__dirname,'..' ,'..','..', admin.image));
// Delete previous image if it exists
if (admin.image && admin.image!=="undefined" && req.file?.path) {
fs.unlinkSync(path.join(__dirname,'..' ,'..','..', admin.image));
}

try {
let updateFields = { ...req.body };
if (req.file?.path) {
updateFields.image = req.file.path;
}else{
updateFields.image = admin.image;
}

const updatedAdmin = await Admin.findByIdAndUpdate(
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Admin/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ export const Admin = (props) => {
let formattedPath = response.data[0].image?.replace(/\\/g, "/");
if (formattedPath?.startsWith("uploads/")) {
formattedPath = formattedPath.replace("uploads/", "");
if (formattedPath) {
if (formattedPath && formattedPath !=="undefined") {
formattedPath = `${END_POINT}/${formattedPath}`;
}
}

if(formattedPath){
if(formattedPath!=="undefined" && formattedPath){
setImage(formattedPath);
}
setAdminData(response.data[0]);
Expand Down

0 comments on commit dcb03a7

Please sign in to comment.