mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
chrone: sperate client and server to different repos.
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
import React, { useMemo } from 'react';
|
||||
import { Intent } from '@blueprintjs/core';
|
||||
import { Formik } from 'formik';
|
||||
import moment from 'moment';
|
||||
import { FormattedMessage as T } from 'components';
|
||||
import intl from 'react-intl-universal';
|
||||
import { AppToaster } from 'components';
|
||||
import {
|
||||
CreateExchangeRateFormSchema,
|
||||
EditExchangeRateFormSchema,
|
||||
} from './ExchangeRateForm.schema';
|
||||
import ExchangeRateFormContent from './ExchangeRateFormContent';
|
||||
import { useExchangeRateFromContext } from './ExchangeRateFormProvider';
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
|
||||
import { compose, transformToForm } from 'utils';
|
||||
|
||||
const defaultInitialValues = {
|
||||
exchange_rate: '',
|
||||
currency_code: '',
|
||||
date: moment(new Date()).format('YYYY-MM-DD'),
|
||||
};
|
||||
|
||||
/**
|
||||
* Exchange rate form.
|
||||
*/
|
||||
function ExchangeRateForm({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const {
|
||||
createExchangeRateMutate,
|
||||
editExchangeRateMutate,
|
||||
isNewMode,
|
||||
dialogName,
|
||||
exchangeRate,
|
||||
} = useExchangeRateFromContext();
|
||||
|
||||
// Form validation schema in create and edit mode.
|
||||
const validationSchema = isNewMode
|
||||
? CreateExchangeRateFormSchema
|
||||
: EditExchangeRateFormSchema;
|
||||
const initialValues = useMemo(
|
||||
() => ({
|
||||
...defaultInitialValues,
|
||||
...transformToForm(exchangeRate, defaultInitialValues),
|
||||
}),
|
||||
[],
|
||||
);
|
||||
|
||||
// Transformers response errors.
|
||||
const transformErrors = (errors, { setErrors }) => {
|
||||
if (
|
||||
errors.find((error) => error.type === 'EXCHANGE.RATE.DATE.PERIOD.DEFINED')
|
||||
) {
|
||||
setErrors({
|
||||
exchange_rate: intl.get(
|
||||
'there_is_exchange_rate_in_this_date_with_the_same_currency',
|
||||
),
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Handle the form submit.
|
||||
const handleFormSubmit = (values, { setSubmitting, setErrors }) => {
|
||||
setSubmitting(true);
|
||||
|
||||
// Handle close the dialog after success response.
|
||||
const afterSubmit = () => {
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
const onSuccess = ({ response }) => {
|
||||
AppToaster.show({
|
||||
message: intl.get(
|
||||
!isNewMode
|
||||
? 'the_exchange_rate_has_been_edited_successfully'
|
||||
: 'the_exchange_rate_has_been_created_successfully',
|
||||
),
|
||||
intent: Intent.SUCCESS,
|
||||
});
|
||||
afterSubmit(response);
|
||||
};
|
||||
// Handle the response error.
|
||||
const onError = (error) => {
|
||||
const {
|
||||
response: {
|
||||
data: { errors },
|
||||
},
|
||||
} = error;
|
||||
|
||||
transformErrors(errors, { setErrors });
|
||||
setSubmitting(false);
|
||||
};
|
||||
if (isNewMode) {
|
||||
createExchangeRateMutate(values).then(onSuccess).catch(onError);
|
||||
} else {
|
||||
editExchangeRateMutate([exchangeRate.id, values])
|
||||
.then(onSuccess)
|
||||
.catch(onError);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Formik
|
||||
validationSchema={validationSchema}
|
||||
initialValues={initialValues}
|
||||
onSubmit={handleFormSubmit}
|
||||
>
|
||||
<ExchangeRateFormContent />
|
||||
</Formik>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(ExchangeRateForm);
|
||||
@@ -0,0 +1,18 @@
|
||||
import * as Yup from 'yup';
|
||||
import intl from 'react-intl-universal';
|
||||
import { DATATYPES_LENGTH } from 'common/dataTypes';
|
||||
|
||||
const Schema = Yup.object().shape({
|
||||
exchange_rate: Yup.number()
|
||||
.required()
|
||||
.label(intl.get('exchange_rate_')),
|
||||
currency_code: Yup.string()
|
||||
.max(3)
|
||||
.required(intl.get('currency_code_')),
|
||||
date: Yup.date()
|
||||
.required()
|
||||
.label(intl.get('date')),
|
||||
});
|
||||
|
||||
export const CreateExchangeRateFormSchema = Schema;
|
||||
export const EditExchangeRateFormSchema = Schema;
|
||||
@@ -0,0 +1,13 @@
|
||||
import React from 'react';
|
||||
import { Form } from 'formik';
|
||||
import ExchangeRateFormFields from './ExchangeRateFormFields';
|
||||
import ExchangeRateFormFooter from './ExchangeRateFormFooter';
|
||||
|
||||
export default function ExchangeRateFormContent() {
|
||||
return (
|
||||
<Form>
|
||||
<ExchangeRateFormFields />
|
||||
<ExchangeRateFormFooter />
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import React from 'react';
|
||||
|
||||
import ExchangeRateForm from './ExchangeRateForm';
|
||||
import { ExchangeRateFormProvider } from './ExchangeRateFormProvider';
|
||||
|
||||
import 'style/pages/ExchangeRate/ExchangeRateDialog.scss';
|
||||
|
||||
/**
|
||||
* Exchange rate form content.
|
||||
*/
|
||||
export default function ExchangeRateFormDialogContent({
|
||||
// #ownProp
|
||||
action,
|
||||
exchangeRateId,
|
||||
dialogName,
|
||||
}) {
|
||||
return (
|
||||
<ExchangeRateFormProvider
|
||||
dialogName={dialogName}
|
||||
exchangeRate={exchangeRateId}
|
||||
action={action}
|
||||
>
|
||||
<ExchangeRateForm />
|
||||
</ExchangeRateFormProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import React from 'react';
|
||||
import { Classes, FormGroup, InputGroup, Position } from '@blueprintjs/core';
|
||||
import { FastField } from 'formik';
|
||||
import { DateInput } from '@blueprintjs/datetime';
|
||||
import { FormattedMessage as T } from 'components';
|
||||
import {
|
||||
momentFormatter,
|
||||
tansformDateValue,
|
||||
handleDateChange,
|
||||
inputIntent,
|
||||
} from 'utils';
|
||||
import {
|
||||
ErrorMessage,
|
||||
FieldRequiredHint,
|
||||
CurrencySelectList,
|
||||
} from 'components';
|
||||
import { useExchangeRateFromContext } from './ExchangeRateFormProvider';
|
||||
|
||||
import classNames from 'classnames';
|
||||
|
||||
export default function ExchangeRateFormFields() {
|
||||
const { action, currencies } = useExchangeRateFromContext();
|
||||
|
||||
return (
|
||||
<div className={Classes.DIALOG_BODY}>
|
||||
{/* ----------- Date ----------- */}
|
||||
<FastField name={'date'}>
|
||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'date'} />}
|
||||
labelInfo={FieldRequiredHint}
|
||||
className={classNames('form-group--select-list', Classes.FILL)}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="date" />}
|
||||
inline={true}
|
||||
>
|
||||
<DateInput
|
||||
{...momentFormatter('YYYY/MM/DD')}
|
||||
value={tansformDateValue(value)}
|
||||
onChange={handleDateChange((formattedDate) => {
|
||||
form.setFieldValue('date', formattedDate);
|
||||
})}
|
||||
popoverProps={{ position: Position.BOTTOM, minimal: true }}
|
||||
disabled={action === 'edit'}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
{/* ----------- Currency Code ----------- */}
|
||||
<FastField name={'currency_code'}>
|
||||
{({ form, field: { value }, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'currency_code'} />}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
className={classNames('form-group--currency', Classes.FILL)}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="currency_code" />}
|
||||
inline={true}
|
||||
>
|
||||
<CurrencySelectList
|
||||
currenciesList={currencies}
|
||||
selectedCurrencyCode={value}
|
||||
onCurrencySelected={({ currency_code }) => {
|
||||
form.setFieldValue('currency_code', currency_code);
|
||||
}}
|
||||
disabled={action === 'edit'}
|
||||
/>
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
|
||||
{/*------------ Exchange Rate -----------*/}
|
||||
<FastField name={'exchange_rate'}>
|
||||
{({ form, field, meta: { error, touched } }) => (
|
||||
<FormGroup
|
||||
label={<T id={'exchange_rate'} />}
|
||||
labelInfo={<FieldRequiredHint />}
|
||||
intent={inputIntent({ error, touched })}
|
||||
helperText={<ErrorMessage name="exchange_rate" />}
|
||||
inline={true}
|
||||
>
|
||||
<InputGroup intent={inputIntent({ error, touched })} {...field} />
|
||||
</FormGroup>
|
||||
)}
|
||||
</FastField>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import React from 'react';
|
||||
import { useFormikContext } from 'formik';
|
||||
|
||||
import { Button, Classes, Intent } from '@blueprintjs/core';
|
||||
import { FormattedMessage as T } from 'components';
|
||||
import { useExchangeRateFromContext } from './ExchangeRateFormProvider';
|
||||
import withDialogActions from 'containers/Dialog/withDialogActions';
|
||||
import { compose } from 'utils';
|
||||
|
||||
function ExchangeRateFormFooter({
|
||||
// #withDialogActions
|
||||
closeDialog,
|
||||
}) {
|
||||
const { isSubmitting } = useFormikContext();
|
||||
const { dialogName, action } = useExchangeRateFromContext();
|
||||
|
||||
const handleClose = () => {
|
||||
closeDialog(dialogName);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={Classes.DIALOG_FOOTER}>
|
||||
<div className={Classes.DIALOG_FOOTER_ACTIONS}>
|
||||
<Button onClick={handleClose}>
|
||||
<T id={'close'} />
|
||||
</Button>
|
||||
<Button intent={Intent.PRIMARY} type="submit" disabled={isSubmitting}>
|
||||
{action === 'edit' ? <T id={'edit'} /> : <T id={'submit'} />}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogActions)(ExchangeRateFormFooter);
|
||||
@@ -0,0 +1,52 @@
|
||||
import React, { createContext, useContext } from 'react';
|
||||
import {
|
||||
useCreateExchangeRate,
|
||||
useEdiExchangeRate,
|
||||
useCurrencies,
|
||||
useExchangeRates,
|
||||
} from 'hooks/query';
|
||||
import { DialogContent } from 'components';
|
||||
|
||||
const ExchangeRateFormContext = createContext();
|
||||
|
||||
/**
|
||||
* Exchange rate Form page provider.
|
||||
*/
|
||||
function ExchangeRateFormProvider({
|
||||
exchangeRate,
|
||||
action,
|
||||
dialogName,
|
||||
...props
|
||||
}) {
|
||||
// Create and edit exchange rate mutations.
|
||||
const { mutateAsync: createExchangeRateMutate } = useCreateExchangeRate();
|
||||
const { mutateAsync: editExchangeRateMutate } = useEdiExchangeRate();
|
||||
|
||||
// Load Currencies list.
|
||||
const { data: currencies, isFetching: isCurrenciesLoading } = useCurrencies();
|
||||
const { isFetching: isExchangeRatesLoading } = useExchangeRates();
|
||||
|
||||
const isNewMode = !exchangeRate;
|
||||
|
||||
// Provider state.
|
||||
const provider = {
|
||||
createExchangeRateMutate,
|
||||
editExchangeRateMutate,
|
||||
dialogName,
|
||||
exchangeRate,
|
||||
action,
|
||||
currencies,
|
||||
isExchangeRatesLoading,
|
||||
isNewMode,
|
||||
};
|
||||
|
||||
return (
|
||||
<DialogContent isLoading={isCurrenciesLoading} name={'exchange-rate-form'}>
|
||||
<ExchangeRateFormContext.Provider value={provider} {...props} />
|
||||
</DialogContent>
|
||||
);
|
||||
}
|
||||
|
||||
const useExchangeRateFromContext = () => useContext(ExchangeRateFormContext);
|
||||
|
||||
export { ExchangeRateFormProvider, useExchangeRateFromContext };
|
||||
49
src/containers/Dialogs/ExchangeRateFormDialog/index.js
Normal file
49
src/containers/Dialogs/ExchangeRateFormDialog/index.js
Normal file
@@ -0,0 +1,49 @@
|
||||
import React, { lazy } from 'react';
|
||||
import { FormattedMessage as T } from 'components';
|
||||
import intl from 'react-intl-universal';
|
||||
import {
|
||||
Dialog,
|
||||
DialogSuspense,
|
||||
} from 'components';
|
||||
import withDialogRedux from 'components/DialogReduxConnect';
|
||||
import { compose } from 'utils';
|
||||
|
||||
const ExchangeRateFormDialogContent = lazy(() =>
|
||||
import('./ExchangeRateFormDialogContent'),
|
||||
);
|
||||
|
||||
/**
|
||||
* Exchange rate form dialog.
|
||||
*/
|
||||
function ExchangeRateFormDialog({
|
||||
dialogName,
|
||||
payload = { action: '', id: null , exchangeRate:"" },
|
||||
isOpen,
|
||||
}) {
|
||||
return (
|
||||
<Dialog
|
||||
name={dialogName}
|
||||
title={
|
||||
payload.action === 'edit' ? (
|
||||
<T id={'edit_exchange_rate'} />
|
||||
) : (
|
||||
<T id={'new_exchange_rate'} />
|
||||
)
|
||||
}
|
||||
className={'dialog--exchangeRate-form'}
|
||||
isOpen={isOpen}
|
||||
autoFocus={true}
|
||||
canEscapeKeyClose={true}
|
||||
>
|
||||
<DialogSuspense>
|
||||
<ExchangeRateFormDialogContent
|
||||
dialogName={dialogName}
|
||||
action={payload.action}
|
||||
exchangeRateId={payload.exchangeRate}
|
||||
/>
|
||||
</DialogSuspense>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
export default compose(withDialogRedux())(ExchangeRateFormDialog);
|
||||
Reference in New Issue
Block a user