# HG changeset patch # User Alexandre Segura # Date 1497436089 -7200 # Node ID 4d93f4ed95bcef23687e1c31774820b9b4ef5365 # Parent 4cfeabef7d5e9744f07ed2321a1d87e536319908 Update session in PouchDB. diff -r 4cfeabef7d5e -r 4d93f4ed95bc client/src/actions/sessionsActions.js --- a/client/src/actions/sessionsActions.js Mon Jun 12 18:12:38 2017 +0200 +++ b/client/src/actions/sessionsActions.js Wed Jun 14 12:28:09 2017 +0200 @@ -16,7 +16,7 @@ export const updateSession = (session, values) => { return { - type: types.UPDATE_SESSION, + type: types.UPDATE_SESSION_ASYNC, session: session, values: values, }; diff -r 4cfeabef7d5e -r 4d93f4ed95bc client/src/components/Session.js --- a/client/src/components/Session.js Mon Jun 12 18:12:38 2017 +0200 +++ b/client/src/components/Session.js Wed Jun 14 12:28:09 2017 +0200 @@ -1,30 +1,67 @@ import React, { Component } from 'react'; import { connect } from 'react-redux'; import { bindActionCreators } from 'redux'; -import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button } from 'react-bootstrap'; +import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl } from 'react-bootstrap'; import '../App.css'; import Navbar from './Navbar'; import NoteInput from './NoteInput'; import NotesList from './NotesList'; import * as sessionsActions from '../actions/sessionsActions'; import * as notesActions from '../actions/notesActions'; +import _ from 'lodash'; class Session extends Component { - saveSession = () => { - const title = this.title.value; - const description = this.description.value; + onChange = (e) => { + const { name, value } = e.target; + const changes = { [name]: value } + this.onChangeThrottle(changes); + } - this.props.sessionsActions.updateSession(this.props.currentSession, { - title: title, - description: description - }); - } + onChangeThrottle = _.debounce((changes) => { + this.props.sessionsActions.updateSession(this.props.currentSession, changes); + }, 750) componentDidMount = () => { this.props.notesActions.loadNotesBySession({ _id: this.props.match.params.id }); } + renderForm() { + + if (!this.props.currentSession) { + return ( +
+ ) + } + + return ( +
+ + Title + { this.title = ref; } } + /> + + + Description + { this.description = ref; } } + /> + +
+ ); + } + render() { return (
@@ -33,27 +70,7 @@ -
- - Title - { this.title = ref; }} - /> - - - Description - { this.description = ref; }} - /> - - -
+ { this.renderForm() }
@@ -77,9 +94,9 @@ const currentSession = sessions.find(session => session._id === sessionId); return { - currentSession: currentSession, - sessions: sessions, - notes: notes + currentSession, + sessions, + notes, }; } diff -r 4cfeabef7d5e -r 4d93f4ed95bc client/src/constants/actionTypes.js --- a/client/src/constants/actionTypes.js Mon Jun 12 18:12:38 2017 +0200 +++ b/client/src/constants/actionTypes.js Wed Jun 14 12:28:09 2017 +0200 @@ -6,5 +6,6 @@ export const CREATE_SESSION = 'CREATE_SESSION'; export const CREATE_SESSION_ASYNC = 'CREATE_SESSION_ASYNC'; export const UPDATE_SESSION = 'UPDATE_SESSION'; +export const UPDATE_SESSION_ASYNC = 'UPDATE_SESSION_ASYNC'; export const LOAD_SESSIONS = 'LOAD_SESSIONS'; export const LOAD_SESSIONS_ASYNC = 'LOAD_SESSIONS_ASYNC'; diff -r 4cfeabef7d5e -r 4d93f4ed95bc client/src/sagas/index.js --- a/client/src/sagas/index.js Mon Jun 12 18:12:38 2017 +0200 +++ b/client/src/sagas/index.js Wed Jun 14 12:28:09 2017 +0200 @@ -40,6 +40,28 @@ // --- +export function* updateSession(action) { + + const { _id } = action.session; + let session; + + const response = yield sessionsDB.get(_id).then(function(doc) { + session = Object.assign({}, doc, action.values); + return sessionsDB.put(session); + }); + + yield put({ + type: types.UPDATE_SESSION, + session: Object.assign({}, session, { rev: response.rev }) + }) +} + +export function* watchUpdateSession() { + yield takeLatest(types.UPDATE_SESSION_ASYNC, updateSession) +} + +// --- + export function* addNote(action) { const response = yield notesDB.put(action.note); // TODO Error control @@ -73,5 +95,6 @@ watchLoadNotesBySession(), watchAddNote(), watchCreateSession(), + watchUpdateSession(), ]) }