-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR] #70 - PropertyTab.js MVC패턴으로 수정
- Loading branch information
Showing
2 changed files
with
79 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { useCookies } from "react-cookie"; | ||
import client from 'gamja-backend-client'; | ||
|
||
const host = 'https://api.miruku.dog'; | ||
|
||
const PropertyModel = () => { | ||
const [cookies] = useCookies(['token']); | ||
const [coins, setCoins] = useState([]); | ||
const [allCoins, setAllCoins] = useState([]); | ||
const [price, setPrice] = useState(0); | ||
|
||
const title = ["코인명", "매입가", "현재가", "대비", "수량"]; | ||
|
||
const getConnection = () => { | ||
return { | ||
host: host, | ||
headers: { | ||
...cookies.token ? { | ||
'Authorization': `Bearer ${cookies.token}` | ||
} : null | ||
} | ||
} | ||
} | ||
|
||
const getUserMoney = async () => { | ||
await client.functional.user.me.getMyUser( | ||
getConnection() | ||
).then((response) => { | ||
// 사용자 보유 화폐 | ||
setPrice(response.user.balance); | ||
console.log(response.user); | ||
}); | ||
} | ||
|
||
const getCoins = async () => { | ||
await client.functional.coin.getCoins( | ||
getConnection() | ||
).then(response => { | ||
const allcoin = response.coins; | ||
const myCoin = coins.map(coin => coin.name); | ||
setAllCoins(allcoin.filter(coin => myCoin.includes(coin.name))); | ||
}); | ||
} | ||
|
||
const getMyCoins = async () => { | ||
await client.functional.user.me.coins.getMyCoins( | ||
getConnection() | ||
).then(response => { | ||
const coin = response.coins.filter(coin => coin.amount > 0); | ||
setCoins(coin); | ||
getCoins(); | ||
getUserMoney(); | ||
}); | ||
} | ||
|
||
useEffect(() => { | ||
if (cookies.token) { | ||
getMyCoins(); | ||
} | ||
}, [cookies.token, coins]); | ||
|
||
return { | ||
cookies, | ||
coins, | ||
allCoins, | ||
price, | ||
title | ||
}; | ||
} | ||
export default PropertyModel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters