Skip to content

Commit 38e99f0

Browse files
committed
Merge pull request #1135 from thomasjshafer/survey-validate-api
addressed TODO: made SurveyForm use api endpoint for validation
2 parents 3e8453d + 831bf04 commit 38e99f0

File tree

5 files changed

+65
-19
lines changed

5 files changed

+65
-19
lines changed

api/actions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export loadAuth from './loadAuth';
33
export login from './login';
44
export logout from './logout';
55
export * as widget from './widget/index';
6+
export * as survey from './survey/index';

api/actions/survey/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export isValid from './isValid';

api/actions/survey/isValid.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function survey(req) {
2+
return new Promise((resolve, reject) => {
3+
setTimeout(() => {
4+
const errors = {};
5+
let valid = true;
6+
if (~['[email protected]', '[email protected]'].indexOf(req.body.email)) {
7+
errors.email = 'Email address already used';
8+
valid = false;
9+
}
10+
if (valid) {
11+
resolve();
12+
} else {
13+
reject(errors);
14+
}
15+
}, 1000);
16+
});
17+
}

src/components/SurveyForm/SurveyForm.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import React, {Component, PropTypes} from 'react';
22
import {reduxForm} from 'redux-form';
3+
import {connect} from 'react-redux';
4+
import {bindActionCreators} from 'redux';
35
import surveyValidation from './surveyValidation';
6+
import * as surveyActions from 'redux/modules/survey';
47

5-
function asyncValidate(data) {
6-
// TODO: figure out a way to move this to the server. need an instance of ApiClient
8+
function asyncValidate(data, dispatch, {isValidEmail}) {
79
if (!data.email) {
810
return Promise.resolve({});
911
}
10-
return new Promise((resolve, reject) => {
11-
setTimeout(() => {
12-
const errors = {};
13-
let valid = true;
14-
if (~['[email protected]', '[email protected]'].indexOf(data.email)) {
15-
errors.email = 'Email address already used';
16-
valid = false;
17-
}
18-
if (valid) {
19-
resolve();
20-
} else {
21-
reject(errors);
22-
}
23-
}, 1000);
24-
});
12+
return isValidEmail(data);
2513
}
26-
14+
@connect(() => ({}),
15+
dispatch => bindActionCreators(surveyActions, dispatch)
16+
)
2717
@reduxForm({
2818
form: 'survey',
2919
fields: ['name', 'email', 'occupation', 'currentlyEmployed', 'sex'],
@@ -137,4 +127,3 @@ class SurveyForm extends Component {
137127
);
138128
}
139129
}
140-

src/redux/modules/survey.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const IS_VALID = 'redux-example/survey/IS_VALID';
2+
const IS_VALID_SUCCESS = 'redux-example/survey/IS_VALID_SUCCESS';
3+
const IS_VALID_FAIL = 'redux-example/survey/IS_VALID_FAIL';
4+
5+
const initialState = {
6+
saveError: null,
7+
};
8+
9+
export default function reducer(state = initialState, action = {}) {
10+
switch (action.type) {
11+
case IS_VALID:
12+
return state; // 'saving' flag handled by redux-form
13+
case IS_VALID_SUCCESS:
14+
const data = [...state.data];
15+
data[action.result.id - 1] = action.result;
16+
return {
17+
...state,
18+
data: data,
19+
saveError: null,
20+
};
21+
case IS_VALID_FAIL:
22+
return typeof action.error === 'string' ? {
23+
...state,
24+
saveError: action.error
25+
} : state;
26+
default:
27+
return state;
28+
}
29+
}
30+
31+
export function isValidEmail(data) {
32+
return {
33+
types: [IS_VALID, IS_VALID_SUCCESS, IS_VALID_FAIL],
34+
promise: (client) => client.post('/survey/isValid', {
35+
data
36+
})
37+
};
38+
}

0 commit comments

Comments
 (0)