mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 15:20:34 +00:00
refactor: customers alerts.
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
import React, { useCallback, useState } from 'react';
|
||||||
|
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||||
|
import { Intent, Alert } from '@blueprintjs/core';
|
||||||
|
import { AppToaster } from 'components';
|
||||||
|
import { transformErrors } from 'containers/Customers/utils';
|
||||||
|
|
||||||
|
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||||
|
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customer bulk delete alert.
|
||||||
|
*/
|
||||||
|
function CustomerBulkDeleteAlert({
|
||||||
|
name,
|
||||||
|
|
||||||
|
// #withAlertStoreConnect
|
||||||
|
isOpen,
|
||||||
|
payload: { customersIds },
|
||||||
|
// #withCustomersActions
|
||||||
|
requestDeleteBulkCustomers,
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
closeAlert,
|
||||||
|
}) {
|
||||||
|
const { formatMessage } = useIntl();
|
||||||
|
const [isLoading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
// handle cancel delete alert.
|
||||||
|
const handleCancelDeleteAlert = () => {
|
||||||
|
closeAlert(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(customersIds, 'EE');
|
||||||
|
|
||||||
|
// Handle confirm customers bulk delete.
|
||||||
|
const handleConfirmBulkDelete = useCallback(() => {
|
||||||
|
setLoading(true);
|
||||||
|
requestDeleteBulkCustomers(customersIds)
|
||||||
|
.then(() => {
|
||||||
|
AppToaster.show({
|
||||||
|
message: formatMessage({
|
||||||
|
id: 'the_customers_has_been_deleted_successfully',
|
||||||
|
}),
|
||||||
|
intent: Intent.SUCCESS,
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((errors) => {
|
||||||
|
transformErrors(errors);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setLoading(false);
|
||||||
|
closeAlert(name);
|
||||||
|
});
|
||||||
|
}, [requestDeleteBulkCustomers, customersIds, formatMessage]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Alert
|
||||||
|
cancelButtonText={<T id={'cancel'} />}
|
||||||
|
confirmButtonText={<T id={'delete'} />}
|
||||||
|
icon="trash"
|
||||||
|
intent={Intent.DANGER}
|
||||||
|
isOpen={isOpen}
|
||||||
|
onCancel={handleCancelDeleteAlert}
|
||||||
|
onConfirm={handleConfirmBulkDelete}
|
||||||
|
loading={isLoading}
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
<T id={'once_delete_these_customers_you_will_not_able_restore_them'} />
|
||||||
|
</p>
|
||||||
|
</Alert>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(
|
||||||
|
withAlertStoreConnect(),
|
||||||
|
withAlertActions,
|
||||||
|
withCustomersActions,
|
||||||
|
)(CustomerBulkDeleteAlert);
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import React, { useCallback, useState } from 'react';
|
||||||
|
import {
|
||||||
|
FormattedMessage as T,
|
||||||
|
FormattedHTMLMessage,
|
||||||
|
useIntl,
|
||||||
|
} from 'react-intl';
|
||||||
|
import { Intent, Alert } from '@blueprintjs/core';
|
||||||
|
import { queryCache } from 'react-query';
|
||||||
|
import { AppToaster } from 'components';
|
||||||
|
import { transformErrors } from 'containers/Customers/utils';
|
||||||
|
|
||||||
|
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||||
|
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customer delete alert.
|
||||||
|
*/
|
||||||
|
function CustomerDeleteAlert({
|
||||||
|
name,
|
||||||
|
|
||||||
|
// #withAlertStoreConnect
|
||||||
|
isOpen,
|
||||||
|
payload: { customerId },
|
||||||
|
// #withCustomersActions
|
||||||
|
requestDeleteCustomer,
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
closeAlert,
|
||||||
|
}) {
|
||||||
|
const { formatMessage } = useIntl();
|
||||||
|
const [isLoading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
// handle cancel delete alert.
|
||||||
|
const handleCancelDeleteAlert = () => {
|
||||||
|
closeAlert(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
// handle confirm delete customer.
|
||||||
|
const handleConfirmDeleteCustomer = useCallback(() => {
|
||||||
|
setLoading(true);
|
||||||
|
requestDeleteCustomer(customerId)
|
||||||
|
.then(() => {
|
||||||
|
AppToaster.show({
|
||||||
|
message: formatMessage({
|
||||||
|
id: 'the_customer_has_been_deleted_successfully',
|
||||||
|
}),
|
||||||
|
intent: Intent.SUCCESS,
|
||||||
|
});
|
||||||
|
queryCache.invalidateQueries('customers-table');
|
||||||
|
})
|
||||||
|
.catch((errors) => {
|
||||||
|
transformErrors(errors);
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setLoading(false);
|
||||||
|
closeAlert(name);
|
||||||
|
});
|
||||||
|
}, [requestDeleteCustomer, customerId, formatMessage]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Alert
|
||||||
|
cancelButtonText={<T id={'cancel'} />}
|
||||||
|
confirmButtonText={<T id={'delete'} />}
|
||||||
|
icon="trash"
|
||||||
|
intent={Intent.DANGER}
|
||||||
|
isOpen={isOpen}
|
||||||
|
onCancel={handleCancelDeleteAlert}
|
||||||
|
onConfirm={handleConfirmDeleteCustomer}
|
||||||
|
loading={isLoading}
|
||||||
|
>
|
||||||
|
<p>
|
||||||
|
<FormattedHTMLMessage
|
||||||
|
id={'once_delete_this_customer_you_will_able_to_restore_it'}
|
||||||
|
/>
|
||||||
|
</p>
|
||||||
|
</Alert>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(
|
||||||
|
withAlertStoreConnect(),
|
||||||
|
withAlertActions,
|
||||||
|
withCustomersActions,
|
||||||
|
)(CustomerDeleteAlert);
|
||||||
@@ -27,6 +27,7 @@ function ItemBulkDeleteAlert({
|
|||||||
}) {
|
}) {
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const [isLoading, setLoading] = useState(false);
|
const [isLoading, setLoading] = useState(false);
|
||||||
|
|
||||||
// handle cancel item bulk delete alert.
|
// handle cancel item bulk delete alert.
|
||||||
const handleCancelBulkDelete = () => {
|
const handleCancelBulkDelete = () => {
|
||||||
closeAlert(name);
|
closeAlert(name);
|
||||||
@@ -49,7 +50,6 @@ function ItemBulkDeleteAlert({
|
|||||||
closeAlert(name);
|
closeAlert(name);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Alert
|
<Alert
|
||||||
cancelButtonText={<T id={'cancel'} />}
|
cancelButtonText={<T id={'cancel'} />}
|
||||||
@@ -61,6 +61,7 @@ function ItemBulkDeleteAlert({
|
|||||||
isOpen={isOpen}
|
isOpen={isOpen}
|
||||||
onCancel={handleCancelBulkDelete}
|
onCancel={handleCancelBulkDelete}
|
||||||
onConfirm={handleConfirmBulkDelete}
|
onConfirm={handleConfirmBulkDelete}
|
||||||
|
loading={isLoading}
|
||||||
>
|
>
|
||||||
<p>
|
<p>
|
||||||
<T id={'once_delete_these_items_you_will_not_able_restore_them'} />
|
<T id={'once_delete_these_items_you_will_not_able_restore_them'} />
|
||||||
|
|||||||
@@ -14,33 +14,31 @@ import classNames from 'classnames';
|
|||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
|
||||||
import Icon from 'components/Icon';
|
|
||||||
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
|
import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar';
|
||||||
import FilterDropdown from 'components/FilterDropdown';
|
import { If, Icon, DashboardActionViewsList } from 'components';
|
||||||
import { If, DashboardActionViewsList } from 'components';
|
|
||||||
|
|
||||||
import withResourceDetail from 'containers/Resources/withResourceDetails';
|
import withResourceDetail from 'containers/Resources/withResourceDetails';
|
||||||
import withCustomers from 'containers/Customers/withCustomers';
|
import withCustomers from 'containers/Customers/withCustomers';
|
||||||
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||||
|
import withAlertActions from 'containers/Alert/withAlertActions';
|
||||||
|
|
||||||
import { compose } from 'utils';
|
import { compose } from 'utils';
|
||||||
|
|
||||||
const CustomerActionsBar = ({
|
const CustomerActionsBar = ({
|
||||||
// #withResourceDetail
|
|
||||||
resourceFields,
|
|
||||||
|
|
||||||
// #withCustomers
|
// #withCustomers
|
||||||
customersViews,
|
customersViews,
|
||||||
|
customersSelectedRows,
|
||||||
|
|
||||||
//#withCustomersActions
|
//#withCustomersActions
|
||||||
addCustomersTableQueries,
|
addCustomersTableQueries,
|
||||||
changeCustomerView,
|
changeCustomerView,
|
||||||
|
|
||||||
|
// #withAlertActions
|
||||||
|
openAlert,
|
||||||
|
|
||||||
// #ownProps
|
// #ownProps
|
||||||
selectedRows = [],
|
|
||||||
onFilterChanged,
|
onFilterChanged,
|
||||||
onBulkDelete,
|
|
||||||
}) => {
|
}) => {
|
||||||
const [filterCount, setFilterCount] = useState(0);
|
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
|
||||||
@@ -48,14 +46,10 @@ const CustomerActionsBar = ({
|
|||||||
history.push('/customers/new');
|
history.push('/customers/new');
|
||||||
}, [history]);
|
}, [history]);
|
||||||
|
|
||||||
|
// Handle Customers bulk delete button click.,
|
||||||
const hasSelectedRows = useMemo(() => selectedRows.length > 0, [
|
const handleBulkDelete = () => {
|
||||||
selectedRows,
|
openAlert('customers-bulk-delete', { customersIds: customersSelectedRows });
|
||||||
]);
|
};
|
||||||
|
|
||||||
const handleBulkDelete = useCallback(() => {
|
|
||||||
onBulkDelete && onBulkDelete(selectedRows.map((r) => r.id));
|
|
||||||
}, [onBulkDelete, selectedRows]);
|
|
||||||
|
|
||||||
const handleTabChange = (viewId) => {
|
const handleTabChange = (viewId) => {
|
||||||
changeCustomerView(viewId.id || -1);
|
changeCustomerView(viewId.id || -1);
|
||||||
@@ -88,18 +82,12 @@ const CustomerActionsBar = ({
|
|||||||
>
|
>
|
||||||
<Button
|
<Button
|
||||||
className={classNames(Classes.MINIMAL, 'button--filter')}
|
className={classNames(Classes.MINIMAL, 'button--filter')}
|
||||||
text={
|
text={`${formatMessage({ id: 'filters_applied' })}`}
|
||||||
filterCount <= 0 ? (
|
|
||||||
<T id={'filter'} />
|
|
||||||
) : (
|
|
||||||
`${filterCount} ${formatMessage({ id: 'filters_applied' })}`
|
|
||||||
)
|
|
||||||
}
|
|
||||||
icon={<Icon icon="filter-16" iconSize={16} />}
|
icon={<Icon icon="filter-16" iconSize={16} />}
|
||||||
/>
|
/>
|
||||||
</Popover>
|
</Popover>
|
||||||
|
|
||||||
<If condition={hasSelectedRows}>
|
<If condition={customersSelectedRows.length}>
|
||||||
<Button
|
<Button
|
||||||
className={Classes.MINIMAL}
|
className={Classes.MINIMAL}
|
||||||
icon={<Icon icon="trash-16" iconSize={16} />}
|
icon={<Icon icon="trash-16" iconSize={16} />}
|
||||||
@@ -134,7 +122,9 @@ export default compose(
|
|||||||
withResourceDetail(({ resourceFields }) => ({
|
withResourceDetail(({ resourceFields }) => ({
|
||||||
resourceFields,
|
resourceFields,
|
||||||
})),
|
})),
|
||||||
withCustomers(({ customersViews }) => ({
|
withCustomers(({ customersViews, customersSelectedRows }) => ({
|
||||||
customersViews,
|
customersViews,
|
||||||
|
customersSelectedRows,
|
||||||
})),
|
})),
|
||||||
|
withAlertActions,
|
||||||
)(CustomerActionsBar);
|
)(CustomerActionsBar);
|
||||||
|
|||||||
@@ -204,7 +204,6 @@ const CustomerTable = ({
|
|||||||
noInitialFetch={true}
|
noInitialFetch={true}
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={customers}
|
data={customers}
|
||||||
// loading={customersLoading}
|
|
||||||
onFetchData={handleFetchData}
|
onFetchData={handleFetchData}
|
||||||
selectionColumn={true}
|
selectionColumn={true}
|
||||||
expandable={false}
|
expandable={false}
|
||||||
|
|||||||
15
client/src/containers/Customers/CustomersAlerts.js
Normal file
15
client/src/containers/Customers/CustomersAlerts.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import CustomerDeleteAlert from 'containers/Alerts/Customers/CustomerDeleteAlert';
|
||||||
|
import CustomerBulkDeleteAlert from 'containers/Alerts/Customers/CustomerBulkDeleteAlert';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customers alert.
|
||||||
|
*/
|
||||||
|
export default function ItemsAlerts() {
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<CustomerDeleteAlert name={'customer-delete'} />
|
||||||
|
<CustomerBulkDeleteAlert name={'customers-bulk-delete'} />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,21 +1,13 @@
|
|||||||
import React, { useEffect, useCallback, useState, useMemo } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import { Route, Switch, useHistory } from 'react-router-dom';
|
|
||||||
import { Intent, Alert } from '@blueprintjs/core';
|
|
||||||
import { useQuery } from 'react-query';
|
import { useQuery } from 'react-query';
|
||||||
import {
|
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||||
FormattedMessage as T,
|
|
||||||
FormattedHTMLMessage,
|
|
||||||
useIntl,
|
|
||||||
} from 'react-intl';
|
|
||||||
|
|
||||||
import AppToaster from 'components/AppToaster';
|
|
||||||
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
import DashboardInsider from 'components/Dashboard/DashboardInsider';
|
||||||
import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
|
import DashboardPageContent from 'components/Dashboard/DashboardPageContent';
|
||||||
|
|
||||||
import CustomersTable from 'containers/Customers/CustomerTable';
|
|
||||||
import CustomerActionsBar from 'containers/Customers/CustomerActionsBar';
|
import CustomerActionsBar from 'containers/Customers/CustomerActionsBar';
|
||||||
import CustomersViewsTabs from 'containers/Customers/CustomersViewsTabs';
|
import CustomersAlerts from 'containers/Customers/CustomersAlerts';
|
||||||
|
import CustomersViewPage from 'containers/Customers/CustomersViewPage';
|
||||||
import withCustomers from 'containers/Customers/withCustomers';
|
import withCustomers from 'containers/Customers/withCustomers';
|
||||||
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||||
import withResourceActions from 'containers/Resources/withResourcesActions';
|
import withResourceActions from 'containers/Resources/withResourcesActions';
|
||||||
@@ -35,26 +27,21 @@ function CustomersList({
|
|||||||
|
|
||||||
// #withResourceActions
|
// #withResourceActions
|
||||||
requestFetchResourceViews,
|
requestFetchResourceViews,
|
||||||
requestFetchResourceFields,
|
|
||||||
|
|
||||||
// #withCustomers
|
// #withCustomers
|
||||||
customersTableQuery,
|
customersTableQuery,
|
||||||
|
|
||||||
|
// #withAlertsActions.
|
||||||
|
openAlert,
|
||||||
|
|
||||||
// #withCustomersActions
|
// #withCustomersActions
|
||||||
requestFetchCustomers,
|
requestFetchCustomers,
|
||||||
requestDeleteCustomer,
|
|
||||||
requestDeleteBulkCustomers,
|
|
||||||
addCustomersTableQueries,
|
addCustomersTableQueries,
|
||||||
}) {
|
}) {
|
||||||
const [deleteCustomer, setDeleteCustomer] = useState(false);
|
|
||||||
const [selectedRows, setSelectedRows] = useState([]);
|
|
||||||
const [tableLoading, setTableLoading] = useState(false);
|
const [tableLoading, setTableLoading] = useState(false);
|
||||||
const [bulkDelete, setBulkDelete] = useState(false);
|
|
||||||
|
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
|
||||||
const history = useHistory();
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
changePageTitle(formatMessage({ id: 'customers_list' }));
|
changePageTitle(formatMessage({ id: 'customers_list' }));
|
||||||
}, [changePageTitle, formatMessage]);
|
}, [changePageTitle, formatMessage]);
|
||||||
@@ -70,178 +57,23 @@ function CustomersList({
|
|||||||
(key, query) => requestFetchCustomers({ ...query }),
|
(key, query) => requestFetchCustomers({ ...query }),
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleEditCustomer = useCallback(
|
|
||||||
(customer) => {
|
|
||||||
history.push(`/customers/${customer.id}/edit`);
|
|
||||||
},
|
|
||||||
[history],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Handle click delete customer.
|
|
||||||
const handleDeleteCustomer = useCallback(
|
|
||||||
(customer) => {
|
|
||||||
setDeleteCustomer(customer);
|
|
||||||
},
|
|
||||||
[setDeleteCustomer],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Handle cancel delete the customer.
|
|
||||||
const handleCancelDeleteCustomer = useCallback(() => {
|
|
||||||
setDeleteCustomer(false);
|
|
||||||
}, [setDeleteCustomer]);
|
|
||||||
|
|
||||||
const transformErrors = (errors) => {
|
|
||||||
if (errors.some((e) => e.type === 'CUSTOMER.HAS.SALES_INVOICES')) {
|
|
||||||
AppToaster.show({
|
|
||||||
message: formatMessage({
|
|
||||||
id: 'customer_has_sales_invoices',
|
|
||||||
}),
|
|
||||||
intent: Intent.DANGER,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// handle confirm delete customer.
|
|
||||||
const handleConfirmDeleteCustomer = useCallback(() => {
|
|
||||||
requestDeleteCustomer(deleteCustomer.id)
|
|
||||||
.then(() => {
|
|
||||||
setDeleteCustomer(false);
|
|
||||||
AppToaster.show({
|
|
||||||
message: formatMessage({
|
|
||||||
id: 'the_customer_has_been_deleted_successfully',
|
|
||||||
}),
|
|
||||||
intent: Intent.SUCCESS,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((errors) => {
|
|
||||||
setDeleteCustomer(false);
|
|
||||||
transformErrors(errors);
|
|
||||||
});
|
|
||||||
}, [requestDeleteCustomer, deleteCustomer, formatMessage]);
|
|
||||||
|
|
||||||
// Handle selected rows change.
|
|
||||||
const handleSelectedRowsChange = useCallback(
|
|
||||||
(customer) => {
|
|
||||||
setSelectedRows(customer);
|
|
||||||
},
|
|
||||||
[setSelectedRows],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (tableLoading && !fetchCustomers.isFetching) {
|
if (tableLoading && !fetchCustomers.isFetching) {
|
||||||
setTableLoading(false);
|
setTableLoading(false);
|
||||||
}
|
}
|
||||||
}, [tableLoading, fetchCustomers.isFetching]);
|
}, [tableLoading, fetchCustomers.isFetching]);
|
||||||
|
|
||||||
// Calculates the data table selected rows count.
|
|
||||||
const selectedRowsCount = useMemo(() => Object.values(selectedRows).length, [
|
|
||||||
selectedRows,
|
|
||||||
]);
|
|
||||||
|
|
||||||
// Handle Customers bulk delete button click.,
|
|
||||||
const handleBulkDelete = useCallback(
|
|
||||||
(customersIds) => {
|
|
||||||
setBulkDelete(customersIds);
|
|
||||||
},
|
|
||||||
[setBulkDelete],
|
|
||||||
);
|
|
||||||
|
|
||||||
// Handle cancel cusomters bulk delete.
|
|
||||||
const handleCancelBulkDelete = useCallback(() => {
|
|
||||||
setBulkDelete(false);
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const transformApiErrors = (errors) => {
|
|
||||||
if (
|
|
||||||
errors.find(
|
|
||||||
(error) => error.type === 'SOME.CUSTOMERS.HAVE.SALES_INVOICES',
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
AppToaster.show({
|
|
||||||
message: formatMessage({
|
|
||||||
id: 'some_customers_have_sales_invoices',
|
|
||||||
}),
|
|
||||||
intent: Intent.DANGER,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// Handle confirm customers bulk delete.
|
|
||||||
const handleConfirmBulkDelete = useCallback(() => {
|
|
||||||
requestDeleteBulkCustomers(bulkDelete)
|
|
||||||
.then(() => {
|
|
||||||
setBulkDelete(false);
|
|
||||||
AppToaster.show({
|
|
||||||
message: formatMessage({
|
|
||||||
id: 'the_customers_has_been_deleted_successfully',
|
|
||||||
}),
|
|
||||||
intent: Intent.SUCCESS,
|
|
||||||
});
|
|
||||||
})
|
|
||||||
.catch((errors) => {
|
|
||||||
transformApiErrors(errors);
|
|
||||||
setBulkDelete(false);
|
|
||||||
});
|
|
||||||
}, [requestDeleteBulkCustomers, bulkDelete, formatMessage]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DashboardInsider
|
<DashboardInsider
|
||||||
loading={fetchResourceViews.isFetching}
|
loading={fetchResourceViews.isFetching}
|
||||||
name={'customers-list'}
|
name={'customers-list'}
|
||||||
>
|
>
|
||||||
<CustomerActionsBar
|
<CustomerActionsBar />
|
||||||
selectedRows={selectedRows}
|
|
||||||
onBulkDelete={handleBulkDelete}
|
|
||||||
/>
|
|
||||||
|
|
||||||
<DashboardPageContent>
|
<DashboardPageContent>
|
||||||
<Switch>
|
<CustomersViewPage />
|
||||||
<Route
|
|
||||||
exact={true}
|
|
||||||
path={['/customers/:custom_view_id/custom_view', '/customers']}
|
|
||||||
>
|
|
||||||
<CustomersViewsTabs />
|
|
||||||
<CustomersTable
|
|
||||||
onDeleteCustomer={handleDeleteCustomer}
|
|
||||||
onEditCustomer={handleEditCustomer}
|
|
||||||
onSelectedRowsChange={handleSelectedRowsChange}
|
|
||||||
/>
|
|
||||||
</Route>
|
|
||||||
</Switch>
|
|
||||||
|
|
||||||
<Alert
|
|
||||||
cancelButtonText={<T id={'cancel'} />}
|
|
||||||
confirmButtonText={<T id={'delete'} />}
|
|
||||||
icon="trash"
|
|
||||||
intent={Intent.DANGER}
|
|
||||||
isOpen={deleteCustomer}
|
|
||||||
onCancel={handleCancelDeleteCustomer}
|
|
||||||
onConfirm={handleConfirmDeleteCustomer}
|
|
||||||
>
|
|
||||||
<p>
|
|
||||||
<FormattedHTMLMessage
|
|
||||||
id={'once_delete_this_customer_you_will_able_to_restore_it'}
|
|
||||||
/>
|
|
||||||
</p>
|
|
||||||
</Alert>
|
|
||||||
|
|
||||||
<Alert
|
|
||||||
cancelButtonText={<T id={'cancel'} />}
|
|
||||||
confirmButtonText={`${formatMessage({
|
|
||||||
id: 'delete',
|
|
||||||
})} (${selectedRowsCount})`}
|
|
||||||
icon="trash"
|
|
||||||
intent={Intent.DANGER}
|
|
||||||
isOpen={bulkDelete}
|
|
||||||
onCancel={handleCancelBulkDelete}
|
|
||||||
onConfirm={handleConfirmBulkDelete}
|
|
||||||
>
|
|
||||||
<p>
|
|
||||||
<T
|
|
||||||
id={'once_delete_these_customers_you_will_not_able_restore_them'}
|
|
||||||
/>
|
|
||||||
</p>
|
|
||||||
</Alert>
|
|
||||||
</DashboardPageContent>
|
</DashboardPageContent>
|
||||||
|
<CustomersAlerts />
|
||||||
</DashboardInsider>
|
</DashboardInsider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
61
client/src/containers/Customers/CustomersViewPage.js
Normal file
61
client/src/containers/Customers/CustomersViewPage.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
import React, { useCallback } from 'react';
|
||||||
|
import { Route, Switch, useHistory } from 'react-router-dom';
|
||||||
|
|
||||||
|
import CustomersViewsTabs from 'containers/Customers/CustomersViewsTabs';
|
||||||
|
import CustomersTable from 'containers/Customers/CustomerTable';
|
||||||
|
|
||||||
|
import withCustomersActions from 'containers/Customers/withCustomersActions';
|
||||||
|
import withAlertsActions from 'containers/Alert/withAlertActions';
|
||||||
|
import { compose } from 'utils';
|
||||||
|
|
||||||
|
function CustomersViewPage({
|
||||||
|
// #withAlertsActions.
|
||||||
|
openAlert,
|
||||||
|
|
||||||
|
// #withCustomersActions
|
||||||
|
setSelectedRowsCustomers,
|
||||||
|
}) {
|
||||||
|
const history = useHistory();
|
||||||
|
|
||||||
|
// Handle click delete customer.
|
||||||
|
const handleDeleteCustomer = useCallback(
|
||||||
|
({ id }) => {
|
||||||
|
openAlert('customer-delete', { customerId: id });
|
||||||
|
},
|
||||||
|
[openAlert],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Handle select customer rows.
|
||||||
|
const handleSelectedRowsChange = (selectedRows) => {
|
||||||
|
const selectedRowsIds = selectedRows.map((r) => r.id);
|
||||||
|
setSelectedRowsCustomers(selectedRowsIds);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEditCustomer = useCallback(
|
||||||
|
(customer) => {
|
||||||
|
history.push(`/customers/${customer.id}/edit`);
|
||||||
|
},
|
||||||
|
[history],
|
||||||
|
);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Switch>
|
||||||
|
<Route
|
||||||
|
exact={true}
|
||||||
|
path={['/customers/:custom_view_id/custom_view', '/customers']}
|
||||||
|
>
|
||||||
|
<CustomersViewsTabs />
|
||||||
|
<CustomersTable
|
||||||
|
onDeleteCustomer={handleDeleteCustomer}
|
||||||
|
onEditCustomer={handleEditCustomer}
|
||||||
|
onSelectedRowsChange={handleSelectedRowsChange}
|
||||||
|
/>
|
||||||
|
</Route>
|
||||||
|
</Switch>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default compose(
|
||||||
|
withAlertsActions,
|
||||||
|
withCustomersActions,
|
||||||
|
)(CustomersViewPage);
|
||||||
25
client/src/containers/Customers/utils.js
Normal file
25
client/src/containers/Customers/utils.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { Intent } from '@blueprintjs/core';
|
||||||
|
import { AppToaster } from 'components';
|
||||||
|
import { formatMessage } from 'services/intl';
|
||||||
|
|
||||||
|
export const transformErrors = (errors) => {
|
||||||
|
if (errors.some((e) => e.type === 'CUSTOMER.HAS.SALES_INVOICES')) {
|
||||||
|
AppToaster.show({
|
||||||
|
message: formatMessage({
|
||||||
|
id: 'customer_has_sales_invoices',
|
||||||
|
}),
|
||||||
|
intent: Intent.DANGER,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
errors.find((error) => error.type === 'SOME.CUSTOMERS.HAVE.SALES_INVOICES')
|
||||||
|
) {
|
||||||
|
AppToaster.show({
|
||||||
|
message: formatMessage({
|
||||||
|
id: 'some_customers_have_sales_invoices',
|
||||||
|
}),
|
||||||
|
intent: Intent.DANGER,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -15,7 +15,6 @@ export default (mapState) => {
|
|||||||
|
|
||||||
const mapStateToProps = (state, props) => {
|
const mapStateToProps = (state, props) => {
|
||||||
const query = getCustomerTableQuery(state, props);
|
const query = getCustomerTableQuery(state, props);
|
||||||
|
|
||||||
const mapped = {
|
const mapped = {
|
||||||
customers: getCustomersList(state, props, query),
|
customers: getCustomersList(state, props, query),
|
||||||
customersViews: getResourceViews(state, props, 'customers'),
|
customersViews: getResourceViews(state, props, 'customers'),
|
||||||
@@ -24,7 +23,7 @@ export default (mapState) => {
|
|||||||
customersLoading: state.customers.loading,
|
customersLoading: state.customers.loading,
|
||||||
customersItems: state.customers.items,
|
customersItems: state.customers.items,
|
||||||
customersCurrentViewId: getCustomersCurrentViewId(state, props),
|
customersCurrentViewId: getCustomersCurrentViewId(state, props),
|
||||||
// customerErrors: state.customers.errors,
|
customersSelectedRows: state.customers.selectedRows,
|
||||||
};
|
};
|
||||||
return mapState ? mapState(mapped, state, props) : mapped;
|
return mapState ? mapState(mapped, state, props) : mapped;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,6 +27,11 @@ export const mapDispatchToProps = (dispatch) => ({
|
|||||||
currentViewId: parseInt(id, 10),
|
currentViewId: parseInt(id, 10),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
setSelectedRowsCustomers: (selectedRows) =>
|
||||||
|
dispatch({
|
||||||
|
type: t.CUSTOMER_SELECTED_ROWS_SET,
|
||||||
|
payload: { selectedRows },
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default connect(null, mapDispatchToProps);
|
export default connect(null, mapDispatchToProps);
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ const initialState = {
|
|||||||
views: {},
|
views: {},
|
||||||
loading: false,
|
loading: false,
|
||||||
currentViewId: -1,
|
currentViewId: -1,
|
||||||
|
selectedRows: [],
|
||||||
// Responsible for data fetch query based on this query.
|
// Responsible for data fetch query based on this query.
|
||||||
tableQuery: {
|
tableQuery: {
|
||||||
page_size: 12,
|
page_size: 12,
|
||||||
@@ -49,7 +49,6 @@ export default createReducer(initialState, {
|
|||||||
state.views[viewId] = {
|
state.views[viewId] = {
|
||||||
...view,
|
...view,
|
||||||
pages: {
|
pages: {
|
||||||
|
|
||||||
...(state.views?.[viewId]?.pages || {}),
|
...(state.views?.[viewId]?.pages || {}),
|
||||||
[paginationMeta.page]: {
|
[paginationMeta.page]: {
|
||||||
ids: customers.map((i) => i.id),
|
ids: customers.map((i) => i.id),
|
||||||
@@ -85,7 +84,10 @@ export default createReducer(initialState, {
|
|||||||
});
|
});
|
||||||
state.items = items;
|
state.items = items;
|
||||||
},
|
},
|
||||||
|
[t.CUSTOMER_SELECTED_ROWS_SET]: (state, action) => {
|
||||||
|
const { selectedRows } = action.payload;
|
||||||
|
state.selectedRows = selectedRows;
|
||||||
|
},
|
||||||
...viewPaginationSetReducer(t.CUSTOMERS_PAGINATION_SET),
|
...viewPaginationSetReducer(t.CUSTOMERS_PAGINATION_SET),
|
||||||
...createTableQueryReducers('CUSTOMERS'),
|
...createTableQueryReducers('CUSTOMERS'),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -8,4 +8,5 @@ export default {
|
|||||||
CUSTOMERS_BULK_DELETE: 'CUSTOMERS_BULK_DELETE',
|
CUSTOMERS_BULK_DELETE: 'CUSTOMERS_BULK_DELETE',
|
||||||
CUSTOMERS_PAGINATION_SET: 'CUSTOMERS_PAGINATION_SET',
|
CUSTOMERS_PAGINATION_SET: 'CUSTOMERS_PAGINATION_SET',
|
||||||
CUSTOMERS_SET_CURRENT_VIEW: 'CUSTOMERS_SET_CURRENT_VIEW',
|
CUSTOMERS_SET_CURRENT_VIEW: 'CUSTOMERS_SET_CURRENT_VIEW',
|
||||||
|
CUSTOMER_SELECTED_ROWS_SET: 'CUSTOMER_SELECTED_ROWS_SET',
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user