Commit e7928c3d authored by Martin Fahl's avatar Martin Fahl

delete now deletes resource on server

parent 3cf2f8cd
// actions // actions
export const ITEM_ADDED = 'ITEM_ADDED'; export const ITEM_ADDED = 'ITEM_ADDED';
export const UPDATE_FILTER = 'UPDATE_FILTER'; export const UPDATE_FILTER = 'UPDATE_FILTER';
export const DELETE_ITEM = 'DELETE_ITEM'; export const ITEM_DELETED = 'ITEM_DELETED';
export const INCREMENT = 'INREMENT_ITEM'; export const INCREMENT = 'INREMENT_ITEM';
export const DECREMENT = 'DECREMENT_ITEM'; export const DECREMENT = 'DECREMENT_ITEM';
export const ITEMS_LOADED = 'ITEMS_LOADED'; export const ITEMS_LOADED = 'ITEMS_LOADED';
...@@ -22,8 +22,8 @@ export const updateFilter = filterTerm => ({ ...@@ -22,8 +22,8 @@ export const updateFilter = filterTerm => ({
filterTerm: filterTerm filterTerm: filterTerm
}) })
export const deleteItem = id => ({ export const itemDeleted = id => ({
type: DELETE_ITEM, type: ITEM_DELETED,
id id
}) })
...@@ -77,5 +77,15 @@ export const addItem = (name, quantity) => dispatch => { ...@@ -77,5 +77,15 @@ export const addItem = (name, quantity) => dispatch => {
} }
} }
xmlhttp.send(JSON.stringify({name, quantity})) xmlhttp.send(JSON.stringify({name, quantity}))
}
export const deleteItem = (id) => dispatch => {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("DELETE", "/api/items/"+id);
xmlhttp.onreadystatechange = () => {
if (xmlhttp.readyState === 4 && xmlhttp.status === 204) {
dispatch(itemDeleted(id))
}
}
xmlhttp.send()
} }
\ No newline at end of file
import { DELETE_ITEM, INCREMENT, DECREMENT, ITEMS_LOADED, ITEM_ADDED } from '../actions'; import { ITEM_DELETED, INCREMENT, DECREMENT, ITEMS_LOADED, ITEM_ADDED } from '../actions';
const items = (state = [], action) => { const items = (state = [], action) => {
switch (action.type) { switch (action.type) {
...@@ -13,8 +13,7 @@ const items = (state = [], action) => { ...@@ -13,8 +13,7 @@ const items = (state = [], action) => {
quantity: action.quantity quantity: action.quantity
} }
] ]
case DELETE_ITEM: case ITEM_DELETED:
console.log(action.id);
return state.filter(item => (item.id !== action.id)) return state.filter(item => (item.id !== action.id))
case INCREMENT: case INCREMENT:
return state.map(item => ( return state.map(item => (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment