Skip to content

Commit

Permalink
delete request back and front jc
Browse files Browse the repository at this point in the history
  • Loading branch information
ElyasMehraein committed May 1, 2024
1 parent 339a0a6 commit 5b50989
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/api/billDelete/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function DELETE(req, res) {
const user = await response.json()
const bill = await BillModel.findById(billId);
if (bill.to !== user._id) {
Response.json(
return Response.json(
{ message: `You are not authorized to delete this bill ` },
{ status: 403 }
)
Expand Down
31 changes: 31 additions & 0 deletions app/api/requests/deleteRequest/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import RequestModel from '@/models/Request';
import { GET } from "@/app/api/auth/me/route"
import { createHash } from "crypto";

export async function DELETE(req, res) {
const body = await req.json()
let { jobRequestId } = body
const response = await GET(req)
const user = await response.json()

const uniqCode = createHash("sha256").update(user._id + process.env.PAPER).digest("hex");
const userJobRequest = await RequestModel.findById(jobRequestId)

if (!userJobRequest) {
return Response.json({ status: 404 }, { message: 'Document not found' })
}

if (!userJobRequest.uniqCode === uniqCode) {
return Response.json(
{ message: `You are not authorized to delete this jobRequest ` },
{ status: 403 }
)
}

try {
await RequestModel.findOneAndDelete({ _id: jobRequestId });
return Response.json({ message: 'Document deleted successfully' }, { status: 200 })
} catch (error) {
return Response.json({ message: 'Server error', error }, { status: 500 })
}
}
1 change: 0 additions & 1 deletion components/templates/index/Bills/BillFrame.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export default function BillFrame({ user, bill }) {
billId: bill._id
}),
});
console.log("res", res);
res.status === 200 ? setSnackbarReject(true) : setSnackbarServerError(true)
}

Expand Down
65 changes: 45 additions & 20 deletions components/templates/index/Requests/MyRequestFrame.jsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
import * as React from "react";
import Typography from "@mui/material/Typography";
import List from "@mui/material/List";
import ListItem from "@mui/material/ListItem";
import Divider from "@mui/material/Divider";
import ListItemText from "@mui/material/ListItemText";
import ListItemAvatar from "@mui/material/ListItemAvatar";
import Avatar from "@mui/material/Avatar";
import AvatarGroup from "@mui/material/AvatarGroup";
import CardHeader from "@mui/material/CardHeader";
import { red } from "@mui/material/colors";
import Box from "@mui/material/Box";
import Image from 'next/image'
import { CardActionArea, Container } from "@mui/material";
import { Button } from "@mui/material";
import ItsAvatar from "@/components/modules/ItsAvatar";
import ListItemButton from '@mui/material/ListItemButton';
import SelectProvider from "./SelectProvider";
import Accordion from '@mui/material/Accordion';
import AccordionActions from '@mui/material/AccordionActions';
import AccordionSummary from '@mui/material/AccordionSummary';
import AccordionDetails from '@mui/material/AccordionDetails';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
import { blue } from '@mui/material/colors';
import { useRouter } from "next/navigation";
import DeleteIcon from '@mui/icons-material/Delete';


const MyRequestFrame = ({ request }) => {
const router = useRouter()

const DeleteRequest = async () => {
const res = await fetch("/api/requests/deleteRequest", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jobRequestId: request._id
}),
});
res.status === 200 && location.reload();
;
}
return (
<Accordion sx={{ bgcolor: blue[50], my: 1 }} >
<AccordionSummary
Expand Down Expand Up @@ -84,21 +92,38 @@ const MyRequestFrame = ({ request }) => {
{
< AccordionDetails >
{request.acceptedBy?.map((acceptor) => (
<Box key={acceptor._id}>
<ListItemButton onClick={() => router.push(`/${acceptor.businessName}`)}>
<ListItemAvatar >
<Avatar sx={{ width: 40, height: 40 }}>
<ItsAvatar isAvatar={acceptor.isAvatar} userCodeOrBusinessBrand={acceptor.businessName} alt="workers avatar" />
</Avatar>
</ListItemAvatar>
<ListItemText align='right' primary={acceptor.businessName} secondary={acceptor.businessBrand} />
<Typography sx={{pr:5, color: 'text.secondary' }}>{acceptor.bio}</Typography>
</ListItemButton>
</Box>
<Box key={acceptor._id}>
<ListItemButton onClick={() => router.push(`/${acceptor.businessName}`)}>
<ListItemAvatar >
<Avatar sx={{ width: 40, height: 40 }}>
<ItsAvatar isAvatar={acceptor.isAvatar} userCodeOrBusinessBrand={acceptor.businessName} alt="workers avatar" />
</Avatar>
</ListItemAvatar>
<ListItemText align='right' primary={acceptor.businessName} secondary={acceptor.businessBrand} />
<Typography sx={{ pr: 5, color: 'text.secondary' }}>{acceptor.bio}</Typography>
</ListItemButton>
</Box>
))}

</AccordionDetails>
}
<Box
sx={{
display: "flex",
alignItems: "center",
justifyContent: "flex-end"
}}
>
<AccordionActions>
<Button
variant="outlined" color="error"
endIcon={<DeleteIcon sx={{ ml: -2, mr: 1 }} />}
onClick={() => DeleteRequest(request._id)}
>
حذف درخواست
</Button>
</AccordionActions>
</Box>
</Accordion>
);
};
Expand Down

0 comments on commit 5b50989

Please sign in to comment.