Skip to content
Open

H2 #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/components/people/PeopleForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React, { Component } from 'react'
import { reduxForm, Field } from 'redux-form'
import ErrorField from '../common/ErrorField'
import emailValidator from 'email-validator'
import { moduleName, addUser} from '../../ducks/people'
import { connect } from 'react-redux'

class PeopleForm extends Component {
render() {
const { handleSubmit, submitting, reset } = this.props;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Возможно у тебя у тебя какие-то особые соображения, поделись. Но вся суть в том, что ты можешь ресетить как часть декларативной логики, при успешном сабмите, а не по клику

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ясно

return (
<div>
<h2>
Add person
</h2>
<form onSubmit={handleSubmit(this.props.addUser)}>
<Field name="first_name" component={ErrorField} />
<Field name="last_name" component={ErrorField} />
<Field name="email" component={ErrorField} />
<div>
<input type="submit" disabled={submitting} onClick={reset} />
</div>
</form>
</div>
);
}
}

const validate = ({first_name, last_name, email}) => {
const errors = {}

if (!email) errors.email = 'email is required'
else if (!emailValidator.validate(email)) errors.email = 'invalid email'

if (!first_name) errors.first_name = 'first_name is required'

if(!last_name) errors.last_name = 'last_name is required'

return errors
}

const mapStateToProps = (state) => ({
loading: state[moduleName.loading]
});

const mapDispatchToProps = (dispatch) => ({

addUser: ({first_name, last_name, email}) => dispatch(addUser({first_name, last_name, email}))
});

PeopleForm = connect(
mapStateToProps,
mapDispatchToProps
)(PeopleForm);

export default reduxForm({
form: 'people',
validate,
})(PeopleForm);
6 changes: 3 additions & 3 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import firebase from 'firebase'

export const appName = "advreact-21-08"
export const appName = "adv-21-08"
export const firebaseConfig = {
apiKey: "AIzaSyDjA6CeIHuni5lNm4ML1b-TSxJltsYUO8g",
apiKey: "AIzaSyAFf2xnTf-RQAxK0W_QWKbsf8nEW0vJ66Y",
authDomain: `${appName}.firebaseapp.com`,
databaseURL: `https://${appName}.firebaseio.com`,
projectId: appName,
storageBucket: `${appName}.appspot.com`,
messagingSenderId: "789814589283"
messagingSenderId: "497019564518"
}

firebase.initializeApp(firebaseConfig)
24 changes: 17 additions & 7 deletions src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Record} from 'immutable'
import {all, cps, call, put, take, takeEvery} from 'redux-saga/effects'
import {push} from 'react-router-redux'

export const ReducerRecord = Record({
const ReducerRecord = Record({
user: null,
error: null,
loading: false
Expand All @@ -16,8 +16,8 @@ export const SIGN_UP_REQUEST = `${appName}/${moduleName}/SIGN_UP_REQUEST`
export const SIGN_UP_SUCCESS = `${appName}/${moduleName}/SIGN_UP_SUCCESS`
export const SIGN_UP_ERROR = `${appName}/${moduleName}/SIGN_UP_ERROR`
export const SIGN_IN_REQUEST = `${appName}/${moduleName}/SIGN_IN_REQUEST`
export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR`
export const SIGN_IN_SUCCESS = `${appName}/${moduleName}/SIGN_IN_SUCCESS`
export const SIGN_IN_ERROR = `${appName}/${moduleName}/SIGN_IN_ERROR`
export const SIGN_OUT_REQUEST = `${appName}/${moduleName}/SIGN_OUT_REQUEST`
export const SIGN_OUT_SUCCESS = `${appName}/${moduleName}/SIGN_OUT_SUCCESS`

Expand All @@ -27,16 +27,25 @@ export default function reducer(state = new ReducerRecord(), action) {

switch (type) {
case SIGN_UP_REQUEST:
case SIGN_IN_REQUEST:
return state.set('loading', true)

case SIGN_UP_SUCCESS:
return state
.set('loading', false)
.set('user', payload.user)
.set('error', null)

case SIGN_IN_SUCCESS:
return state
.set('loading', false)
.set('user', payload.user)
.set('error', null)

case SIGN_UP_ERROR:
return state
.set('loading', false)
.set('error', error)

case SIGN_IN_ERROR:
return state
.set('loading', false)
Expand Down Expand Up @@ -75,7 +84,7 @@ export const signUpSaga = function * () {

while (true) {
const action = yield take(SIGN_UP_REQUEST)

try {
const user = yield call(
[auth, auth.createUserWithEmailAndPassword],
Expand All @@ -96,11 +105,12 @@ export const signUpSaga = function * () {

export const signInSaga = function * () {
const auth = firebase.auth()

while (true) {
const action = yield take(SIGN_IN_REQUEST)

const action = yield take(SIGN_IN_REQUEST);
// debugger
try {
//UserCredential
const user = yield call(
[auth, auth.signInWithEmailAndPassword],
action.payload.email, action.payload.password
Expand Down
2 changes: 1 addition & 1 deletion src/redux/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import rootSaga from './saga'
const sagaMiddleware = createSagaMiddleware()
const enhancer = applyMiddleware(sagaMiddleware, routerMiddleware(history), logger)

const store = createStore(reducer, enhancer)
const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),enhancer)
window.store = store

sagaMiddleware.run(rootSaga)
Expand Down