mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 23:30:32 +00:00
re-structure to monorepo.
This commit is contained in:
70
packages/webapp/src/store/currencies/currencies.actions.tsx
Normal file
70
packages/webapp/src/store/currencies/currencies.actions.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
// @ts-nocheck
|
||||
import ApiService from '@/services/ApiService';
|
||||
import t from '@/store/types';
|
||||
|
||||
export const submitCurrencies = ({ form }) => {
|
||||
return (dispatch) => {
|
||||
return ApiService.post('currencies', form);
|
||||
};
|
||||
};
|
||||
|
||||
export const deleteCurrency = ({ currency_code }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.delete(`currencies/${currency_code}`)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.CURRENCY_CODE_DELETE, currency_code });
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
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) => {
|
||||
dispatch({
|
||||
type: t.CURRENCIES_TABLE_LOADING,
|
||||
loading: true,
|
||||
});
|
||||
ApiService.get('currencies')
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.CURRENCIES_REGISTERED_SET,
|
||||
currencies: response.data.currencies,
|
||||
});
|
||||
dispatch({
|
||||
type: t.CURRENCIES_TABLE_LOADING,
|
||||
loading: false,
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
30
packages/webapp/src/store/currencies/currencies.reducer.tsx
Normal file
30
packages/webapp/src/store/currencies/currencies.reducer.tsx
Normal file
@@ -0,0 +1,30 @@
|
||||
// @ts-nocheck
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import t from '@/store/types';
|
||||
|
||||
const initialState = {
|
||||
data: {},
|
||||
loading: false,
|
||||
};
|
||||
|
||||
export default createReducer(initialState, {
|
||||
[t.CURRENCIES_REGISTERED_SET]: (state, action) => {
|
||||
const _currencies = {};
|
||||
|
||||
action.currencies.forEach((currency) => {
|
||||
_currencies[currency.currency_code] = currency;
|
||||
});
|
||||
state.data = {
|
||||
...state.data,
|
||||
..._currencies,
|
||||
};
|
||||
},
|
||||
[t.CURRENCIES_TABLE_LOADING]: (state, action) => {
|
||||
state.loading = action.loading;
|
||||
},
|
||||
[t.CURRENCY_CODE_DELETE]: (state, action) => {
|
||||
if (typeof state.data[action.currency_code] !== 'undefined') {
|
||||
delete state.data[action.currency_code];
|
||||
}
|
||||
},
|
||||
});
|
||||
23
packages/webapp/src/store/currencies/currencies.selector.tsx
Normal file
23
packages/webapp/src/store/currencies/currencies.selector.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
// @ts-nocheck
|
||||
// @flow
|
||||
import { createSelector } from 'reselect';
|
||||
import { getItemById } from '@/store/selectors';
|
||||
|
||||
const currenciesItemsSelector = (state) => state.currencies.data;
|
||||
const currenciesCodePropSelector = (state, props) => props.currencyId;
|
||||
|
||||
export const getCurrenciesList = createSelector(
|
||||
currenciesItemsSelector,
|
||||
(currencies) => {
|
||||
return Object.values(currencies);
|
||||
},
|
||||
);
|
||||
|
||||
export const getCurrencyByCode = createSelector(
|
||||
currenciesItemsSelector,
|
||||
currenciesCodePropSelector,
|
||||
(currencies, currencyCode) => {
|
||||
return getItemById(currencies, currencyCode);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
CURRENCIES_REGISTERED_SET: 'CURRENCIES_REGISTERED_SET',
|
||||
CLEAR_CURRENCY_FORM_ERRORS: 'CLEAR_CURRENCY_FORM_ERRORS',
|
||||
CURRENCIES_TABLE_LOADING: 'CURRENCIES_TABLE_LOADING',
|
||||
CURRENCY_CODE_DELETE: 'CURRENCY_CODE_DELETE',
|
||||
};
|
||||
Reference in New Issue
Block a user