Merge remote-tracking branch 'origin/feature/Currencies'

This commit is contained in:
Ahmed Bouhuolia
2020-04-26 03:09:48 +02:00
13 changed files with 477 additions and 37 deletions

View File

@@ -1,26 +1,51 @@
import ApiService from "services/ApiService";
import ApiService from 'services/ApiService';
import t from 'store/types';
export const fetchCurrencies = () => {
return (dispatch) => new Promise((resolve, reject) => {
ApiService.get(`currencies/registered`).then((response) => {
dispatch({
type: t.CURRENCIES_REGISTERED_SET,
currencies: response.data.currencies,
});
resolve(response);
}).catch(error => { reject(error); });
});
export const submitCurrencies = ({ form }) => {
return (dispatch) => {
return ApiService.post('currencies', form);
};
};
export const fetchAllCurrencies = () => {
return (dispatch) => new Promise((resolve, reject) => {
ApiService.get(`currencies/all`).then((response) => {
dispatch({
type: t.CURRENCIES_ALL_SET,
currencies: response.data.currencies,
});
resolve(response);
}).catch(error => { reject(error); });
});
};
export const deleteCurrency = ({ currency_code }) => {
return (dispatch) => ApiService.delete(`currencies/${currency_code}`);
};
export const editCurrency = ({ id, form }) => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.post(`currencies/${id}`, form)
.then((response) => {
dispatch({ type: t.CLEAR_CURRENCY_FORM_ERRORS });
resolve(response);
})
.catch((error) => {
const { response } = error;
const { data } = response;
const { errors } = data;
dispatch({ type: t.CLEAR_CURRENCY_FORM_ERRORS });
if (errors) {
dispatch({ type: t.CLEAR_CURRENCY_FORM_ERRORS, errors });
}
reject(error);
});
});
};
export const fetchCurrencies = () => {
return (dispatch) =>
new Promise((resolve, reject) => {
ApiService.get('currencies')
.then((response) => {
dispatch({
type: t.CURRENCIES_REGISTERED_SET,
currencies: response.data.currencies,
});
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};

View File

@@ -1,18 +1,22 @@
import {createReducer} from '@reduxjs/toolkit'
import { createReducer } from '@reduxjs/toolkit';
import t from 'store/types';
const initialState = {
all: [],
registered: [],
preferences: {
currencies: [],
},
};
export default createReducer(initialState, {
[t.CURRENCIES_REGISTERED_SET]: (state, action) => {
state.registered = action.currencies;
},
const _currencies = {};
[t.CURRENCIES_ALL_SET]: (state, action) => {
state.all = action.currencies;
action.currencies.forEach((currency) => {
_currencies[currency.currency_code] = currency;
});
state.preferences.currencies = {
...state.preferences.currencies,
..._currencies,
};
},
});

View File

@@ -0,0 +1,3 @@
export const getCurrencyById = (items, id) => {
return items[id] || null;
};

View File

@@ -1,5 +1,4 @@
export default {
CURRENCIES_REGISTERED_SET: 'CURRENCIES_REGISTERED_SET',
CURRENCIES_ALL_SET: 'CURRENCIES_ALL_SET',
};
CLEAR_CURRENCY_FORM_ERRORS: 'CLEAR_CURRENCY_FORM_ERRORS',
};