Skip to content

Commit

Permalink
fix setState error
Browse files Browse the repository at this point in the history
  • Loading branch information
radekm2000 committed Apr 11, 2024
1 parent fb7a039 commit 8d8519e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 64 deletions.
12 changes: 6 additions & 6 deletions client/ecommerce/src/api/axios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import { RequestAccessTokenInterceptor } from "./request-access-token.intercepto
import { ResponseOAuthInterceptor } from "./response-auth.interceptor";
import { FeedbackFormData } from "../components/FeedbackDialog";
const LIMIT = 5;
let baseUrl;
if (import.meta.env.IS_INDEV == "true") {
baseUrl = "http://localhost:3000";
} else {
baseUrl = "https://ecommerce-123.onrender.com";
}
const baseUrl = "https://ecommerce-123.onrender.com";
// if (import.meta.env.VITE_NETLIFY == "true") {
// baseUrl = "https://ecommerce-123.onrender.com";
// } else {
// baseUrl = "http://localhost:3000";
// }

export const axiosApi = axios.create({
baseURL: baseUrl,
Expand Down
12 changes: 8 additions & 4 deletions client/ecommerce/src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import SearchIcon from "@mui/icons-material/Search";
import AccountCircle from "@mui/icons-material/AccountCircle";
import MailIcon from "@mui/icons-material/Mail";
import MoreIcon from "@mui/icons-material/MoreVert";
import { useState } from "react";
import { useEffect, useState } from "react";
import { useUserContext } from "../contexts/UserContext";
import {
Avatar,
Expand Down Expand Up @@ -147,14 +147,18 @@ export const Navbar = () => {
const handleClose = () => {
setOpenDialogContent(false);
};

const {
data: userData,
isLoading: isUserLoading,
isSuccess: isFetchedUserSuccess,
} = useFetchUserInfo(user.id);
if (isFetchedUserSuccess && !isUserLoading) {
setUser(userData);
}

useEffect(() => {
if (isFetchedUserSuccess && !isUserLoading) {
setUser(userData);
}
}, []);
const {
data: productNotificationsReceived,
isLoading: isProductNotificationsLoading,
Expand Down
3 changes: 1 addition & 2 deletions client/ecommerce/src/components/pages/Product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ProductWithImageAndUser } from "../../types/types";
import { DisplayUserInfo } from "../ProductPage/DisplayUserInfo";
import { sendProductInfoToCheckout } from "../../api/axios";
import { useStripe } from "@stripe/react-stripe-js";
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useUserContext } from "../../contexts/UserContext";
import { useDeleteProduct } from "../../hooks/useDeleteProduct";
import { useAddAdminNotification } from "../../hooks/useAddAdminNotification";
Expand All @@ -22,7 +22,6 @@ export const Product = () => {
const params = useParams();
const [, setLocation] = useLocation();
const productId = params?.productId;

const {
data: product,
isLoading,
Expand Down
4 changes: 3 additions & 1 deletion client/ecommerce/src/hooks/useDeleteProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import { deleteProduct } from "../api/axios";
import toast from "react-hot-toast";
import { useLocation } from "wouter";
import { ProductWithImageAndUser } from "../types/types";
import { useAllProducts } from "./useAllProducts";

export const useDeleteProduct = (productId: number | undefined) => {
const [, setLocation] = useLocation();
const queryClient = useQueryClient();
const { refetch } = useAllProducts();
return useMutation({
mutationFn: deleteProduct,
mutationKey: ["product", productId, "delete"],
onSuccess: () => {

toast.success("Product deleted");
setLocation("/");
refetch();
queryClient.setQueryData(
["products"],
(oldData: ProductWithImageAndUser[]) => {
Expand Down
163 changes: 112 additions & 51 deletions server/ecommerce/db/migrations/1712764568998-newMigration.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,117 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner } from 'typeorm';

export class NewMigration1712764568998 implements MigrationInterface {
name = 'NewMigration1712764568998'
name = 'NewMigration1712764568998';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "image" ("id" SERIAL NOT NULL, "imageUrl" character varying, "imageName" character varying NOT NULL, "productId" integer, CONSTRAINT "PK_d6db1ab4ee9ad9dbe86c64e4cc3" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "product" ("id" SERIAL NOT NULL, "brand" character varying NOT NULL, "category" character varying NOT NULL, "title" character varying NOT NULL, "description" character varying NOT NULL, "price" integer NOT NULL, "userId" integer, CONSTRAINT "PK_bebc9158e480b949565b4dc7a82" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "profile" ("id" SERIAL NOT NULL, "aboutYou" character varying, "country" character varying NOT NULL DEFAULT 'Poland', CONSTRAINT "PK_3dd8bfc97e4a77c70971591bdcb" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "follow" ("id" SERIAL NOT NULL, "followerId" integer, "followingId" integer, CONSTRAINT "PK_fda88bc28a84d2d6d06e19df6e5" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "message" ("id" SERIAL NOT NULL, "content" character varying NOT NULL, "conversationId" integer, "authorId" integer, CONSTRAINT "PK_ba01f0a3e0123651915008bc578" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "avatar" ("id" SERIAL NOT NULL, "avatarUrl" character varying, "avatarName" character varying NOT NULL, "userId" integer, CONSTRAINT "PK_50e36da9d45349941038eaf149d" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "review" ("id" SERIAL NOT NULL, "comment" character varying NOT NULL, "rating" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "reviewRecipientId" integer, "reviewCreatorId" integer, CONSTRAINT "PK_2e4299a343a81574217255c00ca" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "user" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "googleId" character varying, "email" character varying NOT NULL, "password" character varying, "role" character varying NOT NULL DEFAULT 'user', "avatar" character varying, "profileId" integer, "avatarEntityId" integer, CONSTRAINT "UQ_78a916df40e02a9deb1c4b75edb" UNIQUE ("username"), CONSTRAINT "REL_9466682df91534dd95e4dbaa61" UNIQUE ("profileId"), CONSTRAINT "REL_b8bf2e21095af67654559d2fed" UNIQUE ("avatarEntityId"), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "conversation" ("id" SERIAL NOT NULL, "lastMessageSentAt" TIMESTAMP NOT NULL DEFAULT now(), "creatorId" integer, "recipientId" integer, "lastMessageSentId" integer, CONSTRAINT "REL_941c7b5b184899308341512638" UNIQUE ("lastMessageSentId"), CONSTRAINT "PK_864528ec4274360a40f66c29845" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "admin_notifications" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "userId" integer NOT NULL, "action" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_1fecd1cab747b7ab6e850091901" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "feedback" ("id" SERIAL NOT NULL, "featureType" character varying NOT NULL, "email" character varying NOT NULL, "contactName" character varying NOT NULL, "description" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_8389f9e087a57689cd5be8b2b13" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "product_notification" ("id" SERIAL NOT NULL, "message" character varying NOT NULL, "isRead" boolean NOT NULL, "receiverId" integer NOT NULL, "productId" integer, CONSTRAINT "PK_17eabbfd538c6ab61d0fa6425e0" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "notification" ("id" SERIAL NOT NULL, "isRead" boolean NOT NULL, "date" TIMESTAMP NOT NULL DEFAULT now(), "senderId" integer, "receiverId" integer, CONSTRAINT "PK_705b6c7cdf9b2c2ff7ac7872cb7" PRIMARY KEY ("id"))`);
await queryRunner.query(`ALTER TABLE "product" ADD CONSTRAINT "FK_329b8ae12068b23da547d3b4798" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "follow" ADD CONSTRAINT "FK_550dce89df9570f251b6af2665a" FOREIGN KEY ("followerId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "follow" ADD CONSTRAINT "FK_e9f68503556c5d72a161ce38513" FOREIGN KEY ("followingId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "message" ADD CONSTRAINT "FK_c72d82fa0e8699a141ed6cc41b3" FOREIGN KEY ("authorId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "review" ADD CONSTRAINT "FK_30909ac2f5b587d9944fb7cf3ef" FOREIGN KEY ("reviewRecipientId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "review" ADD CONSTRAINT "FK_b9d6f5a7e9a73348c3dbf005d80" FOREIGN KEY ("reviewCreatorId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "user" ADD CONSTRAINT "FK_9466682df91534dd95e4dbaa616" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "user" ADD CONSTRAINT "FK_b8bf2e21095af67654559d2fed2" FOREIGN KEY ("avatarEntityId") REFERENCES "avatar"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "conversation" ADD CONSTRAINT "FK_941c7b5b184899308341512638a" FOREIGN KEY ("lastMessageSentId") REFERENCES "message"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "conversation" DROP CONSTRAINT "FK_941c7b5b184899308341512638a"`);
await queryRunner.query(`ALTER TABLE "user" DROP CONSTRAINT "FK_b8bf2e21095af67654559d2fed2"`);
await queryRunner.query(`ALTER TABLE "user" DROP CONSTRAINT "FK_9466682df91534dd95e4dbaa616"`);
await queryRunner.query(`ALTER TABLE "review" DROP CONSTRAINT "FK_b9d6f5a7e9a73348c3dbf005d80"`);
await queryRunner.query(`ALTER TABLE "review" DROP CONSTRAINT "FK_30909ac2f5b587d9944fb7cf3ef"`);
await queryRunner.query(`ALTER TABLE "message" DROP CONSTRAINT "FK_c72d82fa0e8699a141ed6cc41b3"`);
await queryRunner.query(`ALTER TABLE "follow" DROP CONSTRAINT "FK_e9f68503556c5d72a161ce38513"`);
await queryRunner.query(`ALTER TABLE "follow" DROP CONSTRAINT "FK_550dce89df9570f251b6af2665a"`);
await queryRunner.query(`ALTER TABLE "product" DROP CONSTRAINT "FK_329b8ae12068b23da547d3b4798"`);
await queryRunner.query(`DROP TABLE "notification"`);
await queryRunner.query(`DROP TABLE "product_notification"`);
await queryRunner.query(`DROP TABLE "feedback"`);
await queryRunner.query(`DROP TABLE "admin_notifications"`);
await queryRunner.query(`DROP TABLE "conversation"`);
await queryRunner.query(`DROP TABLE "user"`);
await queryRunner.query(`DROP TABLE "review"`);
await queryRunner.query(`DROP TABLE "avatar"`);
await queryRunner.query(`DROP TABLE "message"`);
await queryRunner.query(`DROP TABLE "follow"`);
await queryRunner.query(`DROP TABLE "profile"`);
await queryRunner.query(`DROP TABLE "product"`);
await queryRunner.query(`DROP TABLE "image"`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "image" ("id" SERIAL NOT NULL, "imageUrl" character varying, "imageName" character varying NOT NULL, "productId" integer, CONSTRAINT "PK_d6db1ab4ee9ad9dbe86c64e4cc3" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "product" ("id" SERIAL NOT NULL, "brand" character varying NOT NULL, "category" character varying NOT NULL, "title" character varying NOT NULL, "description" character varying NOT NULL, "price" integer NOT NULL, "userId" integer, CONSTRAINT "PK_bebc9158e480b949565b4dc7a82" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "profile" ("id" SERIAL NOT NULL, "aboutYou" character varying, "country" character varying NOT NULL DEFAULT 'Poland', CONSTRAINT "PK_3dd8bfc97e4a77c70971591bdcb" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "follow" ("id" SERIAL NOT NULL, "followerId" integer, "followingId" integer, CONSTRAINT "PK_fda88bc28a84d2d6d06e19df6e5" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "message" ("id" SERIAL NOT NULL, "content" character varying NOT NULL, "conversationId" integer, "authorId" integer, CONSTRAINT "PK_ba01f0a3e0123651915008bc578" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "avatar" ("id" SERIAL NOT NULL, "avatarUrl" character varying, "avatarName" character varying NOT NULL, "userId" integer, CONSTRAINT "PK_50e36da9d45349941038eaf149d" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "review" ("id" SERIAL NOT NULL, "comment" character varying NOT NULL, "rating" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "reviewRecipientId" integer, "reviewCreatorId" integer, CONSTRAINT "PK_2e4299a343a81574217255c00ca" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "user" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "googleId" character varying, "email" character varying NOT NULL, "password" character varying, "role" character varying NOT NULL DEFAULT 'user', "avatar" character varying, "profileId" integer, "avatarEntityId" integer, CONSTRAINT "UQ_78a916df40e02a9deb1c4b75edb" UNIQUE ("username"), CONSTRAINT "REL_9466682df91534dd95e4dbaa61" UNIQUE ("profileId"), CONSTRAINT "REL_b8bf2e21095af67654559d2fed" UNIQUE ("avatarEntityId"), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "conversation" ("id" SERIAL NOT NULL, "lastMessageSentAt" TIMESTAMP NOT NULL DEFAULT now(), "creatorId" integer, "recipientId" integer, "lastMessageSentId" integer, CONSTRAINT "REL_941c7b5b184899308341512638" UNIQUE ("lastMessageSentId"), CONSTRAINT "PK_864528ec4274360a40f66c29845" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "admin_notifications" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "userId" integer NOT NULL, "action" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_1fecd1cab747b7ab6e850091901" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "feedback" ("id" SERIAL NOT NULL, "featureType" character varying NOT NULL, "email" character varying NOT NULL, "contactName" character varying NOT NULL, "description" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_8389f9e087a57689cd5be8b2b13" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "product_notification" ("id" SERIAL NOT NULL, "message" character varying NOT NULL, "isRead" boolean NOT NULL, "receiverId" integer NOT NULL, "productId" integer, CONSTRAINT "PK_17eabbfd538c6ab61d0fa6425e0" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "notification" ("id" SERIAL NOT NULL, "isRead" boolean NOT NULL, "date" TIMESTAMP NOT NULL DEFAULT now(), "senderId" integer, "receiverId" integer, CONSTRAINT "PK_705b6c7cdf9b2c2ff7ac7872cb7" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "product" ADD CONSTRAINT "FK_329b8ae12068b23da547d3b4798" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "follow" ADD CONSTRAINT "FK_550dce89df9570f251b6af2665a" FOREIGN KEY ("followerId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "follow" ADD CONSTRAINT "FK_e9f68503556c5d72a161ce38513" FOREIGN KEY ("followingId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "message" ADD CONSTRAINT "FK_c72d82fa0e8699a141ed6cc41b3" FOREIGN KEY ("authorId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "review" ADD CONSTRAINT "FK_30909ac2f5b587d9944fb7cf3ef" FOREIGN KEY ("reviewRecipientId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "review" ADD CONSTRAINT "FK_b9d6f5a7e9a73348c3dbf005d80" FOREIGN KEY ("reviewCreatorId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "user" ADD CONSTRAINT "FK_9466682df91534dd95e4dbaa616" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "user" ADD CONSTRAINT "FK_b8bf2e21095af67654559d2fed2" FOREIGN KEY ("avatarEntityId") REFERENCES "avatar"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE "conversation" ADD CONSTRAINT "FK_941c7b5b184899308341512638a" FOREIGN KEY ("lastMessageSentId") REFERENCES "message"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "conversation" DROP CONSTRAINT "FK_941c7b5b184899308341512638a"`,
);
await queryRunner.query(
`ALTER TABLE "user" DROP CONSTRAINT "FK_b8bf2e21095af67654559d2fed2"`,
);
await queryRunner.query(
`ALTER TABLE "user" DROP CONSTRAINT "FK_9466682df91534dd95e4dbaa616"`,
);
await queryRunner.query(
`ALTER TABLE "review" DROP CONSTRAINT "FK_b9d6f5a7e9a73348c3dbf005d80"`,
);
await queryRunner.query(
`ALTER TABLE "review" DROP CONSTRAINT "FK_30909ac2f5b587d9944fb7cf3ef"`,
);
await queryRunner.query(
`ALTER TABLE "message" DROP CONSTRAINT "FK_c72d82fa0e8699a141ed6cc41b3"`,
);
await queryRunner.query(
`ALTER TABLE "follow" DROP CONSTRAINT "FK_e9f68503556c5d72a161ce38513"`,
);
await queryRunner.query(
`ALTER TABLE "follow" DROP CONSTRAINT "FK_550dce89df9570f251b6af2665a"`,
);
await queryRunner.query(
`ALTER TABLE "product" DROP CONSTRAINT "FK_329b8ae12068b23da547d3b4798"`,
);
await queryRunner.query(`DROP TABLE "notification"`);
await queryRunner.query(`DROP TABLE "product_notification"`);
await queryRunner.query(`DROP TABLE "feedback"`);
await queryRunner.query(`DROP TABLE "admin_notifications"`);
await queryRunner.query(`DROP TABLE "conversation"`);
await queryRunner.query(`DROP TABLE "user"`);
await queryRunner.query(`DROP TABLE "review"`);
await queryRunner.query(`DROP TABLE "avatar"`);
await queryRunner.query(`DROP TABLE "message"`);
await queryRunner.query(`DROP TABLE "follow"`);
await queryRunner.query(`DROP TABLE "profile"`);
await queryRunner.query(`DROP TABLE "product"`);
await queryRunner.query(`DROP TABLE "image"`);
}
}

0 comments on commit 8d8519e

Please sign in to comment.