Skip to content
Open

H2 #13

Show file tree
Hide file tree
Changes from 6 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
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
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)
48 changes: 47 additions & 1 deletion src/ducks/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const moduleName = 'auth'
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_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,6 +29,12 @@ export default function reducer(state = new ReducerRecord(), action) {
case SIGN_UP_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)
Expand All @@ -38,6 +46,11 @@ export default function reducer(state = new ReducerRecord(), action) {
.set('loading', false)
.set('error', error)

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

case SIGN_OUT_SUCCESS:
return new ReducerRecord()

Expand All @@ -53,6 +66,13 @@ export function signUp(email, password) {
}
}

export function signIn(email, password) {
return {
type: SIGN_IN_REQUEST,
payload: {email, password}
}
}

export function signOut() {
return {
type: SIGN_OUT_REQUEST
Expand All @@ -64,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 @@ -83,6 +103,31 @@ export const signUpSaga = function * () {
}
}

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

while (true) {
const action = yield take(SIGN_IN_REQUEST);
// debugger
try {
//UserCredential
const user = yield call(
[auth, auth.signInWithEmailAndPassword],
action.payload.email, action.payload.password
)
yield put({
type: SIGN_IN_SUCCESS,
payload: {user}
})
} catch (error) {
yield put({
type: SIGN_IN_ERROR,
error
})
}
}
}

/*
export function signUp(email, password) {
return (dispatch) => {
Expand Down Expand Up @@ -143,6 +188,7 @@ export const signOutSaga = function * () {
export const saga = function * () {
yield all([
signUpSaga(),
signInSaga(),
watchStatusChange(),
takeEvery(SIGN_OUT_REQUEST, signOutSaga)
])
Expand Down
48 changes: 48 additions & 0 deletions src/ducks/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
SIGN_IN_REQUEST,
SIGN_UP_REQUEST,
SIGN_IN_SUCCESS,
SIGN_UP_SUCCESS,
SIGN_IN_ERROR,
SIGN_UP_ERROR,
signIn,
signUp,
signUpSaga,
signInSaga,
signOut
} from './auth'
import firebase from 'firebase'
import {call, put, take} from 'redux-saga/effects'
const AUTH = firebase.auth()
const person = {
email: 'kaf_90@mail.ru',
password: '11111111'
}

it('auth by sign up', () => {

const saga = signUpSaga()
expect(saga.next().value).toEqual(take(SIGN_UP_REQUEST))
expect(saga.next({type: SIGN_UP_REQUEST, payload: person}).value).toEqual(call([AUTH, AUTH.createUserWithEmailAndPassword], person.email, person.password));
expect(saga.next(person).value).toEqual(put({
type:SIGN_UP_SUCCESS,
payload:{user:person}
}))
// expect(saga.throw()).toEqual({
// type: SIGN_UP_ERROR
// })
})


it('auth by sign in', () => {
const saga = signInSaga()
expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST))
expect(saga.next({type: SIGN_IN_REQUEST, payload: person}).value).toEqual(call([AUTH, AUTH.signInWithEmailAndPassword], person.email, person.password));
expect(saga.next(person).value).toEqual(put({
type:SIGN_IN_SUCCESS,
payload:{user:person}
}))
// expect(saga.throw()).toEqual({
// type: SIGN_UP_ERROR
// })
})
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
46 changes: 41 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,10 @@ acorn@^5.0.0, acorn@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/acorn/-/acorn-5.1.1.tgz#53fe161111f912ab999ee887a90a0bc52822fd75"

address@1.0.2:
address@1.0.2, address@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/address/-/address-1.0.2.tgz#480081e82b587ba319459fef512f516fe03d58af"

address@^1.0.1:
version "1.0.3"
resolved "https://registry.yarnpkg.com/address/-/address-1.0.3.tgz#b5f50631f8d6cec8bd20c963963afb55e06cbce9"

ajv-keywords@^1.0.0:
version "1.5.1"
resolved "https://registry.yarnpkg.com/ajv-keywords/-/ajv-keywords-1.5.1.tgz#314dd0a4b3368fad3dfcdc54ede6171b886daf3c"
Expand Down Expand Up @@ -338,7 +334,11 @@ babel-code-frame@^6.11.0, babel-code-frame@^6.22.0, babel-code-frame@^6.26.0:
esutils "^2.0.2"
js-tokens "^3.0.2"

<<<<<<< HEAD
babel-core@6.25.0:
=======
babel-core@6.25.0, babel-core@^6.0.0:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "6.25.0"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.25.0.tgz#7dd42b0463c742e9d5296deb3ec67a9322dad729"
dependencies:
Expand All @@ -362,7 +362,11 @@ babel-core@6.25.0:
slash "^1.0.0"
source-map "^0.5.0"

<<<<<<< HEAD
babel-core@^6.0.0, babel-core@^6.26.0:
=======
babel-core@^6.26.0:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-6.26.0.tgz#af32f78b31a6fcef119c87b0fd8d9753f03a0bb8"
dependencies:
Expand Down Expand Up @@ -838,18 +842,25 @@ babel-plugin-transform-react-jsx@6.24.1, babel-plugin-transform-react-jsx@^6.24.
babel-plugin-syntax-jsx "^6.8.0"
babel-runtime "^6.22.0"

<<<<<<< HEAD
babel-plugin-transform-regenerator@6.24.1:
=======
babel-plugin-transform-regenerator@6.24.1, babel-plugin-transform-regenerator@^6.22.0:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.24.1.tgz#b8da305ad43c3c99b4848e4fe4037b770d23c418"
dependencies:
regenerator-transform "0.9.11"

<<<<<<< HEAD
babel-plugin-transform-regenerator@^6.22.0:
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-regenerator/-/babel-plugin-transform-regenerator-6.26.0.tgz#e0703696fbde27f0a3efcacf8b4dca2f7b3a8f2f"
dependencies:
regenerator-transform "^0.10.0"

=======
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
babel-plugin-transform-runtime@6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-runtime/-/babel-plugin-transform-runtime-6.23.0.tgz#88490d446502ea9b8e7efb0fe09ec4d99479b1ee"
Expand Down Expand Up @@ -950,14 +961,22 @@ babel-register@^6.24.1, babel-register@^6.26.0:
mkdirp "^0.5.1"
source-map-support "^0.4.15"

<<<<<<< HEAD
babel-runtime@6.23.0:
=======
babel-runtime@6.23.0, babel-runtime@^6.22.0:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.23.0.tgz#0a9489f144de70efb3ce4300accdb329e2fc543b"
dependencies:
core-js "^2.4.0"
regenerator-runtime "^0.10.0"

<<<<<<< HEAD
babel-runtime@^6.18.0, babel-runtime@^6.22.0, babel-runtime@^6.26.0:
=======
babel-runtime@^6.18.0, babel-runtime@^6.26.0:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "6.26.0"
resolved "https://registry.yarnpkg.com/babel-runtime/-/babel-runtime-6.26.0.tgz#965c7058668e82b55d7bfe04ff2337bc8b5647fe"
dependencies:
Expand Down Expand Up @@ -4199,18 +4218,25 @@ minimatch@^3.0.0, minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4:
dependencies:
brace-expansion "^1.1.7"

<<<<<<< HEAD
minimist@0.0.8:
=======
minimist@0.0.8, minimist@~0.0.1:
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
version "0.0.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.8.tgz#857fcabfc3397d2625b8228262e86aa7a011b05d"

minimist@^1.1.1, minimist@^1.1.3, minimist@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"

<<<<<<< HEAD
minimist@~0.0.1:
version "0.0.10"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf"

=======
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
mkdirp@0.5.1, mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@~0.5.1:
version "0.5.1"
resolved "https://registry.yarnpkg.com/mkdirp/-/mkdirp-0.5.1.tgz#30057438eac6cf7f8c4767f38648d6697d75c903"
Expand Down Expand Up @@ -5440,6 +5466,7 @@ regenerator-transform@0.9.11:
babel-types "^6.19.0"
private "^0.1.6"

<<<<<<< HEAD
regenerator-transform@^0.10.0:
version "0.10.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.10.1.tgz#1e4996837231da8b7f3cf4114d71b5691a0680dd"
Expand All @@ -5448,6 +5475,8 @@ regenerator-transform@^0.10.0:
babel-types "^6.19.0"
private "^0.1.6"

=======
>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
regex-cache@^0.4.2:
version "0.4.3"
resolved "https://registry.yarnpkg.com/regex-cache/-/regex-cache-0.4.3.tgz#9b1a6c35d4d0dfcef5711ae651e8e9d3d7114145"
Expand Down Expand Up @@ -6600,6 +6629,7 @@ window-size@0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/window-size/-/window-size-0.1.0.tgz#5438cd2ea93b202efa3a19fe8887aee7c94f9c9d"

<<<<<<< HEAD
wordwrap@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
Expand All @@ -6608,6 +6638,12 @@ wordwrap@~0.0.2:
version "0.0.3"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"

=======
wordwrap@0.0.2, wordwrap@~0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"

>>>>>>> 0b7f579abde91b9b4dc38ae56fedd5640a1e556a
wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
Expand Down