From bb83a0702d8d12ca974b59a483f3c1a5abc0d83d Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 10 Sep 2022 14:56:21 +0200 Subject: [PATCH] convert store methods to async --- src/stores/ftbot.ts | 128 ++++++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 57 deletions(-) diff --git a/src/stores/ftbot.ts b/src/stores/ftbot.ts index 62f1097e..dbfda20d 100644 --- a/src/stores/ftbot.ts +++ b/src/stores/ftbot.ts @@ -424,17 +424,21 @@ export function createBotSubStore(botId: string, botName: string) { return Promise.reject(data); } }, - getStrategyList() { - return api - .get('/strategies') - .then((result) => (this.strategyList = result.data.strategies)) - .catch(console.error); + async getStrategyList() { + try { + const { data } = await api.get('/strategies'); + this.strategyList = data.strategies; + return Promise.resolve(data); + } catch (error) { + console.error(error); + return Promise.reject(error); + } }, async getStrategy(strategy: string) { try { - const result = await api.get(`/strategy/${strategy}`, {}); - this.strategy = result.data; - return Promise.resolve(result.data); + const { data } = await api.get(`/strategy/${strategy}`, {}); + this.strategy = data; + return Promise.resolve(data); } catch (error) { console.error(error); return Promise.reject(error); @@ -442,13 +446,12 @@ export function createBotSubStore(botId: string, botName: string) { }, async getAvailablePairs(payload: AvailablePairPayload) { try { - const result = await api.get('/available_pairs', { + const { data } = await api.get('/available_pairs', { params: { ...payload }, }); // result is of type AvailablePairResult - const { pairs } = result.data; - this.pairlist = pairs; - return Promise.resolve(result.data); + this.pairlist = data.pairs; + return Promise.resolve(data); } catch (error) { console.error(error); return Promise.reject(error); @@ -456,44 +459,48 @@ export function createBotSubStore(botId: string, botName: string) { }, async getPerformance() { try { - const result = await api.get('/performance'); - this.performanceStats = result.data; - return Promise.resolve(result.data); + const { data } = await api.get('/performance'); + this.performanceStats = data; + return Promise.resolve(data); } catch (error) { console.error(error); return Promise.reject(error); } }, - getWhitelist() { - return api - .get('/whitelist') - .then((result) => { - this.whitelist = result.data.whitelist; - this.pairlistMethods = result.data.method; - return Promise.resolve(result.data); - }) - .catch((error) => { - // console.error(error); - return Promise.reject(error); - }); + async getWhitelist() { + try { + const { data } = await api.get('/whitelist'); + this.whitelist = data.whitelist; + this.pairlistMethods = data.method; + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } }, - getBlacklist() { - return api - .get('/blacklist') - .then((result) => (this.blacklist = result.data.blacklist)) - .catch(console.error); + async getBlacklist() { + try { + const { data } = await api.get('/blacklist'); + this.blacklist = data.blacklist; + return Promise.resolve(data); + } catch (error) { + return Promise.reject(error); + } }, - getProfit() { - return api - .get('/profit') - .then((result) => (this.profit = result.data)) - .catch(console.error); + async getProfit() { + try { + const { data } = await api.get('/profit'); + this.profit = data; + return Promise.resolve(data); + } catch (error) { + console.error(error); + return Promise.reject(error); + } }, async getBalance() { try { - const result = await api.get('/balance'); - this.balance = result.data; - return Promise.resolve(result.data); + const { data } = await api.get('/balance'); + this.balance = data; + return Promise.resolve(data); } catch (error) { return Promise.reject(error); } @@ -505,32 +512,39 @@ export function createBotSubStore(botId: string, botName: string) { this.dailyStats = data; return Promise.resolve(data); } catch (error) { + console.error(error); return Promise.reject(error); } }, - getState() { - return api - .get('/show_config') - .then((result) => { - this.botState = result.data; - this.botStatusAvailable = true; - }) - .catch(console.error); + async getState() { + try { + const { data } = await api.get('/show_config'); + this.botState = data; + this.botStatusAvailable = true; + return Promise.resolve(data); + } catch (error) { + console.error(error); + return Promise.reject(error); + } }, - getLogs() { - return api - .get('/logs') - .then((result) => (this.lastLogs = result.data.logs)) - .catch(console.error); + async getLogs() { + try { + const { data } = await api.get('/logs'); + this.lastLogs = data.logs; + return Promise.resolve(data); + } catch (error) { + console.error(error); + return Promise.reject(error); + } }, // // Post methods // // TODO: Migrate calls to API to a seperate module unrelated to pinia? async startBot() { try { - const res = await api.post<{}, AxiosResponse>('/start', {}); - console.log(res.data); - showAlert(res.data.status); - return Promise.resolve(res); + const { data } = await api.post<{}, AxiosResponse>('/start', {}); + console.log(data); + showAlert(data.status); + return Promise.resolve(data); } catch (error) { if (axios.isAxiosError(error)) { console.error(error.response);