Skip to content

Commit

Permalink
add admin notification after deleting a product
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Mar 21, 2024
1 parent 51af4c5 commit 632335e
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AdminNotification } from "../types/types";
import { Box, Divider, Typography } from "@mui/material";
import { formatDistanceToNowStrict } from "date-fns";
import { Link } from "wouter";

const DisplayAdminNotifications = ({
adminNotifications,
Expand Down Expand Up @@ -34,7 +35,12 @@ const DisplayAdminNotifications = ({
}}
>
<Typography>
{notification.username} {notification.action}
<Link href={`/members/${notification.userId}`}>
<Typography sx={{ color: "#007782", cursor: "pointer" }}>
{notification.username}
</Typography>
</Link>
{notification.action}
</Typography>
<Typography
sx={{
Expand Down
1 change: 1 addition & 0 deletions client/ecommerce/src/components/pages/AddProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export const AddProduct = () => {
mutateAdminNotification({
username: user.username,
action: `added new product ${formData.title} for ${formData.price} USD `,
createdAt: "",
});
};
const below700 = useMediaQuery(700);
Expand Down
6 changes: 5 additions & 1 deletion client/ecommerce/src/components/pages/AdminDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ export const AdminDashboard = () => {
display: "flex",
height: "90%",
position: "relative",
overflowY: "scroll",
"&::-webkit-scrollbar": {
display: "none",
},
}}
>
<CardContent sx={{ width: "100%" }}>
<CardContent sx={{ width: "100%", orientation: "vertical" }}>
<Typography
sx={{ color: color, fontSize: "28px", fontWeight: "600" }}
>
Expand Down
10 changes: 10 additions & 0 deletions client/ecommerce/src/components/pages/Product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { useStripe } from "@stripe/react-stripe-js";
import { useMutation } from "@tanstack/react-query";
import { useUserContext } from "../../contexts/UserContext";
import { useDeleteProduct } from "../../hooks/useDeleteProduct";
import { useAddAdminNotification } from "../../hooks/useAddAdminNotification";
import { useEffect } from "react";

export const Product = () => {
const stripe = useStripe();
Expand All @@ -36,6 +38,8 @@ export const Product = () => {
console.log(err);
},
});
const adminNotificationMutation = useAddAdminNotification();
const { mutate: mutateAdminNotification } = adminNotificationMutation;
const { mutate: deleteProductMutate } = useDeleteProduct(product?.id);
const { mutate } = mutation;
const { data: userProducts, isLoading: isUserProductsLoading } =
Expand Down Expand Up @@ -75,6 +79,12 @@ export const Product = () => {
) => {
e.preventDefault();
deleteProductMutate(product.id);

mutateAdminNotification({
username: user.username,
action: `deleted product ${product.title}`,
createdAt: "",
});
};
return (
<Box
Expand Down
1 change: 1 addition & 0 deletions client/ecommerce/src/hooks/useDeleteProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useDeleteProduct = (productId: number | undefined) => {
mutationFn: deleteProduct,
mutationKey: ["product", productId, "delete"],
onSuccess: () => {

toast.success("Product deleted");
setLocation("/");
queryClient.setQueryData(
Expand Down
2 changes: 1 addition & 1 deletion client/ecommerce/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export type ReviewFormFields = {
};

export type AdminNotification = {
id: number;
username: string;
action: string;
createdAt: string;
userId?: number;
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export class AdminNotificationsController {
@Post()
@UsePipes(new ZodValidationPipe(AdminNotificationDtoSchema))
@UseGuards(AuthGuard)
async create(@Body() dto: adminNotificationDto) {
return await this.adminNotificationsService.create(dto);
async create(
@AuthUser() authUser: AuthUser,
@Body() dto: adminNotificationDto,
) {
return await this.adminNotificationsService.create(dto, authUser.sub);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export class AdminNotificationsService {
@InjectRepository(AdminNotifications)
private readonly adminNotificationsRepository: Repository<AdminNotifications>,
) {}
async create(dto: adminNotificationDto) {

async create(dto: adminNotificationDto, userId: number) {
console.log(dto);
const newAdminNotification = this.adminNotificationsRepository.create({
action: dto.action,
username: dto.username,
userId,
});
console.log(newAdminNotification);

return await this.adminNotificationsRepository.save(newAdminNotification);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export class AdminNotifications {
@Column()
username: string;

@Column()
userId: number;

@Column()
action: string;

Expand Down

0 comments on commit 632335e

Please sign in to comment.