Fix: Expense & PaymentReceive FloatingActions.

This commit is contained in:
elforjani3
2020-11-24 01:31:22 +02:00
parent 222ffd7ac6
commit 98fb5ca448
5 changed files with 252 additions and 100 deletions

View File

@@ -1,9 +1,20 @@
import React from 'react';
import { Intent, Button } from '@blueprintjs/core';
import {
Intent,
Button,
ButtonGroup,
Popover,
PopoverInteractionKind,
Position,
Menu,
MenuItem,
} from '@blueprintjs/core';
import { FormattedMessage as T } from 'react-intl';
import classNames from 'classnames';
import { CLASSES } from 'common/classes';
import { useFormikContext } from 'formik';
import { saveInvoke } from 'utils';
import { Icon } from 'components';
/**
* Payment receive floating actions bar.
@@ -13,53 +24,74 @@ export default function PaymentReceiveFormFloatingActions({
onSubmitClick,
onCancelClick,
onClearClick,
onSubmitForm,
paymentReceiveId,
}) {
const handleSubmitClick = (event) => {
onSubmitClick && onSubmitClick(event);
const handleSubmitBtnClick = (event) => {
saveInvoke(onSubmitClick, event, {
redirect: true,
});
};
const handleClearBtnClick = (event) => {
onClearClick && onClearClick(event);
};
const handleCloseBtnClick = (event) => {
const handleCancelBtnClick = (event) => {
onCancelClick && onCancelClick(event);
saveInvoke(onCancelClick, event);
};
const handleSubmitAndNewClick = (event) => {
onSubmitForm();
saveInvoke(onSubmitClick, event, {
redirect: false,
});
};
return (
<div className={classNames(CLASSES.PAGE_FORM_FLOATING_ACTIONS)}>
<Button
disabled={isSubmitting}
intent={Intent.PRIMARY}
type="submit"
onClick={handleSubmitClick}
>
{paymentReceiveId ? <T id={'edit'} /> : <T id={'save_send'} />}
</Button>
<Button
disabled={isSubmitting}
intent={Intent.PRIMARY}
className={'ml1'}
name={'save'}
type="submit"
onClick={handleSubmitClick}
>
<T id={'save'} />
</Button>
<Button
className={'ml1'}
disabled={isSubmitting}
onClick={handleClearBtnClick}
>
<T id={'clear'} />
</Button>
<Button className={'ml1'} type="submit" onClick={handleCloseBtnClick}>
<T id={'close'} />
</Button>
<ButtonGroup>
{/* ----------- Save and New ----------- */}
<Button
disabled={isSubmitting}
intent={Intent.PRIMARY}
type="submit"
onClick={handleSubmitBtnClick}
text={paymentReceiveId ? <T id={'edit'} /> : <T id={'save_and_send'} />}
/>
<Popover
content={
<Menu>
<MenuItem
text={<T id={'save_and_new'} />}
onClick={handleSubmitAndNewClick}
/>
</Menu>
}
minimal={true}
interactionKind={PopoverInteractionKind.CLICK}
position={Position.BOTTOM_LEFT}
>
<Button
intent={Intent.PRIMARY}
rightIcon={<Icon icon="arrow-drop-up-16" iconSize={20} />}
/>
</Popover>
{/* ----------- Clear & Reset----------- */}
<Button
className={'ml1'}
disabled={isSubmitting}
onClick={handleClearBtnClick}
text={paymentReceiveId ? <T id={'reset'} /> : <T id={'clear'} />}
/>
{/* ----------- Cancel ----------- */}
<Button
className={'ml1'}
onClick={handleCancelBtnClick}
text={<T id={'cancel'} />}
/>
</ButtonGroup>
</div>
);
}

View File

@@ -13,6 +13,7 @@ import { FormattedMessage as T, useIntl } from 'react-intl';
import { pick, sumBy, omit } from 'lodash';
import { Intent, Alert } from '@blueprintjs/core';
import classNames from 'classnames';
import { useHistory } from 'react-router-dom';
import { CLASSES } from 'common/classes';
import PaymentReceiveHeader from './PaymentReceiveFormHeader';
@@ -66,8 +67,11 @@ function PaymentReceiveForm({
const [clearLinesAlert, setClearLinesAlert] = useState(false);
const [fullAmount, setFullAmount] = useState(null);
const [clearFormAlert, setClearFormAlert] = useState(false);
const [submitPayload, setSubmitPayload] = useState({});
const { formatMessage } = useIntl();
const history = useHistory();
const isNewMode = !paymentReceiveId;
const [localPaymentEntries, setLocalPaymentEntries] = useState(
paymentReceiveEntries,
@@ -103,7 +107,7 @@ function PaymentReceiveForm({
const validationSchema = isNewMode
? CreatePaymentReceiveFormSchema
: EditPaymentReceiveFormSchema;
// Default payment receive entry.
const defaultPaymentReceiveEntry = {
id: '',
@@ -187,6 +191,10 @@ function PaymentReceiveForm({
});
setSubmitting(false);
resetForm();
if (submitPayload.redirect) {
history.push('/payment-receives');
}
};
// Handle request response errors.
const onError = (errors) => {
@@ -220,6 +228,7 @@ function PaymentReceiveForm({
isSubmitting,
touched,
resetForm,
submitForm,
} = useFormik({
enableReinitialize: true,
validationSchema,
@@ -338,6 +347,17 @@ function PaymentReceiveForm({
[changePageSubtitle],
);
const handleSubmitClick = useCallback(
(event, payload) => {
setSubmitPayload({ ...payload });
},
[setSubmitPayload],
);
const handleCancelClick = useCallback(() => {
history.goBack();
}, [history]);
return (
<div
className={classNames(
@@ -416,6 +436,9 @@ function PaymentReceiveForm({
isSubmitting={isSubmitting}
paymentReceiveId={paymentReceiveId}
onClearClick={handleClearBtnClick}
onSubmitClick={handleSubmitClick}
onCancelClick={handleCancelClick}
onSubmitForm={submitForm}
/>
</form>
</div>