From 8becd77be17fa160245a3eebee11a38ea64f9661 Mon Sep 17 00:00:00 2001 From: vitaliy Date: Wed, 4 Jul 2018 21:04:33 +0300 Subject: [PATCH] hometask 1 --- admin/src/App.js | 2 ++ admin/src/components/user/form.js | 39 ++++++++++++++++++++++++++++ admin/src/components/user/list.js | 16 ++++++++++++ admin/src/ducks/users.js | 43 +++++++++++++++++++++++++++++++ admin/src/redux/reducer.js | 4 ++- admin/src/routes/users.js | 29 +++++++++++++++++++++ 6 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 admin/src/components/user/form.js create mode 100644 admin/src/components/user/list.js create mode 100644 admin/src/ducks/users.js create mode 100644 admin/src/routes/users.js diff --git a/admin/src/App.js b/admin/src/App.js index c54f214..24a9920 100644 --- a/admin/src/App.js +++ b/admin/src/App.js @@ -3,6 +3,7 @@ import { Route } from 'react-router-dom' import ProtectedRoute from './components/common/protected-route' import Auth from './routes/auth' import Admin from './routes/admin' +import Users from './routes/usep rs' class App extends Component { render() { @@ -10,6 +11,7 @@ class App extends Component {
+
) } diff --git a/admin/src/components/user/form.js b/admin/src/components/user/form.js new file mode 100644 index 0000000..e039dea --- /dev/null +++ b/admin/src/components/user/form.js @@ -0,0 +1,39 @@ +import React from 'react' +import { reduxForm, Field, reset } from 'redux-form' +import { validate as validateEmail } from 'email-validator' +import ErrorField from '../common/error-field' + +const UserForm = ({ handleSubmit }) => ( +
+

Add User Form

+
+ + + + + + +
+) + +const validate = ({ firstName, lastName, email }) => { + const errors = {} + + if (!email) errors.email = 'email is a required field' + else if (!validateEmail(email)) errors.email = 'email is invalid' + + if (!firstName) errors.firstName = 'password is a required field' + if (!lastName) errors.lastName = 'password is a required field' + + return errors +} + +const formName = 'user-add' + +const afterSubmit = (result, dispatch) => dispatch(reset(formName)) + +export default reduxForm({ + form: formName, + validate, + onSubmitSuccess: afterSubmit +})(UserForm) diff --git a/admin/src/components/user/list.js b/admin/src/components/user/list.js new file mode 100644 index 0000000..ffb00c7 --- /dev/null +++ b/admin/src/components/user/list.js @@ -0,0 +1,16 @@ +import React from 'react' + +const UserList = ({ users }) => ( +
+

User List

+ +
+) + +export default UserList diff --git a/admin/src/ducks/users.js b/admin/src/ducks/users.js new file mode 100644 index 0000000..359d7ad --- /dev/null +++ b/admin/src/ducks/users.js @@ -0,0 +1,43 @@ +import { appName } from '../config' +import { Map, List } from 'immutable' + +/** + * Constants + * */ +export const moduleName = 'users' +const prefix = `${appName}/${moduleName}` + +export const USER_ADD_SUCCESS = `${prefix}/USER_ADD_SUCCESS` + +/** + * Reducer + * */ +const initialState = List() + +export default function reducer(state = initialState, action) { + const { type, payload } = action + + switch (type) { + case USER_ADD_SUCCESS: + return state.push(Map(payload.user)) + default: + return state + } +} + +/** + * Selectors + * */ + +/** + * Action Creators + * */ + +export function addUser(user) { + return (dispatch) => { + dispatch({ + type: USER_ADD_SUCCESS, + payload: { user } + }) + } +} diff --git a/admin/src/redux/reducer.js b/admin/src/redux/reducer.js index 78c74cf..f9c6301 100644 --- a/admin/src/redux/reducer.js +++ b/admin/src/redux/reducer.js @@ -1,8 +1,10 @@ import { combineReducers } from 'redux' import { reducer as form } from 'redux-form' import authReducer, { moduleName as authModule } from '../ducks/auth' +import userReducer, { moduleName as userModule } from '../ducks/users' export default combineReducers({ form, - [authModule]: authReducer + [authModule]: authReducer, + [userModule]: userReducer }) diff --git a/admin/src/routes/users.js b/admin/src/routes/users.js new file mode 100644 index 0000000..896028e --- /dev/null +++ b/admin/src/routes/users.js @@ -0,0 +1,29 @@ +import React, { Component } from 'react' +import { connect } from 'react-redux' +import { addUser } from '../ducks/users' +import UserForm from '../components/user/form' +import UserList from '../components/user/list' + +class Users extends Component { + render() { + const { users, addUser } = this.props + return ( +
+

User List Page

+ + {users.size > 0 && } +
+ ) + } +} + +const mapStateToProps = (state) => { + return { + users: state.users + } +} + +export default connect( + mapStateToProps, + { addUser } +)(Users)