-
Notifications
You must be signed in to change notification settings - Fork 10
hometask 1 #7
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
Open
viprogramm
wants to merge
1
commit into
romabelka:master
Choose a base branch
from
viprogramm:ht1_1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
hometask 1 #7
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 }) => ( | ||
| <div> | ||
| <h3>Add User Form</h3> | ||
| <form onSubmit={handleSubmit}> | ||
| <Field label="First Name" name="firstName" component={ErrorField} /> | ||
| <Field label="Last Name" name="lastName" component={ErrorField} /> | ||
| <Field label="Email" name="email" component={ErrorField} /> | ||
|
|
||
| <button type="submit">Add User</button> | ||
| </form> | ||
| </div> | ||
| ) | ||
|
|
||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import React from 'react' | ||
|
|
||
| const UserList = ({ users }) => ( | ||
| <div> | ||
| <h3>User List</h3> | ||
| <ul> | ||
| {users.map((user, index) => ( | ||
| <li key={index}> | ||
| {user.get('firstName')} {user.get('lastName')} {user.get('email')} | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </div> | ||
| ) | ||
|
|
||
| export default UserList |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше Record вместо Map |
||
| default: | ||
| return state | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Selectors | ||
| * */ | ||
|
|
||
| /** | ||
| * Action Creators | ||
| * */ | ||
|
|
||
| export function addUser(user) { | ||
| return (dispatch) => { | ||
| dispatch({ | ||
| type: USER_ADD_SUCCESS, | ||
| payload: { user } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ( | ||
| <div> | ||
| <h1>User List Page</h1> | ||
| <UserForm onSubmit={addUser} /> | ||
| {users.size > 0 && <UserList users={users} />} | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| const mapStateToProps = (state) => { | ||
| return { | ||
| users: state.users | ||
| } | ||
| } | ||
|
|
||
| export default connect( | ||
| mapStateToProps, | ||
| { addUser } | ||
| )(Users) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Я бы чистил форму где-то в redux, в будущем это должно происходить после успешного добавления на сервер