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,78 @@
import React, { useState } from 'react';
import { FormattedMessage as T, useIntl } from 'react-intl';
import { Intent, Alert } from '@blueprintjs/core';
import { queryCache } from 'react-query';
import { AppToaster } from 'components';
import withItemsActions from 'containers/Items/withItemsActions';
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
import { compose } from 'utils';
/**
* Item activate alert.
*/
function ItemActivateAlert({
name,
// #withAlertStoreConnect
isOpen,
payload: { itemId },
// #withItemsActions
requestActivateItem,
// #withAlertActions
closeAlert,
}) {
const { formatMessage } = useIntl();
const [isLoading, setLoading] = useState(false);
// Handle activate item alert cancel.
const handleCancelActivateItem = () => {
closeAlert(name);
};
// Handle confirm item activated.
const handleConfirmItemActivate = () => {
setLoading(true);
requestActivateItem(itemId)
.then(() => {
AppToaster.show({
message: formatMessage({
id: 'the_item_has_been_activated_successfully',
}),
intent: Intent.SUCCESS,
});
queryCache.invalidateQueries('items-table');
})
.catch((error) => {})
.finally(() => {
closeAlert(name);
setLoading(false);
});
};
return (
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'activate'} />}
intent={Intent.WARNING}
isOpen={isOpen}
onCancel={handleCancelActivateItem}
onConfirm={handleConfirmItemActivate}
loading={isLoading}
>
<p>
<T id={'are_sure_to_activate_this_item'} />
</p>
</Alert>
);
}
export default compose(
withAlertStoreConnect(),
withAlertActions,
withItemsActions,
)(ItemActivateAlert);