Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
feat(reducers): simplify redurecers, remove metarouter
Browse files Browse the repository at this point in the history
feat(reducers): simplify redurecers, remove metarouter
  • Loading branch information
Metnew committed Dec 5, 2017
1 parent ff21d51 commit 7bc8676
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 92 deletions.
16 changes: 2 additions & 14 deletions src/common/reducers/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
LOGOUT_AUTH_SUCCESS
} from 'actions/auth'
import {APPLICATION_INIT} from 'actions/common'
//

import type {
LOGIN_AUTH_FAIL_TYPE,
LOGIN_AUTH_PENDING_TYPE,
Expand All @@ -17,8 +17,6 @@ import type {
import type {APPLICATION_INIT_TYPE} from 'actions/common'

export type State = {
isLoading: boolean,
isLoaded: boolean,
isLoggedIn: boolean,
errors: Object
}
Expand All @@ -31,8 +29,6 @@ type Action =
| LOGOUT_AUTH_SUCCESS_TYPE

export const initialState: State = {
isLoading: false,
isLoaded: false,
isLoggedIn: hasLocalToken(),
errors: {}
}
Expand All @@ -44,8 +40,6 @@ export function auth (state: State = initialState, action: Action): State {
case LOGOUT_AUTH_SUCCESS: {
return {
...state,
isLoading: false,
isLoaded: true,
isLoggedIn: false,
errors: {}
}
Expand All @@ -54,25 +48,19 @@ export function auth (state: State = initialState, action: Action): State {
const {errors} = action.payload
return {
...state,
isLoading: false,
isLoaded: true,
isLoggedIn: false,
errors
}
}
case LOGIN_AUTH_SUCCESS: {
return {
...state,
isLoading: false,
isLoaded: true,
isLoggedIn: true
}
}
case LOGIN_AUTH_PENDING: {
return {
...state,
isLoading: true,
isLoaded: false
...state
}
}
default:
Expand Down
12 changes: 4 additions & 8 deletions src/common/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
// @flow
import {combineReducers} from 'redux'
import {routerReducer} from 'react-router-redux'
import {getMetaRoutes} from 'routing'
import {reducer as reduxFormReducer} from 'redux-form'

import type {State as AuthState} from 'reducers/auth'
import type {State as LayoutState} from 'reducers/layout'
import type {State as EntitiesLinksState} from 'reducers/links'

import {layout} from './layout'
import {metaRouting} from './metarouter'
import {links} from './links'
import {auth} from './auth'

// Root reducer
export default combineReducers({
layout,
me: combineReducers({auth}),
auth,
entities: combineReducers({
links
}),
routing: routerReducer,
// NOTE: metaRouting is an addon for react-router-redux
// metaRouting allows you storing meta info (e.g. `meta` field) about the current route
// metaRouting is avaliable as package: `react-router-redux-meta`
metaRouting: metaRouting(getMetaRoutes())
form: reduxFormReducer
})

export type GlobalState = {layout: LayoutState} & {me: {auth: AuthState}} & {
export type GlobalState = {layout: LayoutState} & {auth: AuthState} & {
entities: {links: EntitiesLinksState}
}
33 changes: 8 additions & 25 deletions src/common/reducers/layout/index.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,44 @@
// @flow
import {
UI_OPEN_SIDEBAR,
UI_CLOSE_SIDEBAR,
UI_TOGGLE_SIDEBAR,
UI_WINDOW_RESIZE
} from 'actions/layout'
import {LOCATION_CHANGE} from 'actions/common'
//
import type {LOCATION_CHANGE_TYPE} from 'actions/common'
import type {
UI_OPEN_SIDEBAR_TYPE,
UI_CLOSE_SIDEBAR_TYPE,
UI_TOGGLE_SIDEBAR_TYPE,
UI_WINDOW_RESIZE_TYPE
} from 'actions/layout'

export type State = {
sidebarOpened: boolean,
isMobile: boolean,
isMobileXS: boolean,
isMobileSM: boolean
innerWidth?: number
}

type Action =
| UI_OPEN_SIDEBAR_TYPE
| UI_CLOSE_SIDEBAR_TYPE
| UI_TOGGLE_SIDEBAR_TYPE
| UI_WINDOW_RESIZE_TYPE
| LOCATION_CHANGE_TYPE

export const initialState: State = {
sidebarOpened: false,
isMobile: false,
isMobileXS: false,
isMobileSM: false
sidebarOpened: false
}

export function layout (state: State = initialState, action: Action): State {
const computeMobileStatuses = (innerWidth: number) => {
const isMobile: boolean = innerWidth < 1025 // 1024px - is the main breakpoint in ui
const isMobileXS: boolean = innerWidth < 481
const isMobileSM: boolean = innerWidth > 480 && innerWidth < 767
return {isMobileSM, isMobileXS, isMobile}
}
switch (action.type) {
case UI_WINDOW_RESIZE: {
const {innerWidth} = action.payload
const mobileStates = computeMobileStatuses(innerWidth)
return {
...state,
...mobileStates
innerWidth
}
}
case UI_OPEN_SIDEBAR:
case UI_TOGGLE_SIDEBAR:
return {
...state,
sidebarOpened: true
sidebarOpened: !state.sidebarOpened
}
case LOCATION_CHANGE:
case UI_CLOSE_SIDEBAR:
return {
...state,
sidebarOpened: false
Expand Down
25 changes: 9 additions & 16 deletions src/common/reducers/links/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import type {GET_LINKS_SUCCESS_TYPE, GET_LINKS_FAIL_TYPE, GET_LINKS_PENDING_TYPE
export type State = {
entities: Array<LinkItem>,
errors: Object,
isLoading: boolean,
isLoaded: boolean
fetchStatus: 'none' | 'loaded' | 'loading'
}

type Action =
Expand All @@ -22,41 +21,35 @@ type Action =
export const initialState: State = {
entities: [],
errors: {},
isLoading: false,
isLoaded: false,
count: 0
fetchStatus: 'none'
}

export function links (state: State = initialState, action: Action): State {
switch (action.type) {
case GET_LINKS_PENDING: {
console.log(action)
return {
...state,
errors: {},
isLoaded: false,
isLoading: true
fetchStatus: 'loading'
}
}
case GET_LINKS_SUCCESS: {
console.log(action)
const entities = action.payload
const count = entities.length
return {
...state,
isLoaded: true,
isLoading: false,
entities,
count
fetchStatus: 'loaded'
}
}
case GET_LINKS_FAIL: {
const {errors} = action.payload
console.log(action)
const errors = action.payload
return {
...state,
errors,
isLoaded: true,
isLoading: false,
entities: [],
count: 0
fetchStatus: 'loaded'
}
}
default:
Expand Down
29 changes: 0 additions & 29 deletions src/common/reducers/metarouter/index.js

This file was deleted.

0 comments on commit 7bc8676

Please sign in to comment.