refactor: receipts alerts.

This commit is contained in:
elforjani3
2021-01-27 15:20:58 +02:00
parent 216eef889b
commit 29259f460d
4 changed files with 192 additions and 96 deletions

View File

@@ -0,0 +1,78 @@
import React, { useCallback, 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 withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
import withReceiptActions from 'containers/Sales/Receipt/withReceiptActions';
import { compose } from 'utils';
/**
* Receipt close alert.
*/
function ReceiptCloseAlert({
name,
// #withAlertStoreConnect
isOpen,
payload: { receiptId },
// #withReceiptActions
requestCloseReceipt,
// #withAlertActions
closeAlert,
}) {
const { formatMessage } = useIntl();
const [isLoading, setLoading] = useState(false);
// handle cancel delete alert.
const handleCancelDeleteAlert = () => {
closeAlert(name);
};
// Handle confirm receipt close.
const handleConfirmReceiptClose = useCallback(() => {
setLoading(true);
requestCloseReceipt(receiptId)
.then(() => {
AppToaster.show({
message: formatMessage({
id: 'the_receipt_has_been_closed_successfully',
}),
intent: Intent.SUCCESS,
});
queryCache.invalidateQueries('receipts-table');
})
.catch((error) => {})
.finally(() => {
closeAlert(name);
setLoading(false);
});
}, [receiptId, requestCloseReceipt, formatMessage]);
return (
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'close'} />}
intent={Intent.WARNING}
isOpen={isOpen}
onCancel={handleCancelDeleteAlert}
onConfirm={handleConfirmReceiptClose}
loading={isLoading}
>
<p>
<T id={'are_sure_to_close_this_receipt'} />
</p>
</Alert>
);
}
export default compose(
withAlertStoreConnect(),
withAlertActions,
withReceiptActions,
)(ReceiptCloseAlert);

View File

@@ -0,0 +1,85 @@
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 withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
import withReceiptActions from 'containers/Sales/Receipt/withReceiptActions';
import { compose } from 'utils';
/**
* Invoice alert.
*/
function NameDeleteAlert({
name,
// #withAlertStoreConnect
isOpen,
payload: { receiptId },
// #withReceiptActions
requestDeleteReceipt,
// #withAlertActions
closeAlert,
}) {
const { formatMessage } = useIntl();
const [isLoading, setLoading] = useState(false);
// handle cancel delete alert.
const handleCancelDeleteAlert = () => {
closeAlert(name);
};
// handle confirm delete receipt
const handleConfirmReceiptDelete = useCallback(() => {
setLoading(true);
requestDeleteReceipt(receiptId)
.then(() => {
AppToaster.show({
message: formatMessage({
id: 'the_receipt_has_been_deleted_successfully',
}),
intent: Intent.SUCCESS,
});
queryCache.invalidateQueries('receipts-table');
})
.catch(() => {})
.finally(() => {
setLoading(false);
closeAlert(name);
});
}, [receiptId, requestDeleteReceipt, formatMessage]);
return (
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'delete'} />}
icon="trash"
intent={Intent.DANGER}
isOpen={isOpen}
onCancel={handleCancelDeleteAlert}
onConfirm={handleConfirmReceiptDelete}
loading={isLoading}
>
<p>
<FormattedHTMLMessage
id={'once_delete_this_receipt_you_will_able_to_restore_it'}
/>
</p>
</Alert>
);
}
export default compose(
withAlertStoreConnect(),
withAlertActions,
withReceiptActions,
)(NameDeleteAlert);