refactor: items alert.

This commit is contained in:
elforjani3
2021-01-27 13:20:15 +02:00
parent 62fcbf7c02
commit 0e95c5cce1
9 changed files with 59 additions and 38 deletions

View File

@@ -0,0 +1,85 @@
import React, { useState } from 'react';
import {
FormattedMessage as T,
FormattedHTMLMessage,
useIntl,
} from 'react-intl';
import { Intent, Alert } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { queryCache } from 'react-query';
import withItemCategoriesActions from 'containers/Items/withItemCategoriesActions';
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
import { compose } from 'utils';
/**
* Item Category delete alerts.
*/
function ItemCategoryDeleteAlert({
name,
// #withAlertStoreConnect
isOpen,
payload: { itemCategoryId },
// #withItemCategoriesActions
requestDeleteItemCategory,
// #withAlertActions
closeAlert,
}) {
const { formatMessage } = useIntl();
const [isLoading, setLoading] = useState(false);
// handle cancel delete item category alert.
const handleCancelItemCategoryDelete = () => {
closeAlert(name);
};
// Handle alert confirm delete item category.
const handleConfirmItemDelete = () => {
setLoading(true);
requestDeleteItemCategory(itemCategoryId)
.then(() => {
AppToaster.show({
message: formatMessage({
id: 'the_item_category_has_been_deleted_successfully',
}),
intent: Intent.SUCCESS,
});
queryCache.invalidateQueries('items-categories-list');
})
.catch(() => {})
.finally(() => {
setLoading(false);
closeAlert(name);
});
};
return (
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'delete'} />}
icon="trash"
intent={Intent.DANGER}
isOpen={isOpen}
onCancel={handleCancelItemCategoryDelete}
onConfirm={handleConfirmItemDelete}
loading={isLoading}
>
<p>
<FormattedHTMLMessage
id={'once_delete_this_item_category_you_will_able_to_restore_it'}
/>
</p>
</Alert>
);
}
export default compose(
withAlertStoreConnect(),
withAlertActions,
withItemCategoriesActions,
)(ItemCategoryDeleteAlert);