mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-21 07:10:33 +00:00
Fix: ExpenseFlatingActions.
This commit is contained in:
69
client/src/containers/Expenses/ExpenseFloatingActions.js
Normal file
69
client/src/containers/Expenses/ExpenseFloatingActions.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { Intent, Button } from '@blueprintjs/core';
|
||||||
|
import { useFormikContext } from 'formik';
|
||||||
|
import { FormattedMessage as T } from 'react-intl';
|
||||||
|
import { CLASSES } from 'common/classes';
|
||||||
|
import classNames from 'classnames';
|
||||||
|
import { saveInvoke } from 'utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expense form floating actions.
|
||||||
|
*/
|
||||||
|
export default function ExpenseFloatingFooter({
|
||||||
|
isSubmitting,
|
||||||
|
onSubmitClick,
|
||||||
|
onCancelClick,
|
||||||
|
onDraftClick,
|
||||||
|
onSubmitAndNewClick,
|
||||||
|
expense,
|
||||||
|
}) {
|
||||||
|
const handleSubmitBtnClick = (event) => {
|
||||||
|
saveInvoke(onSubmitClick, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleCancelBtnClick = (event) => {
|
||||||
|
saveInvoke(onCancelClick, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmitAndDraftBtnClick = (event) => {
|
||||||
|
saveInvoke(onDraftClick, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleSubmitAndNewBtnClick = (event) => {
|
||||||
|
saveInvoke(onSubmitAndNewClick, event);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={classNames(CLASSES.PAGE_FORM_FLOATING_ACTIONS)}>
|
||||||
|
<Button
|
||||||
|
disabled={isSubmitting}
|
||||||
|
intent={Intent.PRIMARY}
|
||||||
|
type="submit"
|
||||||
|
onClick={handleSubmitBtnClick}
|
||||||
|
>
|
||||||
|
{expense && expense.id ? <T id={'edit'} /> : <T id={'save'} />}
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
className={'ml1'}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onClick={handleSubmitAndNewBtnClick}
|
||||||
|
type="submit"
|
||||||
|
>
|
||||||
|
<T id={'save_new'} />
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
className={'ml1'}
|
||||||
|
disabled={isSubmitting}
|
||||||
|
onClick={handleSubmitAndDraftBtnClick}
|
||||||
|
>
|
||||||
|
<T id={'save_as_draft'} />
|
||||||
|
</Button>
|
||||||
|
|
||||||
|
<Button className={'ml1'} onClick={handleCancelBtnClick}>
|
||||||
|
<T id={'close'} />
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { Intent, Button } from '@blueprintjs/core';
|
|
||||||
import { FormattedMessage as T } from 'react-intl';
|
|
||||||
|
|
||||||
export default function ExpenseFloatingFooter({
|
|
||||||
formik: { isSubmitting },
|
|
||||||
onSubmitClick,
|
|
||||||
onCancelClick,
|
|
||||||
|
|
||||||
expense,
|
|
||||||
}) {
|
|
||||||
return (
|
|
||||||
<div className={'form__floating-footer'}>
|
|
||||||
<Button
|
|
||||||
disabled={isSubmitting}
|
|
||||||
intent={Intent.PRIMARY}
|
|
||||||
type="submit"
|
|
||||||
onClick={() => {
|
|
||||||
onSubmitClick({ publish: true, redirect: true });
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
{expense && expense.id ? <T id={'edit'} /> : <T id={'save'} />}
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
disabled={isSubmitting}
|
|
||||||
intent={Intent.PRIMARY}
|
|
||||||
className={'ml1'}
|
|
||||||
name={'save_and_new'}
|
|
||||||
onClick={() => {
|
|
||||||
onSubmitClick({ publish: true, redirect: false });
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<T id={'save_new'} />
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className={'ml1'}
|
|
||||||
disabled={isSubmitting}
|
|
||||||
onClick={() => {
|
|
||||||
onSubmitClick({ publish: false, redirect: false });
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<T id={'save_as_draft'} />
|
|
||||||
</Button>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
className={'ml1'}
|
|
||||||
onClick={() => {
|
|
||||||
onCancelClick && onCancelClick();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<T id={'close'} />
|
|
||||||
</Button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -11,10 +11,11 @@ import moment from 'moment';
|
|||||||
import { Intent, FormGroup, TextArea } from '@blueprintjs/core';
|
import { Intent, FormGroup, TextArea } from '@blueprintjs/core';
|
||||||
import { FormattedMessage as T, useIntl } from 'react-intl';
|
import { FormattedMessage as T, useIntl } from 'react-intl';
|
||||||
import { pick } from 'lodash';
|
import { pick } from 'lodash';
|
||||||
|
import { useHistory } from 'react-router-dom';
|
||||||
|
|
||||||
import ExpenseFormHeader from './ExpenseFormHeader';
|
import ExpenseFormHeader from './ExpenseFormHeader';
|
||||||
import ExpenseTable from './ExpenseTable';
|
import ExpenseTable from './ExpenseTable';
|
||||||
import ExpenseFloatingFooter from './ExpenseFooter';
|
import ExpenseFloatingFooter from './ExpenseFloatingActions';
|
||||||
|
|
||||||
import withExpensesActions from 'containers/Expenses/withExpensesActions';
|
import withExpensesActions from 'containers/Expenses/withExpensesActions';
|
||||||
import withExpenseDetail from 'containers/Expenses/withExpenseDetail';
|
import withExpenseDetail from 'containers/Expenses/withExpenseDetail';
|
||||||
@@ -26,13 +27,16 @@ import AppToaster from 'components/AppToaster';
|
|||||||
import Dragzone from 'components/Dragzone';
|
import Dragzone from 'components/Dragzone';
|
||||||
|
|
||||||
import useMedia from 'hooks/useMedia';
|
import useMedia from 'hooks/useMedia';
|
||||||
import { compose, repeatValue } from 'utils';
|
import { compose, repeatValue, transformToForm } from 'utils';
|
||||||
|
|
||||||
const MIN_LINES_NUMBER = 4;
|
const MIN_LINES_NUMBER = 4;
|
||||||
const ERROR = {
|
const ERROR = {
|
||||||
EXPENSE_ALREADY_PUBLISHED: 'EXPENSE.ALREADY.PUBLISHED',
|
EXPENSE_ALREADY_PUBLISHED: 'EXPENSE.ALREADY.PUBLISHED',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expense form.
|
||||||
|
*/
|
||||||
function ExpenseForm({
|
function ExpenseForm({
|
||||||
// #withMedia
|
// #withMedia
|
||||||
requestSubmitMedia,
|
requestSubmitMedia,
|
||||||
@@ -54,8 +58,13 @@ function ExpenseForm({
|
|||||||
onFormSubmit,
|
onFormSubmit,
|
||||||
onCancelForm,
|
onCancelForm,
|
||||||
}) {
|
}) {
|
||||||
const { formatMessage } = useIntl();
|
const isNewMode = !expenseId;
|
||||||
|
|
||||||
const [payload, setPayload] = useState({});
|
const [payload, setPayload] = useState({});
|
||||||
|
|
||||||
|
const history = useHistory();
|
||||||
|
const { formatMessage } = useIntl();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
setFiles,
|
setFiles,
|
||||||
saveMedia,
|
saveMedia,
|
||||||
@@ -67,23 +76,6 @@ function ExpenseForm({
|
|||||||
deleteCallback: requestDeleteMedia,
|
deleteCallback: requestDeleteMedia,
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleDropFiles = useCallback((_files) => {
|
|
||||||
setFiles(_files.filter((file) => file.uploaded === false));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const savedMediaIds = useRef([]);
|
|
||||||
const clearSavedMediaIds = () => {
|
|
||||||
savedMediaIds.current = [];
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (expense && expense.id) {
|
|
||||||
changePageTitle(formatMessage({ id: 'edit_expense' }));
|
|
||||||
} else {
|
|
||||||
changePageTitle(formatMessage({ id: 'new_expense' }));
|
|
||||||
}
|
|
||||||
}, [changePageTitle, expense, formatMessage]);
|
|
||||||
|
|
||||||
const validationSchema = Yup.object().shape({
|
const validationSchema = Yup.object().shape({
|
||||||
beneficiary: Yup.string().label(formatMessage({ id: 'beneficiary' })),
|
beneficiary: Yup.string().label(formatMessage({ id: 'beneficiary' })),
|
||||||
payment_account_id: Yup.number()
|
payment_account_id: Yup.number()
|
||||||
@@ -117,11 +109,27 @@ function ExpenseForm({
|
|||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const handleDropFiles = useCallback((_files) => {
|
||||||
|
setFiles(_files.filter((file) => file.uploaded === false));
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const savedMediaIds = useRef([]);
|
||||||
|
const clearSavedMediaIds = () => {
|
||||||
|
savedMediaIds.current = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (expense && expense.id) {
|
||||||
|
changePageTitle(formatMessage({ id: 'edit_expense' }));
|
||||||
|
} else {
|
||||||
|
changePageTitle(formatMessage({ id: 'new_expense' }));
|
||||||
|
}
|
||||||
|
}, [changePageTitle, expense, formatMessage]);
|
||||||
|
|
||||||
const saveInvokeSubmit = useCallback(
|
const saveInvokeSubmit = useCallback(
|
||||||
(payload) => {
|
(payload) => {
|
||||||
onFormSubmit && onFormSubmit(payload);
|
onFormSubmit && onFormSubmit(payload);
|
||||||
},
|
},
|
||||||
|
|
||||||
[onFormSubmit],
|
[onFormSubmit],
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -205,7 +213,15 @@ function ExpenseForm({
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const formik = useFormik({
|
const {
|
||||||
|
values,
|
||||||
|
errors,
|
||||||
|
touched,
|
||||||
|
isSubmitting,
|
||||||
|
setFieldValue,
|
||||||
|
handleSubmit,
|
||||||
|
getFieldProps,
|
||||||
|
} = useFormik({
|
||||||
enableReinitialize: true,
|
enableReinitialize: true,
|
||||||
validationSchema,
|
validationSchema,
|
||||||
initialValues: {
|
initialValues: {
|
||||||
@@ -294,20 +310,20 @@ function ExpenseForm({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const handleSubmitClick = useCallback(
|
const handleSubmitClick = useCallback(() => {
|
||||||
(payload) => {
|
setPayload({ publish: true, redirect: true });
|
||||||
setPayload(payload);
|
}, [setPayload]);
|
||||||
formik.submitForm();
|
|
||||||
},
|
|
||||||
[setPayload, formik],
|
|
||||||
);
|
|
||||||
|
|
||||||
const handleCancelClick = useCallback(
|
const handleCancelClick = useCallback(() => {
|
||||||
(payload) => {
|
history.goBack();
|
||||||
onCancelForm && onCancelForm(payload);
|
}, []);
|
||||||
},
|
|
||||||
[onCancelForm],
|
const handleSubmitAndNewClick = useCallback(() => {
|
||||||
);
|
setPayload({ publish: true, redirect: false });
|
||||||
|
});
|
||||||
|
const handleSubmitAndDraftClick = useCallback(() => {
|
||||||
|
setPayload({ publish: false, redirect: false });
|
||||||
|
});
|
||||||
|
|
||||||
const handleDeleteFile = useCallback(
|
const handleDeleteFile = useCallback(
|
||||||
(_deletedFiles) => {
|
(_deletedFiles) => {
|
||||||
@@ -322,14 +338,14 @@ function ExpenseForm({
|
|||||||
|
|
||||||
// Handle click on add a new line/row.
|
// Handle click on add a new line/row.
|
||||||
const handleClickAddNewRow = () => {
|
const handleClickAddNewRow = () => {
|
||||||
formik.setFieldValue(
|
setFieldValue(
|
||||||
'categories',
|
'categories',
|
||||||
orderingCategoriesIndex([...formik.values.categories, defaultCategory]),
|
orderingCategoriesIndex([...values.categories, defaultCategory]),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleClearAllLines = () => {
|
const handleClearAllLines = () => {
|
||||||
formik.setFieldValue(
|
setFieldValue(
|
||||||
'categories',
|
'categories',
|
||||||
orderingCategoriesIndex([
|
orderingCategoriesIndex([
|
||||||
...repeatValue(defaultCategory, MIN_LINES_NUMBER),
|
...repeatValue(defaultCategory, MIN_LINES_NUMBER),
|
||||||
@@ -337,21 +353,23 @@ function ExpenseForm({
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const categories = formik.values.categories.filter(
|
|
||||||
(category) =>
|
|
||||||
category.amount && category.index && category.expense_account_id,
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={'expense-form'}>
|
<div className={'expense-form'}>
|
||||||
<form onSubmit={formik.handleSubmit}>
|
<form onSubmit={handleSubmit}>
|
||||||
<ExpenseFormHeader formik={formik} />
|
<ExpenseFormHeader
|
||||||
|
errors={errors}
|
||||||
|
touched={touched}
|
||||||
|
values={values}
|
||||||
|
setFieldValue={setFieldValue}
|
||||||
|
getFieldProps={getFieldProps}
|
||||||
|
/>
|
||||||
|
|
||||||
<ExpenseTable
|
<ExpenseTable
|
||||||
categories={formik.values.categories}
|
categories={values.categories}
|
||||||
onClickAddNewRow={handleClickAddNewRow}
|
onClickAddNewRow={handleClickAddNewRow}
|
||||||
onClickClearAllLines={handleClearAllLines}
|
onClickClearAllLines={handleClearAllLines}
|
||||||
formik={formik}
|
errors={errors}
|
||||||
|
setFieldValue={setFieldValue}
|
||||||
defaultRow={defaultCategory}
|
defaultRow={defaultCategory}
|
||||||
/>
|
/>
|
||||||
<div class="expense-form-footer">
|
<div class="expense-form-footer">
|
||||||
@@ -359,10 +377,7 @@ function ExpenseForm({
|
|||||||
label={<T id={'description'} />}
|
label={<T id={'description'} />}
|
||||||
className={'form-group--description'}
|
className={'form-group--description'}
|
||||||
>
|
>
|
||||||
<TextArea
|
<TextArea growVertically={true} {...getFieldProps('description')} />
|
||||||
growVertically={true}
|
|
||||||
{...formik.getFieldProps('description')}
|
|
||||||
/>
|
|
||||||
</FormGroup>
|
</FormGroup>
|
||||||
|
|
||||||
<Dragzone
|
<Dragzone
|
||||||
@@ -372,13 +387,16 @@ function ExpenseForm({
|
|||||||
hint={'Attachments: Maxiumum size: 20MB'}
|
hint={'Attachments: Maxiumum size: 20MB'}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
|
||||||
<ExpenseFloatingFooter
|
<ExpenseFloatingFooter
|
||||||
formik={formik}
|
isSubmitting={isSubmitting}
|
||||||
onSubmitClick={handleSubmitClick}
|
onSubmitClick={handleSubmitClick}
|
||||||
expense={expense}
|
|
||||||
onCancelClick={handleCancelClick}
|
onCancelClick={handleCancelClick}
|
||||||
|
onDraftClick={handleSubmitAndDraftClick}
|
||||||
|
onSubmitAndNewClick={handleSubmitAndNewClick}
|
||||||
|
expense={expense}
|
||||||
/>
|
/>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,11 @@ import withAccounts from 'containers/Accounts/withAccounts';
|
|||||||
import withCustomers from 'containers/Customers/withCustomers';
|
import withCustomers from 'containers/Customers/withCustomers';
|
||||||
|
|
||||||
function ExpenseFormHeader({
|
function ExpenseFormHeader({
|
||||||
formik: { errors, touched, setFieldValue, getFieldProps, values },
|
errors,
|
||||||
|
touched,
|
||||||
|
setFieldValue,
|
||||||
|
getFieldProps,
|
||||||
|
values,
|
||||||
currenciesList,
|
currenciesList,
|
||||||
accountsList,
|
accountsList,
|
||||||
accountsTypes,
|
accountsTypes,
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ function ExpenseTable({
|
|||||||
onClickClearAllLines,
|
onClickClearAllLines,
|
||||||
defaultRow,
|
defaultRow,
|
||||||
categories,
|
categories,
|
||||||
formik: { errors, setFieldValue, resetForm },
|
errors,
|
||||||
|
setFieldValue,
|
||||||
}) {
|
}) {
|
||||||
const [rows, setRows] = useState([]);
|
const [rows, setRows] = useState([]);
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
|
|||||||
Reference in New Issue
Block a user