From 7326f3f3c2e72c0eb04f13b85392391b366d5117 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 5 Sep 2020 17:08:48 +0200 Subject: [PATCH] Move alert showing to helper method --- src/store/modules/alerts.ts | 4 ++ src/store/modules/ftbot.ts | 111 ++++++++++++++++-------------------- 2 files changed, 54 insertions(+), 61 deletions(-) diff --git a/src/store/modules/alerts.ts b/src/store/modules/alerts.ts index c5e17c65..462cb636 100644 --- a/src/store/modules/alerts.ts +++ b/src/store/modules/alerts.ts @@ -31,3 +31,7 @@ export default { }, }, }; + +export function showAlert(dispatch, message: string, severity = '') { + dispatch(`alerts/${AlertActions.addAlert}`, { message, severity }, { root: true }); +} diff --git a/src/store/modules/ftbot.ts b/src/store/modules/ftbot.ts index 9e438293..d7cbc716 100644 --- a/src/store/modules/ftbot.ts +++ b/src/store/modules/ftbot.ts @@ -1,7 +1,7 @@ import { api } from '@/shared/apiService'; import { BotState, BlacklistPayload, ForcebuyPayload, Logs, DailyPayload, Trade } from '@/types'; -import { AlertActions } from './alerts'; +import { showAlert } from './alerts'; export enum UserStoreGetters { openTrades = 'openTrades', @@ -164,44 +164,60 @@ export default { }, // Post methods // TODO: Migrate calls to API to a seperate module unrelated to vuex? - startBot() { - return api.post('/start', {}).catch(console.error); + async startBot({ dispatch }) { + try { + const res = await api.post('/start', {}); + console.log(res.data); + showAlert(dispatch, res.data.status); + return Promise.resolve(res); + } catch (error) { + console.error(error.resposne); + showAlert(dispatch, 'Error starting bot.'); + return Promise.reject(error); + } }, - stopBot() { - return api.post('/stop', {}).catch(console.error); + async stopBot({ dispatch }) { + try { + const res = await api.post('/stop', {}); + showAlert(dispatch, res.data.status); + return Promise.resolve(res); + } catch (error) { + console.error(error.resposne); + showAlert(dispatch, 'Error stopping bot.'); + return Promise.reject(error); + } }, - stopBuy() { - return api.post('/stopbuy', {}).catch(console.error); + async stopBuy({ dispatch }) { + try { + const res = await api.post('/stopbuy', {}); + showAlert(dispatch, res.data.status); + return Promise.resolve(res); + } catch (error) { + console.error(error.resposne); + showAlert(dispatch, 'Error calling stopbuy.'); + return Promise.reject(error); + } }, async reloadConfig({ dispatch }) { try { const res = await api.post('/reload_config', {}); console.log(res.data); - dispatch(AlertActions.addAlert, { message: res.data.status }, { root: true }); + showAlert(dispatch, res.data.status); return Promise.resolve(res); } catch (error) { console.error(error.resposne); - dispatch(AlertActions.addAlert, { message: 'Error reloading ' }, { root: true }); - + showAlert(dispatch, 'Error reloading.'); return Promise.reject(error); } }, async deleteTrade({ dispatch }, tradeid: string) { try { const res = await api.delete(`/trades/${tradeid}`); - dispatch( - AlertActions.addAlert, - { message: res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}` }, - { root: true }, - ); + showAlert(dispatch, res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}`); return Promise.resolve(res); } catch (error) { console.error(error.response); - dispatch( - AlertActions.addAlert, - { message: `Failed to delete trade ${tradeid}`, severity: 'danger' }, - { root: true }, - ); + showAlert(dispatch, `Failed to delete trade ${tradeid}`, 'danger'); return Promise.reject(error); } }, @@ -210,19 +226,11 @@ export default { const payload = { tradeid }; try { const res = await api.post('/forcesell', payload); - dispatch( - AlertActions.addAlert, - { message: `Sell order for ${tradeid} created` }, - { root: true }, - ); + showAlert(dispatch, `Sell order for ${tradeid} created`); return Promise.resolve(res); } catch (error) { console.error(error.response); - dispatch( - AlertActions.addAlert, - { message: `Failed to create sell order for ${tradeid}`, severity: 'danger' }, - { root: true }, - ); + showAlert(dispatch, `Failed to create sell order for ${tradeid}`, 'danger'); return Promise.reject(error); } } @@ -235,22 +243,12 @@ export default { if (payload && payload.pair) { try { const res = await api.post('/forcebuy', payload); - dispatch( - AlertActions.addAlert, - { message: `Buy order for ${payload.pair} created.` }, - { root: true }, - ); + showAlert(dispatch, `Buy order for ${payload.pair} created.`); + return Promise.resolve(res); } catch (error) { console.error(error.response); - dispatch( - AlertActions.addAlert, - { - message: `Error occured buying: '${error.response.data.error}'`, - severity: 'danger', - }, - { root: true }, - ); + showAlert(dispatch, `Error occured buying: '${error.response.data.error}'`, 'danger'); return Promise.reject(error); } } @@ -268,32 +266,23 @@ export default { if (result.data.errors && Object.keys(result.data.errors).length !== 0) { const { errors } = result.data; Object.keys(errors).forEach((pair) => { - dispatch( - AlertActions.addAlert, - { - message: `Error while adding pair ${pair} to Blacklist: ${errors[pair].error_msg}`, - }, - { root: true }, + showAlert( + dispatch, + `Error while adding pair ${pair} to Blacklist: ${errors[pair].error_msg}`, ); }); } else { - dispatch( - AlertActions.addAlert, - { message: `Pair ${payload.blacklist} added.` }, - { root: true }, - ); + showAlert(dispatch, `Pair ${payload.blacklist} added.`); } return Promise.resolve(result.data); } catch (error) { console.error(error.response); - dispatch( - AlertActions.addAlert, - { - message: `Error occured while adding pairs to Blacklist: '${error.response.data.error}'`, - severity: 'danger', - }, - { root: true }, + showAlert( + dispatch, + `Error occured while adding pairs to Blacklist: '${error.response.data.error}'`, + 'danger', ); + return Promise.reject(error); } }