-
Notifications
You must be signed in to change notification settings - Fork 13
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
Hometask for 05.02 #9
Open
magicwolf9
wants to merge
3
commits into
romabelka:master
Choose a base branch
from
magicwolf9:hometask_for_05.02
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
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 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 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,40 @@ | ||
import React from 'react' | ||
import {Field, reduxForm} from 'redux-form' | ||
import ErrorField from "../common/error-field"; | ||
import validator from 'email-validator' | ||
|
||
class AddPersonForm extends React.Component { | ||
static propTypes = {} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h3>Add person</h3> | ||
<form onSubmit={this.props.handleSubmit}> | ||
<Field name="firstName" component={ErrorField} label="First name"/> | ||
<Field name="secondName" component={ErrorField} label="Second name"/> | ||
<Field name="email" component={ErrorField} label="Email"/> | ||
<button type="submit">Save</button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const validate = ({firstName, secondName, email}) => { | ||
const errors = {} | ||
|
||
if (!firstName) errors.firstName = 'First name is a required field' | ||
|
||
if (!secondName) errors.secondName = 'Second name is a required field' | ||
|
||
if (!email) errors.email = 'email is a required field' | ||
else if (!validator.validate(email)) errors.email = 'invalid email' | ||
|
||
return errors | ||
} | ||
|
||
export default reduxForm({ | ||
form: 'add-person', | ||
validate | ||
})(AddPersonForm) |
This file contains 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,22 @@ | ||
import React from 'react' | ||
import {connect} from 'react-redux' | ||
import {Redirect} from 'react-router-dom' | ||
import {getUser, getAuthStatus, AUTH_PENDING} from '../../ducks/auth' | ||
|
||
class LockedRoutesBounder extends React.Component { | ||
|
||
render() { | ||
if(this.props.authStatus === AUTH_PENDING) return <div>Загрузка...</div> | ||
|
||
if (!this.props.user) return <Redirect to={'/auth'}/> | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default connect((state) => { | ||
return { | ||
user: getUser(state), | ||
authStatus: getAuthStatus(state) | ||
} | ||
})(LockedRoutesBounder) |
This file contains 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,17 +1,38 @@ | ||
import React, { Component } from 'react' | ||
import React, {Component} from 'react' | ||
import LockedRoutesBounder from '../../common/locked-routes-bounder' | ||
import AddPersonForm from '../../admin/add-person-form' | ||
import {getPersons, savePerson} from "../../../ducks/persons"; | ||
import {connect} from 'react-redux' | ||
|
||
class AdminPage extends Component { | ||
static propTypes = { | ||
|
||
} | ||
static propTypes = {} | ||
|
||
render() { | ||
return ( | ||
<div> | ||
<h1>Admin</h1> | ||
</div> | ||
<LockedRoutesBounder> | ||
<div> | ||
<h1>Admin</h1> | ||
</div> | ||
<AddPersonForm onSubmit={this.handleSavePerson}/> | ||
<div> | ||
{this.props.persons.map((person) => <div> | ||
Имя: {person.firstName} | ||
Фамилия: {person.secondName} | ||
Почта: {person.email} | ||
</div>) | ||
} | ||
</div> | ||
</LockedRoutesBounder> | ||
) | ||
} | ||
|
||
handleSavePerson = (personData) => this.props.savePerson(personData) | ||
} | ||
|
||
export default AdminPage | ||
export default connect(state => { | ||
return { | ||
persons: getPersons(state) | ||
} | ||
}, { | ||
savePerson | ||
})(AdminPage) |
This file contains 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 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,61 @@ | ||
import {Record, List} from 'immutable' | ||
import {appName} from '../config' | ||
import {reset} from 'redux-form'; | ||
|
||
|
||
|
||
/** | ||
* Constants | ||
* */ | ||
export const moduleName = 'auth' | ||
const prefix = `${appName}/${moduleName}` | ||
|
||
export const PERSON_SAVED = `${prefix}/PERSON_SAVED` | ||
|
||
/** | ||
* Reducer | ||
* */ | ||
|
||
export const PersonRecord = new Record({ | ||
id: null, | ||
firstName: null, | ||
secondName: null, | ||
email: null | ||
}) | ||
|
||
export const StateRecord = new Record({ | ||
personsList: new List() | ||
}) | ||
|
||
export default function reducer(state = new StateRecord(), action) { | ||
const {type, payload} = action; | ||
|
||
switch (type) { | ||
case PERSON_SAVED: | ||
const personRecord = new PersonRecord({id: state.size + 1, ...payload.personData}) | ||
return {personsList: state.personsList.push(personRecord)}; | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
/** | ||
* Selectors | ||
* */ | ||
|
||
export const getPersons = state => state.persons.personsList | ||
|
||
|
||
/** | ||
* Action Creators | ||
* */ | ||
|
||
export function savePerson(personData) { | ||
return function(dispatch) { | ||
dispatch({ | ||
type: PERSON_SAVED, | ||
payload: {personData} | ||
}) | ||
dispatch(reset('add-person')) | ||
} | ||
} |
This file contains 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 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
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.
Лучше селектор для этого заведи