diff --git a/src/app/chat/page.js b/src/app/chat/page.js index ce8ee56..47fd7e4 100644 --- a/src/app/chat/page.js +++ b/src/app/chat/page.js @@ -21,24 +21,29 @@ import "@chatscope/chat-ui-kit-styles/dist/default/styles.min.css"; const token = Cookies.get("token"); -// const users = localStorage.getItem("usersReceiver"); -// const users = [ -// { id: "1", name: "admin" }, -// { id: "2", name: "shit" }, -// ]; - const WebSocketTest = () => { // const [user, setUser] = useState(null); const [userMessage, setUserMessage] = useState(""); /* 使用者輸入欄的訊息 */ const [messages, setMessages] = useState([]); const [client, setClient] = useState(null); - const [selectedUser, setSelectedUser] = useState(null); + const [isInputDisabled, setIsInputDisabled] = useState(true); + const handleUserClick = (userId) => { - console.log(userId); + // console.log(userId); setSelectedUser(userId); setMessages([]); fetchPreviousMessages(userId); + handleCondition(); + setIsInputDisabled(false); + }; + + const handleCondition = () => { + if (selectedUser !== null) { + setIsInputDisabled(false); + } else { + setIsInputDisabled(true); + } }; /* 取得聯絡人資訊 */ @@ -59,8 +64,8 @@ const WebSocketTest = () => { const parsedContacts = JSON.parse(storedContacts); // 檢查每個 storedContacts 是否存在於 users 中,若不存在則加入 parsedContacts.forEach(contact => { - if (!users.some(user => user.id === contact.id)) { - console.log("test" + parsedContacts) + if (!contactsArray.some(user => user.id == contact.id)) { + console.log(`Contact ${contact.id} not found in contactsArray`); setContacts(prevContacts => [...prevContacts, contact]); } }); @@ -123,7 +128,7 @@ const WebSocketTest = () => { .filter((message) => message !== null); // 過濾掉可能的空值 setMessages(messageObjects); - console.log(previousMessagesData); + // console.log(previousMessagesData); // 設置先前的聊天訊息到狀態中 // setPreviousMessages(previousMessagesData); } catch (error) { @@ -188,7 +193,7 @@ const WebSocketTest = () => { }; } }); - console.log(messageObjects); + // console.log(messageObjects); setMessages(messageObjects); // messageObjects.forEach((msgObj) => { // setMessages((prevMessages) => [...prevMessages, msgObj]); @@ -308,6 +313,7 @@ const WebSocketTest = () => { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -351,7 +351,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -396,7 +396,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -427,7 +427,6 @@ export default function Orders() { 付款金額 購買數量 訂單狀態 - 更改訂單狀態 聯絡賣家 @@ -443,7 +442,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -452,15 +451,6 @@ export default function Orders() { ))} {getStatusText(order.status)} - - - handleButtonClick(order)}> @@ -500,7 +490,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
diff --git a/src/app/order/page.js b/src/app/order/page.js index fe84e42..7a4581b 100644 --- a/src/app/order/page.js +++ b/src/app/order/page.js @@ -265,31 +265,6 @@ export default function Orders() { fetchOrder(); }, []); - const handleButtonClick = (order) => { - // 從本地端存儲讀取已有的使用者資訊陣列,如果沒有就創建一個新陣列 - localStorage.removeItem("usersReceiver"); - const users = JSON.parse( - localStorage.getItem("usersReceiver") ?? "[]" - ); - const sellerIDToAdd = (order.buyerid); - var sellerNameToAdd = ""; - {order.productAddAmountList.map((productItem) => ( - sellerNameToAdd = productItem.product.sellerName - ))} - - const isSellerIDExists = users.some( - (user) => user.id === sellerIDToAdd - ); - - if (!isSellerIDExists) { - // 將新的 sellerID 添加到 users 陣列 - users.push({ id: sellerIDToAdd, name: sellerNameToAdd }); - // 存回 localStorage - localStorage.setItem("usersReceiver", JSON.stringify(users)); - } - window.location.href = '../chat' - }; - return ( 訂單狀態 管理訂單狀態 更改訂單狀態 - 聯絡賣家 + {/* 聯絡賣家 */} @@ -339,7 +314,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -372,11 +347,6 @@ export default function Orders() { 執行 - - handleButtonClick(order)}> - - - ))}
@@ -411,7 +381,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -454,7 +424,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -484,7 +454,6 @@ export default function Orders() { 購買數量 訂單狀態 更改訂單狀態 - 聯絡賣家 @@ -499,7 +468,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
@@ -517,11 +486,6 @@ export default function Orders() { 完成訂單 - - handleButtonClick(order)}> - - - ))} @@ -556,7 +520,7 @@ export default function Orders() { {order.productAddAmountList.map((productItem, index) => ( -
{productItem.product.currentPrice}
+
{productItem.product.currentPrice * productItem.amount}
))}
diff --git a/src/components/Chart.js b/src/components/Chart.js index cc10ad3..b92a414 100644 --- a/src/components/Chart.js +++ b/src/components/Chart.js @@ -1,190 +1,9 @@ "use client"; -// import * as React from "react"; -// import { useTheme } from "@mui/material/styles"; +import React, { useState, useEffect } from "react"; import Paper from "@mui/material/Paper"; -// import { -// LineChart, -// Line, -// XAxis, -// YAxis, -// Label, -// ResponsiveContainer, -// } from "recharts"; import Title from "./Title"; - -// // Generate Sales Data -// function createData(time, amount) { -// return { time, amount }; -// } - -// const data = [ -// createData("12/04", 1000), -// createData("12/05", 300), -// createData("12/06", 2600), -// createData("12/07", 800), -// createData("12/08", 1500), -// createData("12/09", 200), -// ]; - -// export default function Chart() { -// const theme = useTheme(); - -// return ( -// -// -// 本週收入 -// -// -// -// -// -// -// -// -// -// -// -// ); -// } - -// import React, { PureComponent } from "react"; -// import { -// LineChart, -// Line, -// XAxis, -// YAxis, -// CartesianGrid, -// Tooltip, -// Legend, -// ResponsiveContainer, -// } from "recharts"; - -// const data = [ -// { -// name: "Page A", -// uv: 4000, -// pv: 2400, -// amt: 2400, -// }, -// { -// name: "Page B", -// uv: 3000, -// pv: 1398, -// amt: 2210, -// }, -// { -// name: "Page C", -// uv: 2000, -// pv: 9800, -// amt: 2290, -// }, -// { -// name: "Page D", -// uv: 2780, -// pv: 3908, -// amt: 2000, -// }, -// { -// name: "Page E", -// uv: 1890, -// pv: 4800, -// amt: 2181, -// }, -// { -// name: "Page F", -// uv: 2390, -// pv: 3800, -// amt: 2500, -// }, -// { -// name: "Page G", -// uv: 3490, -// pv: 4300, -// amt: 2100, -// }, -// ]; - -// export default function Example() { -// return ( -// -// -// -// -// -// -// -// -// -// {/* */} -// -// -// -// -// -// ); -// } - -import React from "react"; +import axios from "axios"; +import Cookies from "js-cookie"; import { BarChart, Bar, @@ -197,68 +16,60 @@ import { ResponsiveContainer, } from "recharts"; -const data = [ - { - name: "12/04", - uv: 4000, - 收入: 240, - amt: 2400, - }, - { - name: "12/05", - uv: 3000, - 收入: 139, - amt: 2210, - }, - { - name: "12/06", - uv: 2000, - 收入: 980, - amt: 2290, - }, - { - name: "12/07", - uv: 2780, - 收入: 398, - amt: 2000, - }, - { - name: "12/08", - uv: 1890, - 收入: 480, - amt: 2181, - }, - { - name: "12/09", - uv: 2390, - 收入: 380, - amt: 2500, - }, - { - name: "12/10", - uv: 3490, - 收入: 430, - amt: 2100, - }, -]; +const INCOMEAPI = "api/v1/order/check/income" +const token = Cookies.get("token"); + +async function fetchIncome() { + const response = await axios.get(INCOMEAPI, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + return response.data; +} + export default function Chart() { + const [incomesData, setIncomesData] = useState([]); + + useEffect(() => { + async function fetchData() { + try { + const incomeData = await fetchIncome(); + setIncomesData(incomeData); + // console.log(incomeData) + } catch (error) { + console.error("獲取收入資料錯誤:", error); + } + } + fetchData(); + }, []); + + const convertedData = []; + // useEffect(() => { + const today = new Date(); + for (let i = 7; i >= 0; i--) { + const date = new Date(today); + date.setDate(date.getDate() - i); + const addZero = (num) => (num < 10 ? `0${num}` : num); + const formattedDate = `${addZero(date.getMonth() + 1)}/${addZero(date.getDate())}`; + const income = incomesData[i]; + convertedData.push({ + date: formattedDate, + 收入: income + }); + } + // console.log(convertedData); + // }, [incomesData]); + return ( - // <strong>本週收入</strong> - + @@ -276,14 +87,8 @@ export default function Chart() { fill="#8884d8" activeBar={} /> - {/* } - /> */} - /* */ ); } \ No newline at end of file diff --git a/src/components/Deposits.js b/src/components/Deposits.js index 34b8301..72bd63e 100644 --- a/src/components/Deposits.js +++ b/src/components/Deposits.js @@ -1,28 +1,60 @@ -"use client" -import * as React from 'react'; -import Link from '@mui/material/Link'; -import Typography from '@mui/material/Typography'; -import Title from './Title'; +"use client"; +import React, { useState, useEffect } from "react"; +import Link from "@mui/material/Link"; +import Typography from "@mui/material/Typography"; +import Title from "./Title"; +import axios from "axios"; +import Cookies from "js-cookie"; -function preventDefault(event) { - event.preventDefault(); +const INCOMEAPI = "api/v1/order/check/income"; +const token = Cookies.get("token"); + +async function fetchIncome() { + const response = await axios.get(INCOMEAPI, { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + return response.data; } export default function Deposits() { + const [incomesData, setIncomesData] = useState([]); + const total = incomesData.reduce((accumulator, currentValue) => accumulator + currentValue, 0); + const formattedTotal = "NT$" + total.toLocaleString("zh-TW"); + useEffect(() => { + async function fetchData() { + try { + const incomeData = await fetchIncome(); + setIncomesData(incomeData); + console.log(incomeData) + } catch (error) { + console.error("獲取收入資料錯誤:", error); + } + } + fetchData(); + }, []); + + const today = new Date(); // 獲取當前日期 + const oneWeekAgo = new Date(today); + oneWeekAgo.setDate(oneWeekAgo.getDate() - 8); + const year = oneWeekAgo.getFullYear(); // 取得年份 + const month = oneWeekAgo.getMonth() + 1; // 取得月份 + const day = oneWeekAgo.getDate(); // 取得日期 + const addZero = (num) => (num < 10 ? `0${num}` : num); + const formattedDate = `${year}-${addZero(month)}-${addZero(day)}`; + return ( - <strong>最近的總收入</strong> + + <strong>最近的總收入</strong> + - $3,024.00 + {formattedTotal} - 截止日期 2023/11/28 + 從 {formattedDate} 至今 -
- - 查看平均收入 - -
); } \ No newline at end of file diff --git a/src/components/MediaCard.jsx b/src/components/MediaCard.jsx index b0bf7eb..a185cf1 100644 --- a/src/components/MediaCard.jsx +++ b/src/components/MediaCard.jsx @@ -88,25 +88,36 @@ export default function MediaCard({ commodity }) { const [user, setUser] = React.useState(null); const handleButtonClick = () => { - // 從本地端存儲讀取已有的使用者資訊陣列,如果沒有就創建一個新陣列 - localStorage.removeItem("usersReceiver"); - const users = JSON.parse( - localStorage.getItem("usersReceiver") ?? "[]" - ); - const sellerIDToAdd = (commodity.sellerID); - const sellerNameToAdd = (commodity.sellerName); - - const isSellerIDExists = users.some( - (user) => user.id === sellerIDToAdd - ); - - if (!isSellerIDExists) { - // 將新的 sellerID 添加到 users 陣列 - users.push({ id: sellerIDToAdd, name: sellerNameToAdd }); - // 存回 localStorage - localStorage.setItem("usersReceiver", JSON.stringify(users)); + if(user){ + // 從本地端存儲讀取已有的使用者資訊陣列,如果沒有就創建一個新陣列 + localStorage.removeItem("usersReceiver"); + const users = JSON.parse( + localStorage.getItem("usersReceiver") ?? "[]" + ); + if(commodity.sellerID != user.id){ + const sellerIDToAdd = (commodity.sellerID); + const sellerNameToAdd = (commodity.sellerName); + + const isSellerIDExists = users.some( + (user) => user.id === sellerIDToAdd + ); + + if (!isSellerIDExists) { + // 將新的 sellerID 添加到 users 陣列 + users.push({ id: sellerIDToAdd, name: sellerNameToAdd }); + // 存回 localStorage + localStorage.setItem("usersReceiver", JSON.stringify(users)); + } + window.location.href = '../chat' + } + else{ + setError("此商品為您的商品"); + setOpenSnackbarErrror(true); + } + } + else{ + window.location.href = "/sign-in"; } - window.location.href = '../chat' }; React.useEffect(() => {