Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tax rate select fix for invoice form #61

Merged
merged 5 commits into from
May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/components/forms/validators.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { t } from '@lingui/macro';
import { isArray, isEmpty, isString, includes, map } from 'lodash';
import { isArray, isEmpty, isNumber, isString, includes, map } from 'lodash';

import { i18n } from '../../layouts/base';

const isNumeric = value => {
return isNumber(value) || (!isEmpty(value) && !isNaN(value));
};

/* Required */
const required = value => (value ? undefined : i18n._(t`This field is required`));

/* Numeric */
const numeric = value => (isNumeric(value) ? undefined : i18n._(t`Invalid number`));

/* Email */
const email = value =>
value && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(value)
? i18n._(t`Invalid email address`)
: undefined;

/* Array of emails */
const emails = value => {
if (isArray(value) && !isEmpty(value)) {
return includes(
map(value, v => (isString(email(v)) ? false : true)),
false
)
return includes(map(value, v => (isString(email(v)) ? false : true)), false)
? i18n._(t`Invalid email address`)
: undefined;
} else {
return undefined;
}
};

export { email, emails, required };
export { email, emails, numeric, required };
8 changes: 2 additions & 6 deletions src/components/invoices/line-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,8 @@ class LineItems extends Component {
key="taxRate"
render={(field, row, index) => (
<div>
<Field
name={field}
component={ASelect}
options={[]}
onDrop={e => e.preventDefault()}
>
<Field name={field} component={ASelect} onDrop={e => e.preventDefault()}>
<Select.Option />
{map(taxRates.items, rate => {
return (
<Select.Option value={rate._id} key={rate._id}>
Expand Down
8 changes: 7 additions & 1 deletion src/pages/settings/tax-rates/$id.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { has } from 'lodash';
import router from 'umi/router';

import { AInput, ATextarea } from '../../../components/forms/fields';
import { numeric } from '../../../components/forms/validators';

class TaxForm extends Component {
closeDrawer = () => {
Expand Down Expand Up @@ -59,7 +60,12 @@ class TaxForm extends Component {
label={<Trans>Description</Trans>}
rows={4}
/>
<Field name="percentage" component={AInput} label={<Trans>Percentage</Trans>} />
<Field
name="percentage"
component={AInput}
label={<Trans>Percentage</Trans>}
validate={[numeric]}
/>
<Button
type="primary"
htmlType="submit"
Expand Down