mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
40 lines
814 B
JavaScript
40 lines
814 B
JavaScript
|
import fetch from 'isomorphic-unfetch'
|
||
|
|
||
|
const github = {
|
||
|
state: {
|
||
|
users: [],
|
||
|
isLoading: false
|
||
|
}, // initial state
|
||
|
reducers: {
|
||
|
requestUsers (state) {
|
||
|
return {
|
||
|
users: [],
|
||
|
isLoading: true
|
||
|
}
|
||
|
},
|
||
|
receiveUsers (state, payload) {
|
||
|
return {
|
||
|
isLoading: false,
|
||
|
users: payload
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
effects: {
|
||
|
// handle state changes with impure functions.
|
||
|
// use async/await for async actions
|
||
|
async fetchUsers (payload, rootState) {
|
||
|
try {
|
||
|
this.requestUsers()
|
||
|
const response = await fetch('https://api.github.com/users')
|
||
|
const users = await response.json()
|
||
|
this.receiveUsers(users)
|
||
|
} catch (err) {
|
||
|
console.log(err)
|
||
|
this.receiveUsers([])
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default github
|