mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 04:40:32 +00:00
fix(exchangeRate): fix pagination & add sorting.
This commit is contained in:
@@ -1,116 +1,8 @@
|
||||
import ApiService from 'services/ApiService';
|
||||
import t from 'store/types';
|
||||
|
||||
export const fetchExchangeRates = () => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATE_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: true,
|
||||
},
|
||||
});
|
||||
|
||||
ApiService.get('exchange_rates')
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATES_PAGE_SET,
|
||||
payload: {
|
||||
exchange_rates: response.data.exchange_rates.results,
|
||||
pagination: response.data.exchange_rates.pagination,
|
||||
customViewId: response.data.exchange_rates.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATE_LIST_SET,
|
||||
exchange_rates: response.data.exchange_rates.results,
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATES_PAGINATION_SET,
|
||||
payload: {
|
||||
pagination: response.data.exchange_rates.pagination,
|
||||
customViewId: response.data.customViewId || -1,
|
||||
},
|
||||
});
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATE_TABLE_LOADING,
|
||||
payload: {
|
||||
loading: false,
|
||||
},
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const submitExchangeRate = ({ form }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post('exchange_rates', form)
|
||||
.then((response) => {
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteExchangeRate = (id) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.delete(`exchange_rates/${id}`)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.EXCHANGE_RATE_DELETE, id });
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error.response.data.errors || []);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const editExchangeRate = (id, form) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.post(`exchange_rates/${id}`, form)
|
||||
.then((response) => {
|
||||
dispatch({ type: t.CLEAR_EXCHANGE_RATE_FORM_ERRORS });
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
const { response } = error;
|
||||
const { data } = response;
|
||||
const { errors } = data;
|
||||
|
||||
dispatch({ type: t.CLEAR_EXCHANGE_RATE_FORM_ERRORS });
|
||||
if (errors) {
|
||||
dispatch({ type: t.CLEAR_EXCHANGE_RATE_FORM_ERRORS, errors });
|
||||
}
|
||||
reject(data?.errors);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export const deleteBulkExchangeRates = ({ ids }) => {
|
||||
return (dispatch) =>
|
||||
new Promise((resolve, reject) => {
|
||||
ApiService.delete(`exchange_rates/bulk`, { params: { ids } })
|
||||
.then((response) => {
|
||||
dispatch({
|
||||
type: t.EXCHANGE_RATES_BULK_DELETE,
|
||||
payload: { ids },
|
||||
});
|
||||
resolve(response);
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
export const setExchangeRateTableState = (queries) => {
|
||||
return {
|
||||
type: t.EXCHANGE_RATES_TABLE_STATE_SET,
|
||||
payload: { queries },
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,88 +1,13 @@
|
||||
import { createReducer } from '@reduxjs/toolkit';
|
||||
import { createTableQueryReducers } from 'store/queryReducers';
|
||||
import t from 'store/types';
|
||||
import { createTableStateReducers } from 'store/tableState.reducer';
|
||||
|
||||
const initialState = {
|
||||
exchangeRates: {},
|
||||
loading: false,
|
||||
views: {},
|
||||
tableQuery: {
|
||||
page_size: 5,
|
||||
page: 1,
|
||||
tableState: {
|
||||
pageSize: 12,
|
||||
pageIndex: 0,
|
||||
},
|
||||
currentViewId: -1,
|
||||
};
|
||||
|
||||
const reducer = createReducer(initialState, {
|
||||
[t.EXCHANGE_RATE_LIST_SET]: (state, action) => {
|
||||
const _exchangeRates = {};
|
||||
action.exchange_rates.forEach((exchange_rate) => {
|
||||
_exchangeRates[exchange_rate.id] = exchange_rate;
|
||||
});
|
||||
|
||||
state.exchangeRates = {
|
||||
...state.exchangeRates,
|
||||
..._exchangeRates,
|
||||
};
|
||||
},
|
||||
|
||||
[t.EXCHANGE_RATE_TABLE_LOADING]: (state, action) => {
|
||||
const { loading } = action.payload;
|
||||
state.loading = loading;
|
||||
},
|
||||
|
||||
[t.EXCHANGE_RATES_PAGE_SET]: (state, action) => {
|
||||
const { customViewId, exchange_rates, pagination } = action.payload;
|
||||
|
||||
const viewId = customViewId || -1;
|
||||
const view = state.views[viewId] || {};
|
||||
state.views[viewId] = {
|
||||
...view,
|
||||
pages: {
|
||||
...(state.views?.[viewId]?.pages || {}),
|
||||
[pagination.page]: {
|
||||
ids: exchange_rates.map((i) => i.id),
|
||||
},
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.EXCHANGE_RATES_PAGINATION_SET]: (state, action) => {
|
||||
const { pagination, customViewId } = action.payload;
|
||||
|
||||
const mapped = {
|
||||
pageSize: parseInt(pagination.pageSize, 10),
|
||||
page: parseInt(pagination.page, 10),
|
||||
total: parseInt(pagination.total, 10),
|
||||
};
|
||||
const paginationMeta = {
|
||||
...mapped,
|
||||
pagesCount: Math.ceil(mapped.total / mapped.pageSize),
|
||||
pageIndex: Math.max(mapped.page - 1, 0),
|
||||
};
|
||||
|
||||
state.views = {
|
||||
...state.views,
|
||||
[customViewId]: {
|
||||
...(state.views?.[customViewId] || {}),
|
||||
paginationMeta,
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
[t.EXCHANGE_RATES_BULK_DELETE]: (state, action) => {
|
||||
const { ids } = action.payload;
|
||||
ids.forEach((id) => {
|
||||
if (typeof state.exchangeRates[id] !== 'undefined') {
|
||||
delete state.exchangeRates[id];
|
||||
}
|
||||
});
|
||||
},
|
||||
[t.EXCHANGE_RATE_DELETE]: (state, action) => {
|
||||
if (typeof state.exchangeRates[action.id] !== 'undefined') {
|
||||
delete state.exchangeRates[action.id];
|
||||
}
|
||||
},
|
||||
export default createReducer(initialState, {
|
||||
...createTableStateReducers('EXCHANGE_RATES'),
|
||||
});
|
||||
|
||||
export default createTableQueryReducers('exchange_rates', reducer);
|
||||
|
||||
@@ -1,47 +1,18 @@
|
||||
import { createSelector } from 'reselect';
|
||||
import {
|
||||
pickItemsFromIds,
|
||||
getItemById,
|
||||
paginationLocationQuery,
|
||||
} from 'store/selectors';
|
||||
import { createDeepEqualSelector } from 'utils';
|
||||
import { paginationLocationQuery } from 'store/selectors';
|
||||
|
||||
const exchangeRateItemsSelector = (state) => state.exchangeRates.exchangeRates;
|
||||
const exchangeRateIdPropSelector = (state, props) => props.exchangeRateId;
|
||||
const exchangeRateTableQuery = (state) => state.exchangeRates.tableQuery;
|
||||
|
||||
const exchangeRatesCurrentViewSelector = (state, props) => {
|
||||
const viewId = state.exchangeRates.currentViewId;
|
||||
return state.exchangeRates.views?.[viewId];
|
||||
const exchangeRateTableState = (state) => {
|
||||
return state.exchangeRates.tableState;
|
||||
};
|
||||
|
||||
export const getExchangeRatesList = createSelector(
|
||||
exchangeRateItemsSelector,
|
||||
(exchangeRateItems) => {
|
||||
return Object.values(exchangeRateItems);
|
||||
},
|
||||
);
|
||||
|
||||
export const getExchangeRateById = createSelector(
|
||||
exchangeRateItemsSelector,
|
||||
exchangeRateIdPropSelector,
|
||||
(exchangeRates, exchangeRateId) => {
|
||||
return getItemById(exchangeRates, exchangeRateId);
|
||||
},
|
||||
);
|
||||
|
||||
export const getExchangeRatePaginationMetaFactory = () =>
|
||||
createSelector(exchangeRatesCurrentViewSelector, (exchangeRateView) => {
|
||||
return exchangeRateView?.paginationMeta || {};
|
||||
});
|
||||
|
||||
export const getExchangeRatesTableQueryFactory = () =>
|
||||
createSelector(
|
||||
export const getExchangeRatesTableStateFactory = () =>
|
||||
createDeepEqualSelector(
|
||||
paginationLocationQuery,
|
||||
exchangeRateTableQuery,
|
||||
(locationQuery, tableQuery) => {
|
||||
exchangeRateTableState,
|
||||
(locationQuery, tableState) => {
|
||||
return {
|
||||
...locationQuery,
|
||||
...tableQuery,
|
||||
...tableState,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
export default {
|
||||
EXCHANGE_RATE_DATA_TABLE: 'EXCHANGE_RATE_DATA_TABLE',
|
||||
EXCHANGE_RATE_DELETE: 'EXCHANGE_RATE_DELETE',
|
||||
EXCHANGE_RATE_LIST_SET: 'EXCHANGE_RATE_LIST_SET',
|
||||
CLEAR_EXCHANGE_RATE_FORM_ERRORS: 'CLEAR_EXCHANGE_RATE_FORM_ERRORS',
|
||||
ExchangeRates_TABLE_QUERIES_ADD: 'ExchangeRates_TABLE_QUERIES_ADD',
|
||||
EXCHANGE_RATE_TABLE_LOADING: 'EXCHANGE_RATE_TABLE_LOADING',
|
||||
EXCHANGE_RATES_BULK_DELETE: 'EXCHANGE_RATES_BULK_DELETE',
|
||||
EXCHANGE_RATES_PAGE_SET: 'EXCHANGE_RATES_PAGE_SET',
|
||||
EXCHANGE_RATES_PAGINATION_SET: 'EXCHANGE_RATES_PAGINATION_SET',
|
||||
EXCHANGE_RATES_TABLE_STATE_SET: 'EXCHANGE_RATES/TABLE_STATE_SET',
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user