Move alert showing to helper method

This commit is contained in:
Matthias 2020-09-05 17:08:48 +02:00
parent 1355d8e8a6
commit 7326f3f3c2
2 changed files with 54 additions and 61 deletions

View File

@ -31,3 +31,7 @@ export default {
}, },
}, },
}; };
export function showAlert(dispatch, message: string, severity = '') {
dispatch(`alerts/${AlertActions.addAlert}`, { message, severity }, { root: true });
}

View File

@ -1,7 +1,7 @@
import { api } from '@/shared/apiService'; import { api } from '@/shared/apiService';
import { BotState, BlacklistPayload, ForcebuyPayload, Logs, DailyPayload, Trade } from '@/types'; import { BotState, BlacklistPayload, ForcebuyPayload, Logs, DailyPayload, Trade } from '@/types';
import { AlertActions } from './alerts'; import { showAlert } from './alerts';
export enum UserStoreGetters { export enum UserStoreGetters {
openTrades = 'openTrades', openTrades = 'openTrades',
@ -164,44 +164,60 @@ export default {
}, },
// Post methods // Post methods
// TODO: Migrate calls to API to a seperate module unrelated to vuex? // TODO: Migrate calls to API to a seperate module unrelated to vuex?
startBot() { async startBot({ dispatch }) {
return api.post('/start', {}).catch(console.error); 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() { async stopBot({ dispatch }) {
return api.post('/stop', {}).catch(console.error); 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() { async stopBuy({ dispatch }) {
return api.post('/stopbuy', {}).catch(console.error); 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 }) { async reloadConfig({ dispatch }) {
try { try {
const res = await api.post('/reload_config', {}); const res = await api.post('/reload_config', {});
console.log(res.data); console.log(res.data);
dispatch(AlertActions.addAlert, { message: res.data.status }, { root: true }); showAlert(dispatch, res.data.status);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
console.error(error.resposne); console.error(error.resposne);
dispatch(AlertActions.addAlert, { message: 'Error reloading ' }, { root: true }); showAlert(dispatch, 'Error reloading.');
return Promise.reject(error); return Promise.reject(error);
} }
}, },
async deleteTrade({ dispatch }, tradeid: string) { async deleteTrade({ dispatch }, tradeid: string) {
try { try {
const res = await api.delete(`/trades/${tradeid}`); const res = await api.delete(`/trades/${tradeid}`);
dispatch( showAlert(dispatch, res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}`);
AlertActions.addAlert,
{ message: res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}` },
{ root: true },
);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
console.error(error.response); console.error(error.response);
dispatch( showAlert(dispatch, `Failed to delete trade ${tradeid}`, 'danger');
AlertActions.addAlert,
{ message: `Failed to delete trade ${tradeid}`, severity: 'danger' },
{ root: true },
);
return Promise.reject(error); return Promise.reject(error);
} }
}, },
@ -210,19 +226,11 @@ export default {
const payload = { tradeid }; const payload = { tradeid };
try { try {
const res = await api.post('/forcesell', payload); const res = await api.post('/forcesell', payload);
dispatch( showAlert(dispatch, `Sell order for ${tradeid} created`);
AlertActions.addAlert,
{ message: `Sell order for ${tradeid} created` },
{ root: true },
);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
console.error(error.response); console.error(error.response);
dispatch( showAlert(dispatch, `Failed to create sell order for ${tradeid}`, 'danger');
AlertActions.addAlert,
{ message: `Failed to create sell order for ${tradeid}`, severity: 'danger' },
{ root: true },
);
return Promise.reject(error); return Promise.reject(error);
} }
} }
@ -235,22 +243,12 @@ export default {
if (payload && payload.pair) { if (payload && payload.pair) {
try { try {
const res = await api.post('/forcebuy', payload); const res = await api.post('/forcebuy', payload);
dispatch( showAlert(dispatch, `Buy order for ${payload.pair} created.`);
AlertActions.addAlert,
{ message: `Buy order for ${payload.pair} created.` },
{ root: true },
);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
console.error(error.response); console.error(error.response);
dispatch( showAlert(dispatch, `Error occured buying: '${error.response.data.error}'`, 'danger');
AlertActions.addAlert,
{
message: `Error occured buying: '${error.response.data.error}'`,
severity: 'danger',
},
{ root: true },
);
return Promise.reject(error); return Promise.reject(error);
} }
} }
@ -268,32 +266,23 @@ export default {
if (result.data.errors && Object.keys(result.data.errors).length !== 0) { if (result.data.errors && Object.keys(result.data.errors).length !== 0) {
const { errors } = result.data; const { errors } = result.data;
Object.keys(errors).forEach((pair) => { Object.keys(errors).forEach((pair) => {
dispatch( showAlert(
AlertActions.addAlert, dispatch,
{ `Error while adding pair ${pair} to Blacklist: ${errors[pair].error_msg}`,
message: `Error while adding pair ${pair} to Blacklist: ${errors[pair].error_msg}`,
},
{ root: true },
); );
}); });
} else { } else {
dispatch( showAlert(dispatch, `Pair ${payload.blacklist} added.`);
AlertActions.addAlert,
{ message: `Pair ${payload.blacklist} added.` },
{ root: true },
);
} }
return Promise.resolve(result.data); return Promise.resolve(result.data);
} catch (error) { } catch (error) {
console.error(error.response); console.error(error.response);
dispatch( showAlert(
AlertActions.addAlert, dispatch,
{ `Error occured while adding pairs to Blacklist: '${error.response.data.error}'`,
message: `Error occured while adding pairs to Blacklist: '${error.response.data.error}'`, 'danger',
severity: 'danger',
},
{ root: true },
); );
return Promise.reject(error); return Promise.reject(error);
} }
} }