2019-05-22 14:59:57 +00:00
|
|
|
import { createAction } from 'redux-actions';
|
2020-05-22 14:06:05 +00:00
|
|
|
import i18next from 'i18next';
|
2019-09-05 09:12:58 +00:00
|
|
|
import apiClient from '../api/Api';
|
2020-05-22 14:06:05 +00:00
|
|
|
import { getClients } from './index';
|
|
|
|
import { addErrorToast, addSuccessToast } from './toasts';
|
2019-05-22 14:59:57 +00:00
|
|
|
|
|
|
|
export const toggleClientModal = createAction('TOGGLE_CLIENT_MODAL');
|
|
|
|
|
|
|
|
export const addClientRequest = createAction('ADD_CLIENT_REQUEST');
|
|
|
|
export const addClientFailure = createAction('ADD_CLIENT_FAILURE');
|
|
|
|
export const addClientSuccess = createAction('ADD_CLIENT_SUCCESS');
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const addClient = (config) => async (dispatch) => {
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(addClientRequest());
|
|
|
|
try {
|
2019-11-28 11:47:06 +00:00
|
|
|
await apiClient.addClient(config);
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(addClientSuccess());
|
|
|
|
dispatch(toggleClientModal());
|
2020-05-22 14:06:05 +00:00
|
|
|
dispatch(addSuccessToast(i18next.t('client_added', { key: config.name })));
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(getClients());
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(addClientFailure());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const deleteClientRequest = createAction('DELETE_CLIENT_REQUEST');
|
|
|
|
export const deleteClientFailure = createAction('DELETE_CLIENT_FAILURE');
|
|
|
|
export const deleteClientSuccess = createAction('DELETE_CLIENT_SUCCESS');
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const deleteClient = (config) => async (dispatch) => {
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(deleteClientRequest());
|
|
|
|
try {
|
|
|
|
await apiClient.deleteClient(config);
|
|
|
|
dispatch(deleteClientSuccess());
|
2020-05-22 14:06:05 +00:00
|
|
|
dispatch(addSuccessToast(i18next.t('client_deleted', { key: config.name })));
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(getClients());
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(deleteClientFailure());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateClientRequest = createAction('UPDATE_CLIENT_REQUEST');
|
|
|
|
export const updateClientFailure = createAction('UPDATE_CLIENT_FAILURE');
|
|
|
|
export const updateClientSuccess = createAction('UPDATE_CLIENT_SUCCESS');
|
|
|
|
|
|
|
|
export const updateClient = (config, name) => async (dispatch) => {
|
|
|
|
dispatch(updateClientRequest());
|
|
|
|
try {
|
2019-11-28 11:47:06 +00:00
|
|
|
const data = { name, data: { ...config } };
|
2019-05-22 14:59:57 +00:00
|
|
|
|
|
|
|
await apiClient.updateClient(data);
|
|
|
|
dispatch(updateClientSuccess());
|
|
|
|
dispatch(toggleClientModal());
|
2020-05-22 14:06:05 +00:00
|
|
|
dispatch(addSuccessToast(i18next.t('client_updated', { key: name })));
|
2019-05-22 14:59:57 +00:00
|
|
|
dispatch(getClients());
|
|
|
|
} catch (error) {
|
|
|
|
dispatch(addErrorToast({ error }));
|
|
|
|
dispatch(updateClientFailure());
|
|
|
|
}
|
|
|
|
};
|