rsnext/examples/with-rematch/shared/models/github.js
2020-05-26 08:24:36 -04:00

38 lines
780 B
JavaScript

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() {
try {
this.requestUsers()
const response = await fetch('https://api.github.com/users')
const users = await response.json()
this.receiveUsers(users)
return users
} catch (err) {
console.log(err)
this.receiveUsers([])
}
},
},
}
export default github