Skip to content

Instantly share code, notes, and snippets.

@QuadradS
Created October 24, 2019 13:06
Show Gist options
  • Save QuadradS/24568ecfdcd2efdb67d375a4035f35eb to your computer and use it in GitHub Desktop.
Save QuadradS/24568ecfdcd2efdb67d375a4035f35eb to your computer and use it in GitHub Desktop.
import { GET_BLOGS_ERROR, GET_BLOGS_REQUEST, GET_BLOGS_SUCCESS } from './actions'
import { DATA_STATUS } from '../../consts/dataStatus'
const initialState = {
blogsStatus: DATA_STATUS.NOT_TOUCHED,
blogs: []
}
const blogsPageReducer = (state = initialState, action) => {
switch (action.type) {
case GET_BLOGS_REQUEST:
return {
blogsStatus: DATA_STATUS.REQUESTED
}
case GET_BLOGS_SUCCESS:
return {
blogsStatus: DATA_STATUS.SUCCESS,
blogs: action.blogs
}
case GET_BLOGS_ERROR:
return {
blogsStatus: DATA_STATUS.ERROR,
blogs: []
}
default:
return state
}
}
export default blogsPageReducer
import { DATA_STATUSES } from '../../../utils'
import * as authConstants from './constants'
import { actionType, AuthStateType, MainLeftMenuStateType } from './types'
const initialMainLeftMenuState: MainLeftMenuStateType = {
currentState: DATA_STATUSES.UN_TOUCHED,
isMenuOpen: false
}
export const mainLeftMenuState = (state: MainLeftMenuStateType = initialMainLeftMenuState, action: actionType) => {
switch (action.type) {
case authConstants.OPEN_MENU:
return ({ ...state, currentState: action.data, isMenuOpen: true })
case authConstants.CLOSE_MENU:
return ({ ...state, currentState: DATA_STATUSES.UN_TOUCHED, isMenuOpen: false })
default:
return state
}
}
const initialAuthState: AuthStateType = {
isAuthorized: false,
authStatus: DATA_STATUSES.UN_TOUCHED,
resetPasswordStatus: DATA_STATUSES.UN_TOUCHED,
registrationStatus: DATA_STATUSES.UN_TOUCHED
}
export const authState = (state: AuthStateType = initialAuthState, action: actionType) => {
switch (action.type) {
case authConstants.AUTH_REQUEST:
return ({ ...state, authStatus: DATA_STATUSES.REQUESTING })
case authConstants.AUTH_SUCCESS:
return ({ ...state, authStatus: DATA_STATUSES.SUCCESS })
case authConstants.AUTH_ERROR:
return ({ ...state, authStatus: DATA_STATUSES.ERROR })
case authConstants.REGISTRATION_REQUEST:
return ({ ...state, registrationStatus: DATA_STATUSES.REQUESTING })
case authConstants.REGISTRATION_SUCCESS:
return ({ ...state, registrationStatus: DATA_STATUSES.SUCCESS })
case authConstants.REGISTRATION_ERROR:
return ({ ...state, registrationStatus: DATA_STATUSES.ERROR })
case authConstants.RESET_PASSWORD_REQUEST:
return ({ ...state, resetPasswordStatus: DATA_STATUSES.REQUESTING })
case authConstants.RESET_PASSWORD_SUCCESS:
return ({ ...state, resetPasswordStatus: DATA_STATUSES.SUCCESS })
case authConstants.RESET_PASSWORD_ERROR:
return ({ ...state, resetPasswordStatus: DATA_STATUSES.ERROR })
default:
return state
}
}
import { call, put, takeEvery } from 'redux-saga/effects'
import API from '../../API'
import { GET_BLOGS_REQUEST, getBlogsError, getBlogsSuccess } from './actions'
export function* getBlogsRequestWorker() {
try {
const result = yield call(API.getBlogs);
yield put(getBlogsSuccess(result));
} catch (error) {
yield put(getBlogsError());
}
}
export function* getBlogsRequestWatcher() {
yield takeEvery(GET_BLOGS_REQUEST, getBlogsRequestWorker);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment