Skip to content

Instantly share code, notes, and snippets.

@adnaan
Last active November 6, 2016 10:04
Show Gist options
  • Save adnaan/0d6fd203d976e4f3ef72110a7dd7ed29 to your computer and use it in GitHub Desktop.
Save adnaan/0d6fd203d976e4f3ef72110a7dd7ed29 to your computer and use it in GitHub Desktop.
src/model.js
import { getProducts, updateVote } from './services';
export default {
namespace: 'products',
state: {
list: [],
loading: false,
},
subscriptions: {
init({ dispatch }: { dispatch: Function }) {
dispatch({ type: 'query' });
},
},
effects: { * query(_, { call, put }: { call: Function, put: Function }) {
console.log("query effect")
const { success, data } = yield call(getProducts);
if (success) {
yield put({
type: 'querySuccess',
products: data,
});
}
},
* vote({ id }: { id: number }, { call, put }: { call: Function, put: Function }) {
console.log("vote reducer")
const { success } = yield call(updateVote, id);
if (success) {
yield put({
type: 'voteSuccess',
id,
});
}
},
},
reducers: {
query(state) {
console.log("query reducer")
return {...state, loading: true, };
},
querySuccess(state, { products }: { products: Array < any > }) {
return {...state, loading: false, list: products };
},
vote(state) {
console.log("vote reducer")
return {...state, loading: true };
},
voteSuccess(state, { id }: { id: number }) {
const newList = state.list.map(product => {
if (product.ID === id) {
return {...product, vote: product.vote + 1 };
} else {
return product;
}
});
return {...state, list: newList, loading: false };
},
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment