1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/examples/with-rematch/shared/models/github.js

40 lines
814 B
JavaScript
Raw Normal View History

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