Skip to content

Instantly share code, notes, and snippets.

@rowlandekemezie
Created March 10, 2018 19:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rowlandekemezie/f559ec88da8ff348913820d2da3e8ed7 to your computer and use it in GitHub Desktop.
Save rowlandekemezie/f559ec88da8ff348913820d2da3e8ed7 to your computer and use it in GitHub Desktop.
Update and Delete request with redux-saga
// Again, you just move this functions to the ones you already have to creating and reading
import { DELETE_SUCCESS, UPDATE_SUCCESS } from 'path/to/constants';
export const updateSuccess = (id, newUpdate) => {
return {
type: UPDATE_SUCCESS,
id,
newUpdate
}
}
export const deleteSuccess = (id) => {
return {
type: DELETE_SUCCESS,
id
}
}
// Other constants. You decide how you want to go about it but I don keep mine a separate file for easy manageability
export const DELETE_SUCCESS = 'DELETE_SUCCESS';
export const UPDATE_SUCCESS = 'UPDATE_SUCCESS';
/*
1) Depending on your experience, this is all you might need to do to hook up your workflow
2) Remember, not to copy and paste. My idea is to show you how things fit it to you can tailor it to your application
GRACIAS 💪
*/
// import your constants here
// Reducer
const initialState = {
todos: []
}
export default (state, action) => {
switch(action.type) {
case CREATE_SUCCESS:
return { ...state, /* add the new item to the state here */};
case UPDATE_SUCCESS:
/* there are two approach for this:
1) make a get request after you've updated it in the server (That's two api calls)
2) Simply update the the item in your redux stor after successfull update on the server.
*/
return {
...state,
state.todos.slice(0, action.id), // This gets the first part of the todos array
...action.newUpdate, // Update the todo item here
state.todos.slice(action.id + 1) // This takes care of the second part of the todos list
}
case DELETE_SUCCESS:
const newState = state.filter(todo => todo.id !== action.id); // Use filter method to remoreove the item that has been deleted from the st
return newState;
default:
return state;
}
function updateTodoAPI (id, data) {
return fetch(`/path/to/update/endpoint/${id}`, {
method: 'PUT',
body: JSON.stringify(data)
})
.then(res => res.json())
.catch(err => throw err);
}
function deleteTodoAPI (id) {
return fetch(`/path/to/delete/endpoint/${id}`, {
method: 'DELETE'
})
.then(res => res.json())
.catch(err => throw err);
}
/// Other stuff goes here
import { takeEvery, call, put } from 'redux-saga/effects';
import * as API from 'path/to/remote.js' /// So, you can now use this functions that makes ajax calls in your sagas.
function* updateTodo ({ id, data }) {
try {
// Ensure that your API returns the data of the updated todo
const newData = yield call(Your-API-Method, id, data); // Refer sample to api calls in remote.js file
yield put(updateSuccess(id, newData) // pass in the id you updated and the newData returned from the API
/// Other things can go here depending on what you want
} catch (e) {
// Handle your errors here
}
}
function* deleteTodo ({ id }) {
try {
// Ensure that your API returns the data of the updated todo
const newData = yield call(Your-API-Method, id); // Refer sample to api calls in remote.js file
yield put(deleteSuccess(id) // pass in the id you updated and the newData returned from the API
/// Other things can go here depending on what you want
} catch (e) {
// Handle your errors here
}
}
// Create a watcher to listen to UPDATE and DELETE request from the client(REACT)
funtion* updateWatcher() {
takeEvery('UPDATE_REQUEST', updateTodo) // Remember to create an action that returns this action type(UPDATE_REQUEST);
}
funtion* updateWatcher() {
takeEvery('DELETE_REQUEST', deleteTodo) // Remember to create an action that returns this action type(DELETE_REQUEST);
}
// Add this two functions to your root saga so everything is wired up with your reducer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment