-
Notifications
You must be signed in to change notification settings - Fork 10
Ht2 Smotrova Lilit #11
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
SmotrovaLilit
wants to merge
3
commits into
romabelka:master
Choose a base branch
from
SmotrovaLilit:ht2
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
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import React from 'react' | ||
|
|
||
| function Loader() { | ||
| return <h1>Loading...</h1> | ||
| } | ||
|
|
||
| Loader.propTypes = {} | ||
|
|
||
| export default Loader |
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,40 @@ | ||
| import React, { Component } from 'react' | ||
| import { connect } from 'react-redux' | ||
| import { | ||
| loadAllEvents, | ||
| eventSelector, | ||
| loadingEventsSelector | ||
| } from '../../ducks/event' | ||
| import Event from './event' | ||
| import Loader from '../common/loader' | ||
|
|
||
| class EventList extends Component { | ||
| static propTypes = {} | ||
|
|
||
| constructor(props) { | ||
| super(props) | ||
| props.fetchData && props.fetchData() | ||
| } | ||
|
|
||
| render() { | ||
| const { events, loading } = this.props | ||
| if (loading) return <Loader /> | ||
| return ( | ||
| <ul> | ||
| {events.map((event, key) => ( | ||
| <li key={key}> | ||
| <Event event={event} /> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default connect( | ||
| (state) => ({ | ||
| events: eventSelector(state), | ||
| loading: loadingEventsSelector(state) | ||
| }), | ||
| { fetchData: loadAllEvents } | ||
| )(EventList) |
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,31 @@ | ||
| import React, { Component } from 'react' | ||
| import PropTypes from 'prop-types' | ||
|
|
||
| class Event extends Component { | ||
| static propTypes = { | ||
| event: PropTypes.shape({ | ||
| month: PropTypes.string, | ||
| submissionDeadline: PropTypes.string, | ||
| title: PropTypes.string, | ||
| url: PropTypes.string, | ||
| where: PropTypes.string, | ||
| when: PropTypes.string | ||
| }).isRequired | ||
| } | ||
|
|
||
| render() { | ||
| const { title, url, where, when } = this.props.event | ||
| return ( | ||
| <div> | ||
| <a target="_blank" href={url}> | ||
| {title} | ||
| </a> | ||
| <div> | ||
| {where}, {when} | ||
| </div> | ||
| </div> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default Event |
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,158 @@ | ||
| import { call, put, take, apply } from 'redux-saga/effects' | ||
| import { | ||
| SIGN_UP_ERROR, | ||
| SIGN_UP_REQUEST, | ||
| SIGN_UP_SUCCESS, | ||
| SIGN_IN_REQUEST, | ||
| signUpSaga, | ||
| signInSaga, | ||
| signIn, | ||
| SIGN_IN_ERROR, | ||
| SIGN_IN_MAX_TRIES_ERROR | ||
| } from './auth' | ||
| import firebase from 'firebase/app' | ||
|
|
||
| describe('signUpSaga', () => { | ||
| it('should signup success', () => { | ||
| const user = { | ||
| email: 'test@javascript.ru', | ||
| password: '12345678' | ||
| } | ||
| const action = { | ||
| type: SIGN_UP_REQUEST, | ||
| payload: { ...user } | ||
| } | ||
|
|
||
| const saga = signUpSaga(action) | ||
|
|
||
| const auth = firebase.auth() | ||
| expect(saga.next().value).toEqual( | ||
| call( | ||
| [auth, auth.createUserWithEmailAndPassword], | ||
| user.email, | ||
| user.password | ||
| ) | ||
| ) | ||
|
|
||
| expect(saga.next(user).value).toEqual( | ||
| put({ | ||
| type: SIGN_UP_SUCCESS, | ||
| payload: { user } | ||
| }) | ||
| ) | ||
|
|
||
| expect(saga.next().done).toBe(true) | ||
| }) | ||
| it('should signup error', () => { | ||
| const user = { | ||
| email: 'test@javascript.ru', | ||
| password: '12345678' | ||
| } | ||
| const action = { | ||
| type: SIGN_UP_REQUEST, | ||
| payload: { ...user } | ||
| } | ||
|
|
||
| const saga = signUpSaga(action) | ||
|
|
||
| const auth = firebase.auth() | ||
| expect(saga.next().value).toEqual( | ||
| call( | ||
| [auth, auth.createUserWithEmailAndPassword], | ||
| user.email, | ||
| user.password | ||
| ) | ||
| ) | ||
|
|
||
| const error = 'error' | ||
| expect(saga.throw(error).value).toEqual( | ||
| put({ | ||
| type: SIGN_UP_ERROR, | ||
| error: error | ||
| }) | ||
| ) | ||
|
|
||
| expect(saga.next().done).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('signInSaga', () => { | ||
| it('should signin success', () => { | ||
| const user = { | ||
| email: 'test@javascript.ru', | ||
| password: '12345678' | ||
| } | ||
|
|
||
| const saga = signInSaga() | ||
|
|
||
| const auth = firebase.auth() | ||
| expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST)) | ||
| signIn(user.email, user.password) | ||
| expect( | ||
| saga.next({ | ||
| type: SIGN_IN_REQUEST, | ||
| payload: user | ||
| }).value | ||
| ).toEqual( | ||
| apply(auth, auth.signInWithEmailAndPassword, [user.email, user.password]) | ||
| ) | ||
|
|
||
| expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST)) | ||
| }) | ||
|
|
||
| it('should signin error if invalid credentials', () => { | ||
| const user = { | ||
| email: 'test@javascript.ru', | ||
| password: '12345678' | ||
| } | ||
|
|
||
| const saga = signInSaga() | ||
|
|
||
| const auth = firebase.auth() | ||
| expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST)) | ||
| signIn(user.email, user.password) | ||
|
|
||
| expect( | ||
| saga.next({ | ||
| type: SIGN_IN_REQUEST, | ||
| payload: user | ||
| }).value | ||
| ).toEqual( | ||
| apply(auth, auth.signInWithEmailAndPassword, [user.email, user.password]) | ||
| ) | ||
|
|
||
| const error = 'error' | ||
| expect(saga.throw(error).value).toEqual( | ||
| put({ | ||
| type: SIGN_IN_ERROR, | ||
| error: error | ||
| }) | ||
| ) | ||
|
|
||
| expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST)) | ||
| }) | ||
| it('should signin error if more then 3 times', () => { | ||
| const user = { | ||
| email: 'test@javascript.ru', | ||
| password: '12345678' | ||
| } | ||
|
|
||
| const saga = signInSaga() | ||
|
|
||
| const auth = firebase.auth() | ||
| for (let i = 0; i < 3; i++) { | ||
| expect(saga.next().value).toEqual(take(SIGN_IN_REQUEST)) | ||
| signIn(user.email, user.password) | ||
| saga.next({ | ||
| type: SIGN_IN_REQUEST, | ||
| payload: user | ||
| }) | ||
| } | ||
|
|
||
| expect(saga.next().value).toEqual( | ||
| put({ | ||
| type: SIGN_IN_MAX_TRIES_ERROR | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
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,103 @@ | ||
| import { appName } from '../config' | ||
| import { Record, List } from 'immutable' | ||
| import { createSelector } from 'reselect' | ||
| import { takeEvery, call, put } from 'redux-saga/effects' | ||
| import firebase from 'firebase/app' | ||
| import Immutable from 'immutable' | ||
|
|
||
| /** | ||
| * Constants | ||
| * */ | ||
| export const moduleName = 'event' | ||
| const prefix = `${appName}/${moduleName}` | ||
| export const LOAD_ALL_EVENTS = `${prefix}/LOAD_ALL_EVENTS` | ||
| export const LOAD_ALL_EVENTS_SUCCESS = `${prefix}/LOAD_ALL_EVENTS_SUCCESS` | ||
| export const LOAD_ALL_EVENTS_ERROR = `${prefix}/LOAD_ALL_EVENTS_ERROR` | ||
| export const LOAD_ALL_EVENTS_START = `${prefix}/LOAD_ALL_EVENTS_START` | ||
|
|
||
| /** | ||
| * Reducer | ||
| * */ | ||
| const ReducerState = Record({ | ||
| entities: new List([]), | ||
| loading: false, | ||
| error: null | ||
| }) | ||
|
|
||
| const EventRecord = Record({ | ||
| id: null, | ||
| title: null, | ||
| url: null, | ||
| where: null, | ||
| when: null, | ||
| submissionDeadline: null, | ||
| month: null | ||
| }) | ||
|
|
||
| export default function reducer(state = new ReducerState(), action) { | ||
| const { type, payload } = action | ||
|
|
||
| switch (type) { | ||
| case LOAD_ALL_EVENTS_START: | ||
| return state.set('loading', true) | ||
| case LOAD_ALL_EVENTS_SUCCESS: | ||
| return state.set('entities', payload.events).set('loading', false) | ||
|
|
||
| default: | ||
| return state | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Action Creators | ||
| * */ | ||
|
|
||
| export function loadAllEvents() { | ||
| return { | ||
| type: LOAD_ALL_EVENTS, | ||
| payload: {} | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Selectors | ||
| * */ | ||
| export const stateSelector = (state) => state[moduleName] | ||
| export const eventSelector = createSelector(stateSelector, (state) => { | ||
| return state.entities.valueSeq().toArray() | ||
| }) | ||
| export const loadingEventsSelector = createSelector( | ||
| stateSelector, | ||
| (state) => state.loading | ||
| ) | ||
|
|
||
| /** | ||
| * Sagas | ||
| */ | ||
| export function* loadAllEventsSaga(action) { | ||
| yield put({ | ||
| type: LOAD_ALL_EVENTS_START, | ||
| payload: { loading: true, events: new List([]) } | ||
| }) | ||
| const eventsRef = firebase.database().ref('/events') | ||
| try { | ||
| const eventsResponse = yield call([eventsRef, eventsRef.once], 'value') | ||
| const events = Immutable.Map(eventsResponse.val()).reduce( | ||
|
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. я бы такое в редюсере делал, а еще лучше - так бы и хранил |
||
| (acc, item) => acc.push(new EventRecord(item)), | ||
| new List([]) | ||
| ) | ||
| yield put({ | ||
| type: LOAD_ALL_EVENTS_SUCCESS, | ||
| payload: { loading: false, events: events } | ||
| }) | ||
| } catch (e) { | ||
| yield put({ | ||
| type: LOAD_ALL_EVENTS_ERROR, | ||
| payload: { loading: false, error: e, events: new List([]) } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export function* saga() { | ||
| yield takeEvery(LOAD_ALL_EVENTS, loadAllEventsSaga) | ||
| } | ||
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
Oops, something went wrong.
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.
Можно бы еще редюсер протестить