Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(graph): 그래프도 실시간으로 #114

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions app/coin/Coin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const tradeText: Record<

const Coin = () => {
const queryClient = useQueryClient();
const { data: market, refetch: marketRefetch } = useSuspenseQuery(coinQuery.price());
const { data: wallet, error, refetch: walletRefetch } = useQuery(coinQuery.myWallet());
const { data: market, refetch } = useSuspenseQuery(coinQuery.price());
const { data: wallet, error } = useQuery(coinQuery.myWallet());

const { mutate: dailyReward } = useDailyRewardMutation();
const { mutate: buy } = useBuyCoinMutation();
Expand All @@ -57,16 +57,6 @@ const Coin = () => {
const [tradeMode, setTradeMode] = useState("BUY");
const [requestAmount, setRequestAmount] = useState(0);

const differenceInSeconds = dayjs().diff(dayjs(market.startedTime), "second");
const remainingSeconds = 3 * 60 - differenceInSeconds;

useEffect(() => {
setInterval(() => {
marketRefetch();
walletRefetch();
}, remainingSeconds * 1000);
}, [market.startedTime]);

if (isAxiosError(error) && error.response?.data.status === 404) {
openModal({
component: <CreateCoinAccount />,
Expand Down Expand Up @@ -254,7 +244,13 @@ const Coin = () => {
)}
</div>
</div>
<Graph updatedAt={market.startedTime} marketPrice={market.price} />
<Graph
refetch={() => {
refetch();
}}
updatedAt={market.startedTime}
marketPrice={market.price}
/>
<Accordion title="거래 내역 보기" open={false}>
<TradeHistory id={wallet.id} />
</Accordion>
Expand Down
19 changes: 15 additions & 4 deletions app/coin/Graph.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client";

import { coinQuery } from "@/services/coin/coin.query";
import { useSuspenseQuery } from "@tanstack/react-query";
import React, { FC, useState } from "react";
import { useQuery, useSuspenseQuery } from "@tanstack/react-query";
import React, { FC, useEffect, useState } from "react";
import Image from "next/image";
import { Line } from "react-chartjs-2";
import dayjs from "dayjs";
Expand Down Expand Up @@ -52,17 +52,28 @@ const cycleList = [
interface GraphProps {
updatedAt: Date;
marketPrice: number;
refetch: () => void;
}

const Graph: FC<GraphProps> = ({ updatedAt, marketPrice }) => {
const Graph: FC<GraphProps> = ({ updatedAt, marketPrice, refetch }) => {
const [cycle, setCycle] = useState("threeHours");
const { data: coin } = useSuspenseQuery(coinQuery.graph(cycle));
const { data: coin, refetch: graphRefetch } = useQuery(coinQuery.graph(cycle));

const labels = coin.map(({ startedTime }: { startedTime: Date }) =>
dayjs(startedTime).format("M/D H:m"),
);
const data = coin.map(({ price }: { price: string }) => price);

const differenceInSeconds = dayjs().diff(dayjs(updatedAt), "second");
const remainingSeconds = 3 * 60 - differenceInSeconds;

useEffect(() => {
setTimeout(() => {
refetch();
graphRefetch();
}, remainingSeconds * 1000);
}, [updatedAt]);

return (
<div className={styles.chartContainer}>
<div className={styles.chartHeader}>
Expand Down