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 dcb03a7 commit aa9d197
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 8 additions & 5 deletions backend/app/routes/admin/updateAdmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ module.exports =async (req, res) => {
if (admin.image && admin.image!=="undefined" && req.file?.path) {
fs.unlinkSync(path.join(__dirname,'..' ,'..','..', admin.image));
}

try {
let updateFields = { ...req.body };
let updateFields = {
firstName:req.body.firstName,
lastName:req.body.lastName,
contact:req.body.contact,
username:req.body.username
};
if (req.file?.path) {
updateFields.image = req.file.path;
}else{
updateFields.image = admin.image;
}

const updatedAdmin = await Admin.findByIdAndUpdate(
req.params.id,
{ $set: updateFields },
{ new: true }
);

res.status(200).json(updatedAdmin);
res.status(200).json({updatedAdmin,updateFields});
} catch (err) {
res.status(500).json(err);
}
Expand Down
5 changes: 2 additions & 3 deletions frontend/src/pages/Admin/Admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ export const Admin = (props) => {
formattedPath = `${END_POINT}/${formattedPath}`;
}
}

if(formattedPath!=="undefined" && formattedPath){
setImage(formattedPath);
}
setImage(formattedPath);
}
setAdminData(response.data[0]);
} catch (error) {
console.error("There was a problem with the fetch operation:", error);
Expand Down

0 comments on commit aa9d197

Please sign in to comment.