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
Bhargav Ponnapalli 5187d142ea Added example for usage with rematch (#4095)
* Usage with rematch

* Run lint fix

* Add counter-display example and readme
2018-04-18 14:03:24 +02:00

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