Commit b8b82fe5 authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

show fav list and remove from fav

parent 5d88fd1f
...@@ -12,7 +12,7 @@ const UserController = { ...@@ -12,7 +12,7 @@ const UserController = {
async index(req, res) { async index(req, res) {
let userAll = await db.user.findMany(); let userAll = await db.user.findMany();
db.$disconnect(); db.$disconnect();
for(let i = 0; i < userAll.length; i++) { for (let i = 0; i < userAll.length; i++) {
delete userAll[i].google_token; delete userAll[i].google_token;
delete userAll[i].rank; delete userAll[i].rank;
delete userAll[i].password; delete userAll[i].password;
...@@ -52,6 +52,7 @@ const UserController = { ...@@ -52,6 +52,7 @@ const UserController = {
name, name,
email, email,
phone, phone,
address: "",
photo, photo,
username, username,
password, password,
......
...@@ -58,8 +58,8 @@ const WishlistController = { ...@@ -58,8 +58,8 @@ const WishlistController = {
user_id: Number(req.user.id), user_id: Number(req.user.id),
}, },
}); });
if(!wishlist_find)throw 400; if (!wishlist_find) throw 400;
await db.wishlist.delete({where: {id: wishlist_find.id}}) await db.wishlist.delete({ where: { id: wishlist_find.id } });
await db.$disconnect(); await db.$disconnect();
res.json({ status: 401, message: "remove fav success" }); res.json({ status: 401, message: "remove fav success" });
} catch (err) { } catch (err) {
......
/*
Warnings:
- Added the required column `count` to the `order_detail` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `order` ADD COLUMN `address` LONGTEXT NOT NULL DEFAULT '';
-- AlterTable
ALTER TABLE `order_detail` ADD COLUMN `count` INTEGER NOT NULL;
-- AlterTable
ALTER TABLE `user` ADD COLUMN `address` LONGTEXT NOT NULL DEFAULT '';
...@@ -15,7 +15,7 @@ model user { ...@@ -15,7 +15,7 @@ model user {
name String name String
email String email String
phone String phone String
address String @db.LongText address String @db.LongText @default("")
photo String @db.LongText photo String @db.LongText
google_token String google_token String
username String @unique username String @unique
...@@ -74,7 +74,7 @@ model order { ...@@ -74,7 +74,7 @@ model order {
user user @relation(fields: [user_id], references: [id]) user user @relation(fields: [user_id], references: [id])
total_price Int total_price Int
product_count Int product_count Int
address String @db.LongText address String @db.LongText @default("")
date DateTime @default(now()) date DateTime @default(now())
shipping_price Int shipping_price Int
pay_status Int pay_status Int
......
import { Box, Button, Divider, Grid, TextField } from "@mui/material"; import { Box, Button, Divider, Grid, Paper, TextField } from "@mui/material";
import { Snackbar } from "@mui/material"; import { Snackbar } from "@mui/material";
import React from "react"; import React from "react";
import { useContext } from "react"; import { useContext } from "react";
...@@ -20,7 +20,9 @@ export default function Login() { ...@@ -20,7 +20,9 @@ export default function Login() {
if (user.value?.name) if (user.value?.name)
return ( return (
<div> <div>
<h3>{user.value.name}</h3> <Paper className="m-auto" sx={{ p: 1, maxWidth: 200 }}>
<h3 className="text-center">คุณเข้าสู่ระบบแล้ว</h3>
</Paper>
</div> </div>
); );
...@@ -35,8 +37,8 @@ export default function Login() { ...@@ -35,8 +37,8 @@ export default function Login() {
if (response.data.status === 201) { if (response.data.status === 201) {
const usrsto = new userCookie(); const usrsto = new userCookie();
usrsto.store(response.data.token); usrsto.store(response.data.token);
window.location.href = '/' window.location.href = "/";
} else if (response.data.status === 203) { } else if (response.data.status === 203) {
setErrorMessage("ชื่อผู้ใช้หรือระหัสผ่านไม่ถูกต้อง"); setErrorMessage("ชื่อผู้ใช้หรือระหัสผ่านไม่ถูกต้อง");
setTimeout(() => { setTimeout(() => {
setErrorMessage(""); setErrorMessage("");
......
...@@ -8,8 +8,10 @@ import { Google } from "@mui/icons-material"; ...@@ -8,8 +8,10 @@ import { Google } from "@mui/icons-material";
import { useState } from "react"; import { useState } from "react";
import axios from "axios"; import axios from "axios";
import Head from "next/head"; import Head from "next/head";
import { useRouter } from "next/router";
export default function Login() { export default function Login() {
const router = useRouter();
const [errorMessage, setErrorMessage] = useState(""); const [errorMessage, setErrorMessage] = useState("");
const [successMessage, setSuccessMessage] = useState(""); const [successMessage, setSuccessMessage] = useState("");
const user = useContext(UserContext); const user = useContext(UserContext);
...@@ -20,13 +22,9 @@ export default function Login() { ...@@ -20,13 +22,9 @@ export default function Login() {
const [username, setUsername] = useState(""); const [username, setUsername] = useState("");
const [password, setPassword] = useState(""); const [password, setPassword] = useState("");
if (user.value?.name) if (user.value?.name) {
return ( router.push({ pathname: "/" });
<div> }
<h3>{user.value.name}</h3>
</div>
);
/** /**
* *
* @param {FormDataEvent} e * @param {FormDataEvent} e
...@@ -46,12 +44,12 @@ export default function Login() { ...@@ -46,12 +44,12 @@ export default function Login() {
setTimeout(() => { setTimeout(() => {
setSuccessMessage(""); setSuccessMessage("");
}, 3000); }, 3000);
setName("") setName("");
setEmail("") setEmail("");
setPhone("") setPhone("");
setImage("") setImage("");
setUsername("") setUsername("");
setPassword("") setPassword("");
} else if (response.data.status === 202) { } else if (response.data.status === 202) {
setErrorMessage("ชื่อผู้ใช้งานนี้ถูกใช้แล้ว กรุณาลองใหม่อีกครั้ง"); setErrorMessage("ชื่อผู้ใช้งานนี้ถูกใช้แล้ว กรุณาลองใหม่อีกครั้ง");
setTimeout(() => { setTimeout(() => {
......
import {
Box,
Button,
Paper,
Table,
TableBody,
TableCell,
TableHead,
TableRow,
} from "@mui/material";
import React, { useContext, useEffect, useState } from "react";
import { UserContext, WishlistContext } from "./_app";
import { useRouter } from "next/router";
import axios from "axios";
import { Delete } from "@mui/icons-material";
import { ShoppingCart } from "@mui/icons-material";
import PopupAlert from "@/components/PopupAlert";
export default function Wishlist() {
const user = useContext(UserContext);
const wishlist = useContext(WishlistContext);
const [products, setProducts] = useState([]);
const [message, setMessage] = useState({ error: false, message: "" });
const router = useRouter();
const [wishlistProduct, setWishlistProduct] = useState([]);
const removeFromWishlist = async (id) => {
try {
let response = await axios.delete(`/api/u/wishlist?id=${id}`, {
headers: { token: user.value.token },
});
fetchWishlist();
setMessage({ message: "นำออกจากรายการแล้ว", error: false });
setTimeout(() => {
setMessage({ message: "", error: false });
}, 2000);
} catch (err) {
console.log(err);
}
};
const fetchWishlist = async () => {
try {
let response = await axios.get("/api/u/wishlist", {
headers: { token: user.value.token },
});
wishlist.set(response.data.wishlists);
} catch (err) {
console.log(err);
}
};
const fetchProduct = async () => {
try {
let product = await axios.get("/api/product");
setProducts(product.data);
} catch (err) {}
};
useEffect(() => {
fetchProduct();
}, []);
useEffect(() => {
console.log(wishlist.value);
setWishlistProduct(
wishlist.value.map(
(wish) => products.filter((pd) => pd.id === wish.product_id)[0]
)
);
}, [wishlist, products]);
return (
<Box>
<PopupAlert
open={!!message.message.length}
isError={message.error}
message={message.message}
/>
<Paper sx={{ p: 1, overflowX: "scroll" }}>
{wishlist.value.length > 0 ? (
<Table>
<TableHead>
<TableRow>
{[
"ลำดับ",
"รูปภาพ",
"ชื่อสินค้า",
"รายละเอียด",
"ราคา",
"เพิ่มลงตระกร้า",
"นำออกจากรายการ",
].map((label, idx) => (
<TableCell key={idx}>{label}</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{wishlistProduct.map(
(pdt, idx) =>
pdt && (
<TableRow key={idx}>
<TableCell>{idx + 1}</TableCell>
<TableCell>
<img
width={100}
src={pdt.image?.length ? pdt.image : "/empty.jpg"}
alt="รูปสินค้า"
/>
</TableCell>
<TableCell>{pdt.name}</TableCell>
<TableCell>
<div className="max-h-10 overflow-scroll">
{pdt.detail}
</div>
</TableCell>
<TableCell>{pdt.price}</TableCell>
<TableCell>
<Button color="warning">
<ShoppingCart />
</Button>
</TableCell>
<TableCell>
<Button
color="error"
onClick={() => removeFromWishlist(pdt.id)}
>
<Delete />
</Button>
</TableCell>
</TableRow>
)
)}
</TableBody>
</Table>
) : (
<div className="text-center">รายการว่างเปล่า</div>
)}
</Paper>
</Box>
);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment