forked from paralect/react-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.actions.js
More file actions
64 lines (53 loc) · 1.9 KB
/
user.actions.js
File metadata and controls
64 lines (53 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// @flow
import { string, object } from 'yup';
import _isEmpty from 'lodash/isEmpty';
import { validate, validateField } from 'helpers/validation';
import type { ValidationResultType, ValidationResultErrorsType } from 'helpers/validation/types';
import * as api from './user.api';
import type { ActionType, StateType } from './user.types';
const schema = object({
firstName: string()
.trim()
.required('Your first name must be longer than 1 letter'),
lastName: string()
.trim()
.required('Your last name must be longer than 1 letter'),
email: string()
.trim()
.lowercase()
.required('Email is required')
.email('Please enter a valid email address'),
});
export const FETCH_USER = 'fetchUser';
export const UPDATE_USER = 'updateUser';
type DispatchFnType = (obj: ActionType | Promise<ActionType>) => void;
type VoidFnType = (dispatch: DispatchFnType) => Promise<*>;
export const fetchUser = (id: string): VoidFnType => (dispatch: DispatchFnType): Promise<StateType> => {
return api.fetchUser(id).then((payload: StateType): StateType => {
dispatch({ type: FETCH_USER, payload });
return payload;
});
};
export const validateUserField = (data: $Shape<StateType>, field: string): Promise<ValidationResultType> => {
return validateField(data, field, schema);
};
export const validateUser = async (data: $Shape<StateType>): Promise<ValidationResultErrorsType> => {
const result: ValidationResultType = await validate(data, schema);
const isValid: boolean = _isEmpty(result.errors);
return {
errors: {
...result.errors,
_global: ['Validation Error.'],
},
isValid,
};
};
export const updateUser = (id: string, data: StateType): VoidFnType => (dispatch: DispatchFnType): Promise<*> => {
return api.updateUser(id, data).then((payload: StateType): StateType => {
dispatch({
type: UPDATE_USER,
payload,
});
return payload;
});
};