diff --git a/client/src/containers/Alerts/Customers/CustomerBulkDeleteAlert.js b/client/src/containers/Alerts/Customers/CustomerBulkDeleteAlert.js new file mode 100644 index 000000000..253f04016 --- /dev/null +++ b/client/src/containers/Alerts/Customers/CustomerBulkDeleteAlert.js @@ -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 ( + } + confirmButtonText={} + icon="trash" + intent={Intent.DANGER} + isOpen={isOpen} + onCancel={handleCancelDeleteAlert} + onConfirm={handleConfirmBulkDelete} + loading={isLoading} + > +

+ +

+
+ ); +} + +export default compose( + withAlertStoreConnect(), + withAlertActions, + withCustomersActions, +)(CustomerBulkDeleteAlert); diff --git a/client/src/containers/Alerts/Customers/CustomerDeleteAlert.js b/client/src/containers/Alerts/Customers/CustomerDeleteAlert.js new file mode 100644 index 000000000..c90da2124 --- /dev/null +++ b/client/src/containers/Alerts/Customers/CustomerDeleteAlert.js @@ -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 ( + } + confirmButtonText={} + icon="trash" + intent={Intent.DANGER} + isOpen={isOpen} + onCancel={handleCancelDeleteAlert} + onConfirm={handleConfirmDeleteCustomer} + loading={isLoading} + > +

+ +

+
+ ); +} + +export default compose( + withAlertStoreConnect(), + withAlertActions, + withCustomersActions, +)(CustomerDeleteAlert); diff --git a/client/src/containers/Alerts/Items/ItemBulkDeleteAlert.js b/client/src/containers/Alerts/Items/ItemBulkDeleteAlert.js index 354fd43e4..7da176ab3 100644 --- a/client/src/containers/Alerts/Items/ItemBulkDeleteAlert.js +++ b/client/src/containers/Alerts/Items/ItemBulkDeleteAlert.js @@ -27,6 +27,7 @@ function ItemBulkDeleteAlert({ }) { const { formatMessage } = useIntl(); const [isLoading, setLoading] = useState(false); + // handle cancel item bulk delete alert. const handleCancelBulkDelete = () => { closeAlert(name); @@ -49,7 +50,6 @@ function ItemBulkDeleteAlert({ closeAlert(name); }); }; - return ( } @@ -61,6 +61,7 @@ function ItemBulkDeleteAlert({ isOpen={isOpen} onCancel={handleCancelBulkDelete} onConfirm={handleConfirmBulkDelete} + loading={isLoading} >

diff --git a/client/src/containers/Customers/CustomerActionsBar.js b/client/src/containers/Customers/CustomerActionsBar.js index aaf60526c..678b5ff10 100644 --- a/client/src/containers/Customers/CustomerActionsBar.js +++ b/client/src/containers/Customers/CustomerActionsBar.js @@ -14,33 +14,31 @@ import classNames from 'classnames'; import { connect } from 'react-redux'; import { useHistory } from 'react-router-dom'; -import Icon from 'components/Icon'; import DashboardActionsBar from 'components/Dashboard/DashboardActionsBar'; -import FilterDropdown from 'components/FilterDropdown'; -import { If, DashboardActionViewsList } from 'components'; +import { If, Icon, DashboardActionViewsList } from 'components'; import withResourceDetail from 'containers/Resources/withResourceDetails'; import withCustomers from 'containers/Customers/withCustomers'; import withCustomersActions from 'containers/Customers/withCustomersActions'; +import withAlertActions from 'containers/Alert/withAlertActions'; + import { compose } from 'utils'; const CustomerActionsBar = ({ - // #withResourceDetail - resourceFields, - // #withCustomers customersViews, + customersSelectedRows, //#withCustomersActions addCustomersTableQueries, changeCustomerView, + // #withAlertActions + openAlert, + // #ownProps - selectedRows = [], onFilterChanged, - onBulkDelete, }) => { - const [filterCount, setFilterCount] = useState(0); const history = useHistory(); const { formatMessage } = useIntl(); @@ -48,14 +46,10 @@ const CustomerActionsBar = ({ history.push('/customers/new'); }, [history]); - - const hasSelectedRows = useMemo(() => selectedRows.length > 0, [ - selectedRows, - ]); - - const handleBulkDelete = useCallback(() => { - onBulkDelete && onBulkDelete(selectedRows.map((r) => r.id)); - }, [onBulkDelete, selectedRows]); + // Handle Customers bulk delete button click., + const handleBulkDelete = () => { + openAlert('customers-bulk-delete', { customersIds: customersSelectedRows }); + }; const handleTabChange = (viewId) => { changeCustomerView(viewId.id || -1); @@ -88,18 +82,12 @@ const CustomerActionsBar = ({ >