Skip to content

Commit cbfc43b

Browse files
authored
Merge pull request #8 from Lemoncode/feature/Implement_validations_to_employee
Feature/implement validations to employee
2 parents 73172ab + 3bc27b6 commit cbfc43b

9 files changed

Lines changed: 86 additions & 12 deletions

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
},
4141
"dependencies": {
4242
"@lemoncode/fonk": "^1.3.0",
43-
"@lemoncode/fonk-final-form": "^2.3.1",
4443
"@lemoncode/fonk-formik": "^4.0.1",
4544
"@lemoncode/fonk-match-field-validator": "^1.0.1",
45+
"@lemoncode/fonk-required-by-field-validator": "^1.0.1",
4646
"@material-ui/core": "4.9.11",
4747
"@material-ui/icons": "^4.9.1",
4848
"@material-ui/lab": "4.0.0-alpha.47",
@@ -51,6 +51,7 @@
5151
"final-form": "^4.18.6",
5252
"formik": "^2.1.4",
5353
"graphql-request": "^1.8.2",
54+
"immer": "^6.0.3",
5455
"lodash.flowright": "^3.5.0",
5556
"lodash.get": "^4.4.2",
5657
"lodash.merge": "^4.6.2",

src/pods/employee/components/data.component.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import React from 'react';
22
import { Formik, Form } from 'formik';
33
import { TextFieldComponent, CheckboxComponent } from 'common/components';
4+
import produce from 'immer';
45
import { CommandFooterComponent } from '../../../common-app/command-footer';
56
import * as classes from './data.styles';
67
import { cx } from 'emotion';
78
import { Employee } from '../employee.vm';
9+
import { formValidation, validationSchema } from './data.validations';
10+
import { Validators } from '@lemoncode/fonk';
811

912
interface Props {
1013
employee: Employee;
@@ -21,11 +24,19 @@ export const DataComponent: React.FunctionComponent<Props> = ({
2124
isEditMode,
2225
onCancel,
2326
}) => {
27+
React.useEffect(() => {
28+
const newValidationSchema = produce(validationSchema, darft => {
29+
darft.field.temporalPassword = isEditMode ? [] : [Validators.required];
30+
});
31+
formValidation.updateValidationSchema(newValidationSchema);
32+
}, [isEditMode]);
33+
2434
return (
2535
<Formik
2636
initialValues={employee}
2737
enableReinitialize={true}
2838
onSubmit={onSave}
39+
validate={formValidation.validateForm}
2940
>
3041
{() => (
3142
<Form className={cx(classes.form({ isEditMode }), className)}>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ValidationSchema, Validators } from '@lemoncode/fonk';
2+
import { createFormikValidation } from '@lemoncode/fonk-formik';
3+
import { requiredByField } from '@lemoncode/fonk-required-by-field-validator';
4+
5+
export const validationSchema: ValidationSchema = {
6+
field: {
7+
name: [Validators.required],
8+
email: [
9+
{
10+
validator: Validators.required,
11+
},
12+
{
13+
validator: Validators.email,
14+
},
15+
],
16+
temporalPassword: [
17+
{
18+
validator: requiredByField.validator,
19+
customArgs: {
20+
field: 'id',
21+
condition: fieldValue => fieldValue === '',
22+
},
23+
},
24+
],
25+
},
26+
};
27+
28+
export const formValidation = createFormikValidation(validationSchema);

src/pods/employee/components/report.component.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,36 @@ import { monthList } from 'common/constants';
55
import * as classes from './report.styles';
66
import { CommandFooterComponent } from 'common-app/command-footer';
77
import { cx } from 'emotion';
8+
import { formValidation } from './report.validations';
9+
import { Report } from '../employee.vm';
810

911
interface Props {
12+
report: Report;
1013
className?: string;
1114
onCancel: () => void;
12-
onGenerateExcel: () => void;
15+
onGenerateExcel: (report: Report) => void;
1316
}
1417

1518
export const ReportComponent: React.FunctionComponent<Props> = ({
19+
report,
1620
className,
1721
onCancel,
1822
onGenerateExcel,
1923
}) => {
2024
return (
2125
<Formik
22-
initialValues={{}}
26+
initialValues={report}
2327
enableReinitialize={true}
2428
onSubmit={onGenerateExcel}
29+
validate={formValidation.validateForm}
2530
>
26-
{({ values }) => (
31+
{() => (
2732
<Form className={cx(classes.form, className)}>
2833
<SelectComponent
2934
name="month"
3035
label="Mes"
3136
items={monthList}
3237
className={classes.month}
33-
disabled
3438
/>
3539
<SelectComponent
3640
name="year"
@@ -42,7 +46,6 @@ export const ReportComponent: React.FunctionComponent<Props> = ({
4246
},
4347
]}
4448
className={classes.year}
45-
disabled
4649
/>
4750
<CommandFooterComponent
4851
onCancel={onCancel}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ValidationSchema, Validators } from '@lemoncode/fonk';
2+
import { createFormikValidation } from '@lemoncode/fonk-formik';
3+
4+
const validationSchema: ValidationSchema = {
5+
field: {
6+
month: [Validators.required],
7+
year: [Validators.required],
8+
},
9+
};
10+
11+
export const formValidation = createFormikValidation(validationSchema);

src/pods/employee/employee.component.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@ import {
66
} from 'common/components';
77
import AppBar from '@material-ui/core/AppBar';
88
import { DataComponent, ProjectComponent, ReportComponent } from './components';
9-
import { Employee } from './employee.vm';
9+
import { Employee, Report } from './employee.vm';
1010
import * as classes from './employee.styles';
1111

1212
interface Props {
1313
employee: Employee;
1414
isEditMode: boolean;
15+
report: Report;
1516
onSave: (employee: Employee) => void;
1617
onCancel: () => void;
17-
onGenerateExcel: () => void;
18+
onGenerateExcel: (report: Report) => void;
1819
}
1920

2021
export const EmployeeComponent: React.FunctionComponent<Props> = ({
2122
employee,
2223
isEditMode,
24+
report,
2325
onSave,
2426
onCancel,
2527
onGenerateExcel,
@@ -52,6 +54,7 @@ export const EmployeeComponent: React.FunctionComponent<Props> = ({
5254
</TabPanelComponent>
5355
<TabPanelComponent value={tab} index={2}>
5456
<ReportComponent
57+
report={report}
5558
className={classes.root}
5659
onGenerateExcel={onGenerateExcel}
5760
onCancel={onCancel}

src/pods/employee/employee.container.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React from 'react';
22
import { EmployeeComponent } from './employee.component';
3-
import { Employee, createEmptyEmployee } from './employee.vm';
3+
import {
4+
Employee,
5+
Report,
6+
createEmptyEmployee,
7+
createEmptyReport,
8+
} from './employee.vm';
49
import { useSnackbarContext } from 'common/components';
510
import { trackPromise } from 'react-promise-tracker';
611
import { getEmployeeById } from './api';
@@ -14,6 +19,7 @@ export const EmployeeContainer: React.FunctionComponent = () => {
1419
createEmptyEmployee()
1520
);
1621
const [isEditMode, setIsEditMode] = React.useState<boolean>(false);
22+
const [report, setReport] = React.useState<Report>(createEmptyReport());
1723
const { showMessage } = useSnackbarContext();
1824

1925
const onLoadEmployee = async () => {
@@ -35,7 +41,7 @@ export const EmployeeContainer: React.FunctionComponent = () => {
3541
history.back();
3642
};
3743

38-
const handleGenerateExcel = () => {
44+
const handleGenerateExcel = (report: Report) => {
3945
// Pending to create real implementation
4046
console.log('Excel creado');
4147
};
@@ -52,6 +58,7 @@ export const EmployeeContainer: React.FunctionComponent = () => {
5258
<EmployeeComponent
5359
employee={employee}
5460
isEditMode={isEditMode}
61+
report={report}
5562
onSave={handleSave}
5663
onCancel={handleCancel}
5764
onGenerateExcel={handleGenerateExcel}

src/pods/employee/employee.vm.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export interface ProjectSummary {
1313
projectName: string;
1414
}
1515

16+
export interface Report {
17+
month: string;
18+
year: string;
19+
}
20+
1621
export const createEmptyEmployee = (): Employee => ({
1722
id: '',
1823
name: '',
@@ -21,3 +26,8 @@ export const createEmptyEmployee = (): Employee => ({
2126
temporalPassword: '',
2227
projects: [],
2328
});
29+
30+
export const createEmptyReport = (): Report => ({
31+
month: '',
32+
year: '',
33+
});

src/pods/login/login.validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ValidationSchema, Validators } from '@lemoncode/fonk';
2-
import { createFinalFormValidation } from '@lemoncode/fonk-final-form';
2+
import { createFormikValidation } from '@lemoncode/fonk-formik';
33

44
const validationSchema: ValidationSchema = {
55
field: {
@@ -8,4 +8,4 @@ const validationSchema: ValidationSchema = {
88
},
99
};
1010

11-
export const formValidation = createFinalFormValidation(validationSchema);
11+
export const formValidation = createFormikValidation(validationSchema);

0 commit comments

Comments
 (0)