Skip to content

Instantly share code, notes, and snippets.

@nanotime
Created February 22, 2018 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanotime/6a604d5d862411e6885ed3a0daa86647 to your computer and use it in GitHub Desktop.
Save nanotime/6a604d5d862411e6885ed3a0daa86647 to your computer and use it in GitHub Desktop.
Reducer approach
import { initialState } from './constants'
import { LOCATION_CHANGE } from 'react-router-redux'
import {
errors,
loaders,
media,
distributions,
scores,
words,
similarTitles,
titleGenerate,
general
} from './actionCreators'
const analysis = (state = initialState, action) => {
/******
ERRORS
********/
if (action.type === errors.DIST_TIME_DIST) {
const errorsState = state.errors
errorsState.distribution.time_dist = action.payload
return { ...state, errors: errorsState }
}
if (action.type === errors.DIST_SCORE_DIST) {
const errorsState = state.errors
errorsState.distribution.score_dist = action.payload
return { ...state, errors: errorsState }
}
if (action.type === errors.MEDIA_SFW) {
return { ...state }
}
if (action.type === errors.MEDIA_SUGESTED) {
return { ...state }
}
if (action.type === errors.MEDIA_PREVIEW) {
return { ...state }
}
if (action.type === errors.WORDCOLUD) {
const errorsState = state.errors
errorsState.words.wordCloud = action.payload
return { ...state, errors: errorsState }
}
if (action.type === errors.GENERATE_TITLE) {
const errorsState = state.errors
errorsState.generatedTitle = action.payload
return { ...state, errors: errorsState }
}
if (action.type === errors.TAGCLOUD) {
return { ...state }
}
if (action.type === errors.SIMILAR_TITLES) {
const errorsState = state.errors
errorsState.similarTitles = action.payload
return { ...state, errors: errorsState }
}
if (action.type === errors.GENERAL) {
const errorsState = state.errors
errorsState.general = action.payload
return { ...state, errors: errorsState }
}
/******
LOADERS
********/
if (action.type === loaders.DIST_TIME_DIST) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.distribution.time_dist.loading = action.payload.loading
loadersState.distribution.time_dist.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.distribution.time_dist.loading = action.payload.loading
loadersState.distribution.time_dist.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.DIST_SCORE_DIST) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.distribution.score_dist.loading = action.payload.loading
loadersState.distribution.score_dist.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.distribution.score_dist.loading = action.payload.loading
loadersState.distribution.score_dist.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.MEDIA_SFW) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.media.sfw.loading = action.payload.loading
loadersState.media.sfw.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.media.sfw.loading = action.payload.loading
loadersState.media.sfw.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.MEDIA_SUGESTED) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.media.sugestedImgs.loading = action.payload.loading
loadersState.media.sugestedImgs.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.media.sugestedImgs.loading = action.payload.loading
loadersState.media.sugestedImgs.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.MEDIA_PREVIEW) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.media.preview.loading = action.payload.loading
loadersState.media.preview.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.media.preview.loading = action.payload.loading
loadersState.media.preview.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.WORDCLOUD) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.words.wordCloud.loading = action.payload.loading
loadersState.words.wordCloud.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.words.wordCloud.loading = action.payload.loading
loadersState.words.wordCloud.done = true
return { ...state, loaders: loadersState }
}
}
if (action.type === loaders.TAGCLOUD) {
return { ...state }
}
if (action.type === loaders.SIMILAR_TITLES) {
const loadersState = state.loaders
if (action.payload.loading) {
loadersState.similarTitles.loading = action.payload.loading
loadersState.similarTitles.done = false
return { ...state, loaders: loadersState }
} else {
loadersState.similarTitles.loading = action.payload.loading
loadersState.similarTitles.done = true
return { ...state, loaders: loadersState }
}
}
/******
MEDIA
********/
if (action.type === media.SET_IMAGE) {
const mediaState = state.media
mediaState.image = action.payload
return { ...state, media: mediaState }
}
if (action.type === media.UPLOAD_STATE) {
const mediaState = state.media
mediaState.uploadState = action.payload
return { ...state, media: mediaState }
}
if (action.type === media.SFW) {
const mediaState = state.media
mediaState.sfw = action.payload
return { ...state, media: mediaState }
}
if (action.type === media.SIMILARS) {
const mediaState = state.media
mediaState.similars = action.payload
return { ...state, media: mediaState }
}
if (action.type === media.TIME_SCORE) {
const mediaState = state.media
mediaState.time_score = action.payload
return { ...state, media: mediaState }
}
/******
DISTRIBUTIONS
********/
if (action.type === distributions.SELECTED_DIST) {
const distState = state.distributions
distState.selected_dist = action.payload
return { ...state, distributions: distState }
}
if (action.type === distributions.SCORE_DIST) {
const distState = state.distributions
distState.score_dist = action.payload
return { ...state, distributions: distState }
}
if (action.type === distributions.TIME_DIST) {
const distState = state.distributions
distState.time_dist = action.payload
return { ...state, distributions: distState }
}
/******
SCORES
********/
if (action.type === scores.TITLE) {
const scoresState = state.scores
scoresState.title.average = action.payload.titleAverage
scoresState.title.score = action.payload.titleScore
return { ...state, scores: scoresState }
}
if (action.type === scores.MEDIA) {
const scoresState = state.scores
scoresState.media.average = action.payload.average
scoresState.media.score = action.payload.score
return { ...state, scores: scoresState }
}
/******
WORDS
********/
if (action.type === words.SELECTED_CLOUD) {
const wordsState = state.words
const selected_cloud = action.payload.replace(' ', '')
wordsState.selected_cloud = selected_cloud.toLowerCase()
return { ...state, words: wordsState }
}
if (action.type === words.WORDCLOUD) {
const wordsState = state.words
wordsState.wordcloud = action.payload
return { ...state, words: wordsState }
}
if (action.type === words.TAGCLOUD) {
const wordsState = state.words
wordsState.tagCloud = action.payload
return { ...state, words: wordsState }
}
/******
SIMILAR TITLES
********/
if (action.type === similarTitles.SIMILAR_TITLES) {
return { ...state, similarTitles: action.payload }
}
/******
TITLE GENERATOR
********/
if (action.type === titleGenerate.GENERATE_TITLE) {
return { ...state, generatedTitle: action.payload }
}
/******
GENERAL
********/
if (action.type === general.CLEAN_STATE || action.type === LOCATION_CHANGE) {
return { ...initialState }
}
return state
}
export default analysis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment