refactor: customers alerts.

This commit is contained in:
elforjani3
2021-01-27 20:01:33 +02:00
parent 29259f460d
commit 25d4b558b6
13 changed files with 311 additions and 213 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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 (
<Alert
cancelButtonText={<T id={'cancel'} />}
@@ -61,6 +61,7 @@ function ItemBulkDeleteAlert({
isOpen={isOpen}
onCancel={handleCancelBulkDelete}
onConfirm={handleConfirmBulkDelete}
loading={isLoading}
>
<p>
<T id={'once_delete_these_items_you_will_not_able_restore_them'} />