added Redux
This commit is contained in:
parent
5da7f8522b
commit
95c473bda1
@ -1,12 +0,0 @@
|
||||
import axios from 'axios'
|
||||
|
||||
const fetchEpisodes = async () => {
|
||||
try {
|
||||
const { data } = await axios.get('/api/v2/episodes')
|
||||
return data
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
export default fetchEpisodes
|
17
src/store/index.js
Normal file
17
src/store/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
import {createStore, combineReducers, applyMiddleware} from 'redux'
|
||||
import {createLogger} from 'redux-logger'
|
||||
import thunkMiddleware from 'redux-thunk'
|
||||
import {composeWithDevTools} from 'redux-devtools-extension'
|
||||
import episodes from './reducers/episodes'
|
||||
import captions from './reducers/captions'
|
||||
|
||||
|
||||
const reducer = combineReducers({episodes, captions})
|
||||
const middleware = composeWithDevTools(
|
||||
applyMiddleware(thunkMiddleware, createLogger({collapsed: true}))
|
||||
)
|
||||
const store = createStore(reducer, middleware)
|
||||
|
||||
export default store
|
||||
export * from './reducers/episodes'
|
||||
export * from './reducers/captions'
|
47
src/store/reducers/captions.js
Normal file
47
src/store/reducers/captions.js
Normal file
@ -0,0 +1,47 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// ACTION TYPES
|
||||
const GOT_ALL_CAPTIONS = 'GOT_ALL_CAPTIONS'
|
||||
const initialCaptions = {all: [], selected: {}}
|
||||
|
||||
// ACTION CREATORS
|
||||
export const gotAllCaptions = captions => ({
|
||||
type: GOT_ALL_CAPTIONS,
|
||||
all: captions
|
||||
})
|
||||
|
||||
|
||||
// THUNK CREATORS
|
||||
|
||||
export const fetchAllCaptions = () => async dispatch => {
|
||||
const captions = []
|
||||
try {
|
||||
const res = await axios.get('https://irc.anarchyplanet.org/ircbang/api/v2/episodes')
|
||||
const episodes = res.data
|
||||
episodes.map(async episode => {
|
||||
try{
|
||||
const {data} = await axios.get(`https://irc.anarchyplanet.org/ircbang/api/v2/episodes/${episode.slug}`)
|
||||
captions.push(data)
|
||||
|
||||
} catch(e) {
|
||||
console.log(`no captions for ${episode.slug}`)
|
||||
}
|
||||
})
|
||||
|
||||
dispatch(gotAllCaptions(captions))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// REDUCER
|
||||
const episodeReducer = (captions = initialCaptions, action) => {
|
||||
switch (action.type) {
|
||||
case GOT_ALL_CAPTIONS:
|
||||
return ({all: action.all, selected: {}})
|
||||
default:
|
||||
return captions
|
||||
}
|
||||
}
|
||||
|
||||
export default episodeReducer
|
35
src/store/reducers/episodes.js
Normal file
35
src/store/reducers/episodes.js
Normal file
@ -0,0 +1,35 @@
|
||||
import axios from 'axios'
|
||||
|
||||
// ACTION TYPES
|
||||
const GOT_ALL_EPISODES = 'GOT_ALL_EPISODES'
|
||||
const initialEpisodes = []
|
||||
|
||||
// ACTION CREATORS
|
||||
export const gotAllEpisodes = episodes => ({
|
||||
type: GOT_ALL_EPISODES,
|
||||
episodes
|
||||
})
|
||||
|
||||
|
||||
// THUNK CREATORS
|
||||
|
||||
export const fetchAllEpisodes = () => async dispatch => {
|
||||
try {
|
||||
const {data} = await axios.get('https://irc.anarchyplanet.org/ircbang/api/v2/episodes')
|
||||
dispatch(gotAllEpisodes(data))
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
// REDUCER
|
||||
const episodeReducer = (episodes = initialEpisodes, action) => {
|
||||
switch (action.type) {
|
||||
case GOT_ALL_EPISODES:
|
||||
return action.episodes
|
||||
default:
|
||||
return episodes
|
||||
}
|
||||
}
|
||||
|
||||
export default episodeReducer
|
Loading…
Reference in New Issue
Block a user