From 7c195db007b7a922cb5092781625a9a337a17d4c Mon Sep 17 00:00:00 2001 From: Emma Paris Date: Mon, 27 Nov 2017 23:53:46 +0100 Subject: [PATCH] ft: enable admin users modify centers --- client/src/Sagax/saga.js | 36 ++++++++++++++- client/src/actions/actionCreators.js | 8 ++++ client/src/components/Edit.js | 69 ++++++++++++++++++---------- client/src/components/admin.js | 25 ++++++++-- 4 files changed, 111 insertions(+), 27 deletions(-) diff --git a/client/src/Sagax/saga.js b/client/src/Sagax/saga.js index 457e705..9397ce8 100644 --- a/client/src/Sagax/saga.js +++ b/client/src/Sagax/saga.js @@ -105,9 +105,12 @@ export function* addCenter(action) { }); yield put({ type: 'SET_CENTER', response: response.data }); yield put({ type: 'ERROR', error: '' }); + yield delay(3000); + yield put({ type: 'UNLOAD' }); yield put({ type: 'SUCCESS', message: 'Successfully added to centers' }); }catch(e){ + yield put({ type: 'UNLOAD' }); const error = e.response.data.message; console.log(error); yield put({ type: 'ERROR', error }); @@ -120,6 +123,36 @@ export function* watchAddCenter() { } +export function* updateCenter(action) { + try{ + const token = localStorage.getItem('token'); + const response = yield call(axios.put, `${centerUrl}/${action.index}?token=${token}`, { + name: action.payload.name, + description: action.payload.description, + capacity: action.payload.capacity, + location: action.payload.location, + image: action.payload.image, + price: action.payload.price + }); + yield put({ type: 'ERROR', error: '' }); + yield delay(5000); + yield put({ type: 'UNLOAD' }); + yield put({ type: 'SUCCESS', message: 'Centers successfully updated' }); + + }catch(e){ + yield put({ type: 'UNLOAD' }); + const error = e.response.data.message; + console.log(error); + yield delay(1000); + yield put({ type: 'ERROR', error }); + } +} +// Our watcher Saga: spawn a new incrementAsync task on each INCREMENT_ASYNC +export function* watchUpdateCenter() { + yield takeEvery('UPDATE_CENTER', updateCenter); +} + + export default function* rootSaga() { yield [ @@ -127,7 +160,8 @@ export default function* rootSaga() { watchSignUser(), watchGetCenters(), watchGetSingle(), - watchAddCenter() + watchAddCenter(), + watchUpdateCenter() ]; } diff --git a/client/src/actions/actionCreators.js b/client/src/actions/actionCreators.js index 2189243..fce9732 100644 --- a/client/src/actions/actionCreators.js +++ b/client/src/actions/actionCreators.js @@ -19,6 +19,14 @@ export function getCenters(index) { }; } +export function updateCenter(payload, index) { + return { + type: 'UPDATE_CENTER', + payload, + index + }; +} + export function getSingle(index) { return { diff --git a/client/src/components/Edit.js b/client/src/components/Edit.js index 0adae28..739c053 100644 --- a/client/src/components/Edit.js +++ b/client/src/components/Edit.js @@ -1,6 +1,7 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Link } from 'react-router'; +import store from '../store'; class Admin extends Component { constructor(){ @@ -20,16 +21,28 @@ class Admin extends Component { componentWillMount(){ this.props.getSingle(this.props.params.id); } + + componentWillReceiveProps(newProps) { + if(newProps.single) { + this.setState(newProps.single); + } + } + onChange(e){ this.setState({ [e.target.name]: e.target.value }); } + handleSubmit(e) { e.preventDefault(); - // this.props.addCenter(this.state); + store.dispatch({type: 'LOAD'}); + console.log(this.state); + const index = this.props.params.id; + this.props.updateCenter(this.state, index); // document.getElementById("add-form").reset(); } render() { - const { error, single, loader, success } = this.props; + const { error, single,loader, success } = this.props; + console.log(single); return (
@@ -47,7 +60,13 @@ class Admin extends Component {
  • ID:3456565646JD
  • HOME
  • -
  • Add Center
  • + +
  • + + Add Center + +
  • +
  • Messages
  • menu @@ -64,9 +83,18 @@ class Admin extends Component {
    : '' }
    - { loader ? -
    -
    + { success ? +
    + × +