feat: fix a bunch of bugs.

This commit is contained in:
Ahmed Bouhuolia
2020-04-28 04:01:10 +02:00
parent 6d0ad42582
commit 0cfa1126c5
41 changed files with 591 additions and 187 deletions

View File

@@ -1,3 +1,4 @@
import { omit } from 'lodash';
import ApiService from 'services/ApiService';
import t from 'store/types';
@@ -41,14 +42,20 @@ export const fetchAccountsList = ({ query } = {}) => {
export const fetchAccountsTable = ({ query } = {}) => {
return (dispatch, getState) =>
new Promise((resolve, reject) => {
const pageQuery = getState().accounts.tableQuery;
let pageQuery = getState().accounts.tableQuery;
if (pageQuery.filter_roles) {
pageQuery = {
...omit(pageQuery, ['filter_roles']),
stringified_filter_roles: JSON.stringify(pageQuery.filter_roles) || '',
};
}
dispatch({
type: t.ACCOUNTS_TABLE_LOADING,
loading: true,
});
ApiService.get('accounts', { params: { ...pageQuery, ...query } })
.then(response => {
.then((response) => {
dispatch({
type: t.ACCOUNTS_PAGE_SET,
accounts: response.data.accounts,
@@ -64,7 +71,7 @@ export const fetchAccountsTable = ({ query } = {}) => {
});
resolve(response);
})
.catch(error => {
.catch((error) => {
reject(error);
});
});
@@ -89,9 +96,12 @@ export const fetchAccountsDataTable = ({ query }) => {
export const submitAccount = ({ form }) => {
return dispatch =>
new Promise((resolve, reject) => {
ApiService.post('accounts', form)
.then(response => {
dispatch({ type: t.CLEAR_ACCOUNT_FORM_ERRORS });
dispatch({
type: t.ACCOUNT_ERRORS_CLEAR,
});
resolve(response);
})
.catch(error => {
@@ -99,11 +109,16 @@ export const submitAccount = ({ form }) => {
const { data } = response;
const { errors } = data;
dispatch({ type: t.CLEAR_ACCOUNT_FORM_ERRORS });
dispatch({
type: t.ACCOUNT_ERRORS_CLEAR,
});
if (errors) {
dispatch({ type: t.ACCOUNT_FORM_ERRORS, errors });
dispatch({
type: t.ACCOUNT_ERRORS_SET,
payload: { errors },
});
}
reject(error);
reject(errors);
});
});
};
@@ -125,7 +140,7 @@ export const editAccount = ({ id, form }) => {
if (errors) {
dispatch({ type: t.ACCOUNT_FORM_ERRORS, errors });
}
reject(error);
reject(errors);
});
});
};
@@ -143,11 +158,25 @@ export const deleteAccount = ({ id }) => {
ApiService.delete(`accounts/${id}`).then((response) => {
dispatch({ type: t.ACCOUNT_DELETE, id });
resolve(response);
}).catch(error => { reject(error); });
}).catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const deleteBulkAccounts = ({ ids }) => {};
export const deleteBulkAccounts = ({ ids }) => {
return dispatch => new Promise((resolve, reject) => {
ApiService.delete(`accounts`, { params: { ids }}).then((response) => {
dispatch({
type: t.ACCOUNTS_BULK_DELETE,
payload: { ids }
});
resolve(response);
}).catch((error) => {
reject(error.response.data.errors || []);
});
});
};
export const fetchAccount = ({ id }) => {
return dispatch =>

View File

@@ -7,11 +7,11 @@ const initialState = {
views: {},
accountsTypes: [],
accountsById: {},
accountFormErrors: [],
datatableQuery: {},
tableQuery: {},
currentViewId: -1,
selectedRows: [],
loading: false,
errors: [],
};
const accountsReducer = createReducer(initialState, {
@@ -34,8 +34,7 @@ const accountsReducer = createReducer(initialState, {
state.views[viewId] = {
...view,
ids: action.accounts.map(i => i.id),
};
state.accounts = action.accounts;
};
},
[t.ACCOUNT_TYPES_LIST_SET]: (state, action) => {
@@ -53,7 +52,8 @@ const accountsReducer = createReducer(initialState, {
},
[t.ACCOUNTS_SELECTED_ROWS_SET]: (state, action) => {
state.selectedRows.push(...action.ids);
const { ids } = action.payload;
state.selectedRows = [];
},
[t.ACCOUNTS_SET_CURRENT_VIEW]: (state, action) => {
@@ -63,6 +63,27 @@ const accountsReducer = createReducer(initialState, {
[t.ACCOUNTS_TABLE_LOADING]: (state, action) => {
state.loading = action.loading;
},
[t.ACCOUNT_ERRORS_SET]: (state, action) => {
const { errors } = action.payload;
state.errors = errors;
},
[t.ACCOUNT_ERRORS_CLEAR]: (state, action) => {
state.errors = [];
},
[t.ACCOUNTS_BULK_DELETE]: (state, action) => {
const { ids } = action.payload;
const items = { ...state.items };
ids.forEach((id) => {
if (typeof items[id] !== 'undefined') {
delete items[id];
}
});
state.items = items;
},
});
export default createTableQueryReducers('accounts', accountsReducer);

View File

@@ -1,7 +1,6 @@
import { pickItemsFromIds } from 'store/selectors';
export const getAccountsItems = (state, viewId) => {
const accountsView = state.accounts.views[viewId || -1];
const accountsItems = state.accounts.items;

View File

@@ -17,4 +17,8 @@ export default {
ACCOUNTS_TABLE_QUERIES_SET: 'ACCOUNTS_TABLE_QUERIES_SET',
ACCOUNTS_TABLE_LOADING: 'ACCOUNTS_TABLE_LOADING',
ACCOUNT_ERRORS_SET: 'ACCOUNT_ERRORS_SET',
ACCOUNT_ERRORS_CLEAR: 'ACCOUNT_ERRORS_CLEAR',
ACCOUNTS_BULK_DELETE: 'ACCOUNTS_BULK_DELETE'
};

View File

@@ -7,6 +7,7 @@ const initialState = {
preferencesPageTitle: '',
dialogs: {},
topbarEditViewId: 1,
requestsLoading: 0,
};
export default createReducer(initialState, {
@@ -42,6 +43,15 @@ export default createReducer(initialState, {
[t.SET_TOPBAR_EDIT_VIEW]: (state, action) => {
state.topbarEditViewId = action.id;
},
[t.SET_DASHBOARD_REQUEST_LOADING]: (state, action) => {
state.requestsLoading = state.requestsLoading + 1;
},
[t.SET_DASHBOARD_REQUEST_COMPLETED]: (state, action) => {
const requestsLoading = state.requestsLoading - 1;
state.requestsLoading = Math.max(requestsLoading, 0);
}
});

View File

@@ -9,4 +9,6 @@ export default {
CHANGE_PREFERENCES_PAGE_TITLE: 'CHANGE_PREFERENCES_PAGE_TITLE',
ALTER_DASHBOARD_PAGE_SUBTITLE: 'ALTER_DASHBOARD_PAGE_SUBTITLE',
SET_TOPBAR_EDIT_VIEW: 'SET_TOPBAR_EDIT_VIEW',
SET_DASHBOARD_REQUEST_LOADING: 'SET_DASHBOARD_REQUEST_LOADING',
SET_DASHBOARD_REQUEST_COMPLETED: 'SET_DASHBOARD_REQUEST_COMPLETED',
};

View File

@@ -1,11 +1,11 @@
import {pick} from 'lodash';
import {pick, at} from 'lodash';
export const getItemById = (items, id) => {
return items[id] || null;
};
export const pickItemsFromIds = (items, ids) => {
return Object.values(pick(items, ids));
return at(items, ids).filter(i => i);
}
export const getCurrentPageResults = (items, pages, pageNumber) => {