diff --git a/client/src/containers/Customers/CustomerActionsBar.js b/client/src/containers/Customers/CustomerActionsBar.js index 1246ae807..aaf60526c 100644 --- a/client/src/containers/Customers/CustomerActionsBar.js +++ b/client/src/containers/Customers/CustomerActionsBar.js @@ -20,17 +20,20 @@ import FilterDropdown from 'components/FilterDropdown'; import { If, DashboardActionViewsList } from 'components'; import withResourceDetail from 'containers/Resources/withResourceDetails'; -import withDashboardActions from 'containers/Dashboard/withDashboardActions'; -import addCustomersTableQueries from 'containers/Customers/withCustomersActions'; -import { compose } from 'utils'; +import withCustomers from 'containers/Customers/withCustomers'; import withCustomersActions from 'containers/Customers/withCustomersActions'; +import { compose } from 'utils'; const CustomerActionsBar = ({ // #withResourceDetail resourceFields, + // #withCustomers + customersViews, + //#withCustomersActions addCustomersTableQueries, + changeCustomerView, // #ownProps selectedRows = [], @@ -45,16 +48,6 @@ const CustomerActionsBar = ({ history.push('/customers/new'); }, [history]); - // const filterDropdown = FilterDropdown({ - // fields: resourceFields, - // onFilterChange: (filterConditions) => { - // setFilterCount(filterConditions.length || 0); - // addCustomersTableQueries({ - // filter_roles: filterConditions || '', - // }); - // onFilterChanged && onFilterChanged(filterConditions); - // }, - // }); const hasSelectedRows = useMemo(() => selectedRows.length > 0, [ selectedRows, @@ -64,12 +57,20 @@ const CustomerActionsBar = ({ onBulkDelete && onBulkDelete(selectedRows.map((r) => r.id)); }, [onBulkDelete, selectedRows]); + const handleTabChange = (viewId) => { + changeCustomerView(viewId.id || -1); + addCustomersTableQueries({ + custom_view_id: viewId.id || null, + }); + }; + return ( @@ -133,4 +134,7 @@ export default compose( withResourceDetail(({ resourceFields }) => ({ resourceFields, })), + withCustomers(({ customersViews }) => ({ + customersViews, + })), )(CustomerActionsBar); diff --git a/client/src/containers/Customers/CustomersList.js b/client/src/containers/Customers/CustomersList.js index 6b133f8f0..9c090ff86 100644 --- a/client/src/containers/Customers/CustomersList.js +++ b/client/src/containers/Customers/CustomersList.js @@ -55,13 +55,14 @@ function CustomersList({ }, [changePageTitle, formatMessage]); // Fetch customers resource views and fields. - // const fetchResourceViews = useQuery(['resource-views', 'customers'], - // () => requestFetchResourceViews('customers') - // ); - + const fetchResourceViews = useQuery( + ['resource-views', 'customers'], + (key, resourceName) => requestFetchResourceViews(resourceName), + ); + const fetchCustomers = useQuery( ['customers-table', customersTableQuery], - () => requestFetchCustomers(), + (key, query) => requestFetchCustomers({ ...query }), ); const handleEditCustomer = useCallback( @@ -84,7 +85,7 @@ function CustomersList({ setDeleteCustomer(false); }, [setDeleteCustomer]); - const transformErrors = (errors) => { + const transformErrors = (errors) => { if (errors.some((e) => e.type === 'CUSTOMER.HAS.SALES_INVOICES')) { AppToaster.show({ message: formatMessage({ @@ -132,16 +133,6 @@ function CustomersList({ selectedRows, ]); - // Handle filter change to re-fetch the items. - const handleFilterChanged = useCallback( - (filterConditions) => { - // addCustomersTableQueries({ - // filter_roles: filterConditions || '', - // }); - }, - [addCustomersTableQueries], - ); - // Handle Customers bulk delete button click., const handleBulkDelete = useCallback( (customersIds) => { @@ -173,10 +164,12 @@ function CustomersList({ }, [requestDeleteBulkCustomers, bulkDelete, formatMessage]); return ( - + @@ -188,7 +181,6 @@ function CustomersList({ > customersViews.map((view) => ({ - ...pick(view, ['name', 'id']), - }), [customersViews])); - useEffect(() => { - setTopbarEditView(customViewId); changePageSubtitle(customViewId && viewItem ? viewItem.name : ''); - // addCustomersTableQueries({ - // custom_view_id: customViewId, - // }); - return () => { - setTopbarEditView(null); - changePageSubtitle(''); - }; + setTopbarEditView(customViewId); }, [customViewId]); + + const tabs = useMemo(() => + customersViews.map( + (view) => ({ + ...pick(view, ['name', 'id']), + }), + [customersViews], + ), + ); + + const handleTabsChange = (viewId) => { + changeCustomerView(viewId || -1); + addCustomersTableQueries({ + custom_view_id: viewId || null, + }); + setTopbarEditView(viewId); + }; return ( @@ -55,6 +62,7 @@ function CustomersViewsTabs({ initialViewId={customViewId} resourceName={'customers'} tabs={tabs} + onChange={handleTabsChange} /> @@ -72,7 +80,8 @@ export default compose( withDashboardActions, withCustomersViewsTabs, withCustomersActions, + withViewDetail(), withCustomers(({ customersViews }) => ({ customersViews, })), -)(CustomersViewsTabs); \ No newline at end of file +)(CustomersViewsTabs); diff --git a/client/src/containers/Customers/withCustomersActions.js b/client/src/containers/Customers/withCustomersActions.js index c42169ce3..a00c52240 100644 --- a/client/src/containers/Customers/withCustomersActions.js +++ b/client/src/containers/Customers/withCustomersActions.js @@ -19,8 +19,14 @@ export const mapDispatchToProps = (dispatch) => ({ addCustomersTableQueries: (queries) => dispatch({ type: t.CUSTOMERS_TABLE_QUERIES_ADD, - payload: { queries } + payload: { queries }, }), + changeCustomerView: (id) => { + dispatch({ + type: t.CUSTOMERS_SET_CURRENT_VIEW, + currentViewId: parseInt(id, 10), + }); + }, }); export default connect(null, mapDispatchToProps); diff --git a/client/src/store/customers/customers.actions.js b/client/src/store/customers/customers.actions.js index 968c1b6db..67c7b2103 100644 --- a/client/src/store/customers/customers.actions.js +++ b/client/src/store/customers/customers.actions.js @@ -48,7 +48,8 @@ export const fetchCustomers = ({ query }) => { type: t.CUSTOMERS_PAGE_SET, payload: { customers: response.data.customers, - customViewId: response.data.customViewId || -1, + customViewId: + response.data?.filter_meta?.view?.custom_view_id || -1, paginationMeta: response.data.pagination, }, }); @@ -62,7 +63,8 @@ export const fetchCustomers = ({ query }) => { type: t.CUSTOMERS_PAGINATION_SET, payload: { pagination: response.data.pagination, - customViewId: response.data.customViewId || -1, + customViewId: + response.data?.filter_meta?.view?.custom_view_id || -1, }, }); dispatch({ diff --git a/client/src/store/customers/customers.reducer.js b/client/src/store/customers/customers.reducer.js index dec6ecf15..e127d789c 100644 --- a/client/src/store/customers/customers.reducer.js +++ b/client/src/store/customers/customers.reducer.js @@ -49,6 +49,7 @@ export default createReducer(initialState, { state.views[viewId] = { ...view, pages: { + ...(state.views?.[viewId]?.pages || {}), [paginationMeta.page]: { ids: customers.map((i) => i.id), @@ -64,6 +65,10 @@ export default createReducer(initialState, { } }, + [t.CUSTOMERS_SET_CURRENT_VIEW]: (state, action) => { + state.currentViewId = action.currentViewId; + }, + [t.CUSTOMERS_TABLE_LOADING]: (state, action) => { const { loading } = action.payload; state.loading = loading; diff --git a/client/src/store/customers/customers.type.js b/client/src/store/customers/customers.type.js index 344af1bf3..c43ef801a 100644 --- a/client/src/store/customers/customers.type.js +++ b/client/src/store/customers/customers.type.js @@ -7,4 +7,5 @@ export default { CUSTOMER_DELETE: 'CUSTOMER_DELETE', CUSTOMERS_BULK_DELETE: 'CUSTOMERS_BULK_DELETE', CUSTOMERS_PAGINATION_SET: 'CUSTOMERS_PAGINATION_SET', + CUSTOMERS_SET_CURRENT_VIEW: 'CUSTOMERS_SET_CURRENT_VIEW', };