Skip to content

Commit

Permalink
create separate objects for query keys
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Apr 20, 2024
1 parent fec84f5 commit ee9e611
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 22 deletions.
8 changes: 5 additions & 3 deletions client/ecommerce/src/api/axios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ export const addAdminNotification = async (dto: AdminNotification) => {
return response.data;
};

export const getAdminNotifications = async () => {
const response = await axiosApi.get("admin-notifications");
return response.data as AdminNotification[];
export const getAdminNotifications = async (): Promise<AdminNotification[]> => {
const response = await axiosApi.get<AdminNotification[]>(
"admin-notifications"
);
return response.data;
};

export const addFeedback = async (dto: FeedbackFormData) => {
Expand Down
8 changes: 8 additions & 0 deletions client/ecommerce/src/api/requests/productNotifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@


export const ProductNotificationsKeys = {
all: ['productNotifications'] as const,
list: () => [...ProductNotificationsKeys.all, 'list'] as const,
details: () => [...ProductNotificationsKeys.all, 'detail'] as const,

}
2 changes: 1 addition & 1 deletion client/ecommerce/src/components/pages/AddProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import AddIcon from "@mui/icons-material/Add";
import { useMediaQuery } from "../../hooks/useMediaQuery";
import { ChangeEvent, useState } from "react";
import { addAdminNotification, addProduct } from "../../api/axios";
import { addProduct } from "../../api/axios";
import { useMutation } from "@tanstack/react-query";
import toast from "react-hot-toast";
import { Redirect } from "wouter";
Expand Down
10 changes: 3 additions & 7 deletions client/ecommerce/src/components/pages/Product.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useLocation, useParams } from "wouter";
import { useParams } from "wouter";
import { useSingleProduct } from "../../hooks/useSingleProduct";
import { Box, Button, Typography } from "@mui/material";
import { Box, Typography } from "@mui/material";
import { useMediaQuery } from "../../hooks/useMediaQuery";
import { useGivenUserProducts } from "../../hooks/useGivenUserProducts";
import { DisplayUserProducts } from "../DisplayUserProducts";
import { ProductWithImageAndUser } from "../../types/types";
import { DisplayUserInfo } from "../ProductPage/DisplayUserInfo";
import { sendProductInfoToCheckout } from "../../api/axios";
import { useStripe } from "@stripe/react-stripe-js";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useMutation } from "@tanstack/react-query";
import { useUserContext } from "../../contexts/UserContext";
import { useDeleteProduct } from "../../hooks/useDeleteProduct";
import { useAddAdminNotification } from "../../hooks/useAddAdminNotification";
Expand Down Expand Up @@ -38,9 +37,7 @@ export const Product = () => {
const stripe = useStripe();
const { user } = useUserContext();
const below960 = useMediaQuery(960);
const below800 = useMediaQuery(800);
const params = useParams();
const [, setLocation] = useLocation();
const productId = params?.productId;
const {
data: product,
Expand Down Expand Up @@ -152,7 +149,6 @@ export const Product = () => {
}}
>
<DisplayUserProducts
setLocation={setLocation}
products={productsWithoutMainOne as ProductWithImageAndUser[]}
/>
</Box>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { deleteProductNotifications } from "../api/axios";
import toast from "react-hot-toast";
import { ProductNotificationsKeys } from "../api/requests/productNotifications";

export const useDeleteProductNotificationsMutation = () => {
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["productNotifications", "delete"],
mutationFn: deleteProductNotifications,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["productNotifications"] });
queryClient.invalidateQueries({
queryKey: ProductNotificationsKeys.list(),
});
toast.success("Notifications cleared");
},
onError: () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useUserContext } from "../contexts/UserContext";
import { markProductNotificationsAsRead } from "../api/axios";
import { ProductNotificationsKeys } from "../api/requests/productNotifications";

export const useMarkProductNotificationAsRead = () => {
const { user } = useUserContext();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["productNotifications", "update", user.id],
mutationFn: markProductNotificationsAsRead,
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["productNotifications"] });
queryClient.invalidateQueries({
queryKey: ProductNotificationsKeys.list(),
});
},
onError: (err) => {
console.log(err);
Expand Down
3 changes: 2 additions & 1 deletion client/ecommerce/src/hooks/useProductNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useQuery } from "@tanstack/react-query";
import { fetchProductNotifications } from "../api/axios";
import { ProductNotificationsKeys } from "../api/requests/productNotifications";

export const useProductNotifications = (userId: number) => {
return useQuery({
queryKey: [`productNotifications`],
queryKey: ProductNotificationsKeys.list(),
queryFn: fetchProductNotifications,
enabled: !!userId,
});
Expand Down
4 changes: 3 additions & 1 deletion client/ecommerce/src/routes/Routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ export const Routes = () => {
</Suspense>
</Route>
</Layout>
<Route path="/:rest*">{() => <Redirect to="/" />}</Route>
<Route>
<Redirect to="/login" />
</Route>
</Switch>
);
};
8 changes: 4 additions & 4 deletions client/ecommerce/src/utils/calculateGridWidth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ export const calculateGridWidth = (
} else if (below700) {
if (totalProducts! > 4) {
return 6;
} else if(totalProducts === 1) {
return 12
} else if (totalProducts === 1) {
return 12;
} else {
return 6
return 6;
}
} else if (below1050) {
if (totalProducts == 2) {
return 6;
} else if (totalProducts == 1) {
return 12
return 12;
} else {
return 4;
}
Expand Down

0 comments on commit ee9e611

Please sign in to comment.