-
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
Ht1 #6
base: master
Are you sure you want to change the base?
Ht1 #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
## HT1.1 Сделать /admin закрытым роутом, только для авторизированых пользоватилей | ||
## HT1.2 При загрузке брать пользоватиля из сесии | ||
## HT1.1 Сделать /admin закрытым роутом, только для авторизированых пользователей | ||
## HT1.2 При загрузке брать пользователя из сессии | ||
## HT1.3 Реализовать роут для добавления людей(форма, список, чистить форму при успешном добавлении) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import React from 'react'; | ||
|
||
export const AuthContext = React.createContext({user: undefined}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import React from 'react' | ||
import {connect} from "react-redux"; | ||
import {checkAuth} from "../../ducks/auth"; | ||
import {AuthContext} from "../auth/auth-context"; | ||
|
||
class AuthProvider extends React.Component { | ||
render() { | ||
return <AuthContext.Provider value={{user: this.props.auth && this.props.auth.user}}> | ||
{this.props.children} | ||
</AuthContext.Provider>; | ||
} | ||
|
||
componentDidMount() { | ||
this.props.checkAuth(); | ||
} | ||
} | ||
|
||
export default connect(({auth}) => ({auth}), {checkAuth})(AuthProvider); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import React from 'react'; | ||
import {AuthContext} from '../auth/auth-context'; | ||
import {Redirect, Route} from "react-router-dom"; | ||
|
||
const ProtectedRoute = ({component: Component, ...rest}) => ( | ||
<AuthContext.Consumer> | ||
{({ user }) => { | ||
return <Route | ||
render={ | ||
props => | ||
user | ||
? <Component {...props} /> | ||
: <Redirect to="/auth/sign-in" /> | ||
} | ||
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. Это очень плохо читается, вынеси в отдельный метод |
||
{...rest} | ||
/> | ||
}} | ||
</AuthContext.Consumer> | ||
); | ||
export default ProtectedRoute; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, {Component} from 'react' | ||
import {Field, reduxForm} from 'redux-form' | ||
import validator from "email-validator"; | ||
import ErrorField from "../common/error-field"; | ||
|
||
class AddPeopleForm extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<h3>Add user</h3> | ||
<form onSubmit={this.props.handleSubmit}> | ||
<Field component={ErrorField} name="firstName" label="First Name"/> | ||
<Field component={ErrorField} name="secondName" label="Second Name"/> | ||
<Field component={ErrorField} name="email" label="Email"/> | ||
<button type="submit">Add Person</button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
const validate = ({email, firstName, secondName}) => { | ||
const errors = {} | ||
if (!email) errors.email = 'email is a required field' | ||
else if (!validator.validate(email)) errors.email = 'invalid email' | ||
|
||
if (!firstName) errors.firstName = 'firstName is a required field' | ||
if (!secondName) errors.secondName = 'secondName is a required field' | ||
return errors | ||
} | ||
|
||
export default reduxForm({ | ||
form: 'add-people', | ||
validate | ||
})(AddPeopleForm) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, {Component} from 'react' | ||
import {personSelector} from '../../ducks/person'; | ||
import {connect} from "react-redux"; | ||
|
||
class PeopleList extends Component { | ||
render() { | ||
return ( | ||
<div> | ||
<nav> | ||
<ul> | ||
{ | ||
this.props.people.map(person => { | ||
return (<li>{person.firstName} {person.secondName} {person.email}</li>) | ||
}) | ||
} | ||
</ul> | ||
</nav> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default PeopleList | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,7 +51,7 @@ export function signIn(email, password) { | |
|
||
dispatch({ | ||
type: SIGN_IN_SUCCESS, | ||
payload: { user } | ||
payload: {user} | ||
}) | ||
} | ||
} | ||
|
@@ -66,15 +66,24 @@ export function signUp(email, password) { | |
|
||
dispatch({ | ||
type: SIGN_UP_SUCCESS, | ||
payload: { user } | ||
payload: {user} | ||
}) | ||
} | ||
} | ||
|
||
/** | ||
* Init | ||
**/ | ||
|
||
firebase.auth().onAuthStateChanged((user) => { | ||
console.log('--- user', user) | ||
}) | ||
export function checkAuth() { | ||
return async (dispatch) => { | ||
dispatch({ | ||
type: SIGN_IN_START | ||
}); | ||
|
||
firebase.auth().onAuthStateChanged((user) => { | ||
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. Есть подозрение, что без перелогина второй раз checkAuth повесит приложение в SIGN_IN_START.
|
||
if (user) { | ||
dispatch({ | ||
type: SIGN_IN_SUCCESS, | ||
payload: {user} | ||
}); | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import {List, Record} from 'immutable' | ||
import {appName} from '../config' | ||
import {createSelector} from 'reselect' | ||
|
||
/** | ||
* Constants | ||
* */ | ||
export const moduleName = 'person' | ||
const prefix = `${appName}/${moduleName}` | ||
|
||
export const ADD_PERSON = `${prefix}/ADD_PERSON` | ||
|
||
/** | ||
* Reducer | ||
* */ | ||
export const ReducerRecord = Record({ | ||
peopleList: List() | ||
}) | ||
|
||
export default function reducer(state = new ReducerRecord(), action) { | ||
const {type, payload} = action | ||
|
||
switch (type) { | ||
case ADD_PERSON: | ||
return state.set('peopleList', state.peopleList.push(payload.person)) | ||
default: | ||
return state | ||
} | ||
} | ||
|
||
/** | ||
* Selectors | ||
* */ | ||
|
||
export const rootSelector = ({person}) => person | ||
export const personSelector = createSelector( | ||
rootSelector, | ||
(person) => { | ||
return person.peopleList.toArray() | ||
|
||
} | ||
) | ||
|
||
/** | ||
* Action Creators | ||
* */ | ||
|
||
export function addPerson(firstName, secondName, email) { | ||
return (dispatch) => { | ||
dispatch({ | ||
type: ADD_PERSON, | ||
payload: {person: {firstName, secondName, email}} | ||
}) | ||
} | ||
} |
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.
Ок, но не вижу смысла держать в отдельном контексте, если уже есть редакс