Skip to content

Commit

Permalink
[REFACTOR] #70 - StockChart.js를 MVC 패턴으로
Browse files Browse the repository at this point in the history
  • Loading branch information
jung0115 committed Sep 4, 2024
1 parent 035c7a2 commit a06d292
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 301 deletions.
40 changes: 40 additions & 0 deletions src/controller/exchange/StockChartController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { fetchCoinPriceHistories } from '../../model/exchange/StockChartModel';

// 코인 가격 데이터 정리
export function processCoinHistoryData(histories, is10Minute, is30Minute, is1Hour) {
const data = [];
const interval = is10Minute ? 10 : is30Minute ? 30 : 60;

for (let i = 0; i < histories.length; i += interval) {
data.unshift(histories[i].price);
}

return data;
}

// 시간 간격에 따라 데이터를 가져오는 함수
export async function loadCoinData(coins, chartWidth, setCoinData, token, timeOptions) {
const { is10Minute, is30Minute, is1Hour } = timeOptions;

const currentDate = new Date();
const pastDate = new Date();
const subTime = (chartWidth - 44 - 1) / (88 + 1) + 1;

if (is10Minute) {
pastDate.setMinutes(pastDate.getMinutes() - (subTime * 10) - 1);
} else if (is30Minute) {
pastDate.setMinutes(pastDate.getMinutes() - (subTime * 30) - 1);
} else if (is1Hour) {
pastDate.setHours(pastDate.getHours() - subTime - 1);
}

setCoinData({}); // 데이터 초기화

const updatedCoinData = {};
for (let coin of coins) {
const histories = await fetchCoinPriceHistories(coin.id, currentDate, pastDate, token);
updatedCoinData[coin.id] = processCoinHistoryData(histories, is10Minute, is30Minute, is1Hour);
}

setCoinData(updatedCoinData);
}
2 changes: 1 addition & 1 deletion src/model/exchange/QuestionModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import client from 'gamja-backend-client';
// API BASE URL
const host = 'https://api.miruku.dog';

export const useQuestionModel = (cookies) => {
export const useQuestionModel = ( cookies ) => {
const [qnaList, setQnaList] = useState([]);
const [isNewAnswer, setIsNewAnswer] = useState(false);
const [isLoading, setIsLoading] = useState(false);
Expand Down
26 changes: 26 additions & 0 deletions src/model/exchange/StockChartModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import client from 'gamja-backend-client';

// API 호출 및 데이터 가공을 담당하는 Model
const host = 'https://api.miruku.dog';

const getConnection = (token) => {
return {
host: host,
headers: {
...token ? { 'Authorization': `Bearer ${token}` } : null
}
}
}

// 코인 가격 기록을 가져오는 함수
export async function fetchCoinPriceHistories(coinId, currentDate, pastDate, token) {
const response = await client.functional.coin.price_histories.getPriceHistories(
getConnection(token),
coinId,
{
from: pastDate.toString(),
to: currentDate.toString()
}
);
return response.histories;
}
3 changes: 0 additions & 3 deletions src/view/components/exchange/Question.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import Loading from "./Loading";

import { useQuestionController } from '../../../controller/exchange/QuestionController';

// api BASE URL
const host = 'https://api.miruku.dog';

const Question = () => {
const [cookies] = useCookies(['token']);
const {
Expand Down
Loading

0 comments on commit a06d292

Please sign in to comment.