Files
bigcapital/src/containers/Alerts/Invoices/CancelBadDebtAlert.js
2021-11-02 14:22:55 +02:00

69 lines
1.5 KiB
JavaScript

import React from 'react';
import { FormattedMessage as T } from 'components';
import intl from 'react-intl-universal';
import { Intent, Alert } from '@blueprintjs/core';
import { AppToaster } from 'components';
import { useCancelBadDebt } from 'hooks/query';
import withAlertStoreConnect from 'containers/Alert/withAlertStoreConnect';
import withAlertActions from 'containers/Alert/withAlertActions';
import { compose } from 'utils';
/**
* Cancel bad debt alert.
*/
function CancelBadDebtAlert({
name,
// #withAlertStoreConnect
isOpen,
payload: { invoiceId },
// #withAlertActions
closeAlert,
}) {
// handle cancel alert.
const handleCancel = () => {
closeAlert(name);
};
const { mutateAsync: cancelBadDebtMutate, isLoading } = useCancelBadDebt();
// handleConfirm alert.
const handleConfirm = () => {
cancelBadDebtMutate(invoiceId)
.then(() => {
AppToaster.show({
message: intl.get('bad_debt.cancel_alert.success_message'),
intent: Intent.SUCCESS,
});
})
.catch(() => {})
.finally(() => {
closeAlert(name);
});
};
return (
<Alert
cancelButtonText={<T id={'cancel'} />}
confirmButtonText={<T id={'save'} />}
intent={Intent.WARNING}
isOpen={isOpen}
onCancel={handleCancel}
onConfirm={handleConfirm}
loading={isLoading}
>
<p>
<T id={'bad_debt.cancel_alert.message'} />
</p>
</Alert>
);
}
export default compose(
withAlertStoreConnect(),
withAlertActions,
)(CancelBadDebtAlert);