From a2c7f7252d76e481fc9304f510c8ba0a0457ba41 Mon Sep 17 00:00:00 2001 From: Martin Sosic Date: Wed, 5 Feb 2020 17:53:21 +0100 Subject: [PATCH] Simplified updating of entity. --- .../react-app/src/entities/_entity/actions.js | 6 +++--- .../src/entities/_entity/components/List.js | 17 +++++------------ .../react-app/src/entities/_entity/state.js | 5 ++++- examples/todoApp/ext/Todo.js | 7 +------ 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/data/Generator/templates/react-app/src/entities/_entity/actions.js b/data/Generator/templates/react-app/src/entities/_entity/actions.js index da438d1c0..3e53095e5 100644 --- a/data/Generator/templates/react-app/src/entities/_entity/actions.js +++ b/data/Generator/templates/react-app/src/entities/_entity/actions.js @@ -12,12 +12,12 @@ export const add = ({= entityLowerName =}) => ({ /** * @param {String} id - * @param {{= entity.name =}} updated{= entity.name =} + * @param {Object} data - Partial data that will be merged with existing {= entityLowerName =}. */ -export const update = (id, updated{= entity.name =}) => ({ +export const update = (id, data) => ({ type: types.UPDATE, id, - data: updated{= entity.name =}.toData() + data }) /** diff --git a/data/Generator/templates/react-app/src/entities/_entity/components/List.js b/data/Generator/templates/react-app/src/entities/_entity/components/List.js index 21a66e30c..11de02a72 100644 --- a/data/Generator/templates/react-app/src/entities/_entity/components/List.js +++ b/data/Generator/templates/react-app/src/entities/_entity/components/List.js @@ -30,13 +30,6 @@ export class {= listName =} extends React.Component { {= entityBeingEditedStateVar =}: null } - update{= entityName =} = (data, {= entityLowerName =}) => { - const updated{= entityName =} = new {= entityClassName =}( - { ...{= entityLowerName =}.toData(), ...data } - ) - this.props.update{= entityName =}({= entityLowerName =}.id, updated{= entityName =}) - } - setAsBeingEdited = {= entityLowerName =} => this.setState({ {= entityBeingEditedStateVar =}: {= entityLowerName =}.id }) @@ -56,7 +49,7 @@ export class {= listName =} extends React.Component { {=& render =} {=/ render =} {=/ listFields =} - + render() { const {= entityLowerName =}ListToShow = this.props.filter ? {=! TODO(matija): duplication, we could extract entityLowerName_List =} @@ -88,8 +81,8 @@ export class {= listName =} extends React.Component { 'aria-label': 'checkbox' }} disabled={!this.props.editable} - onChange={e => this.update{= entityName =}( - { '{= name =}': e.target.checked }, {= entityLowerName =} + onChange={e => this.props.update{= entityName =}( + {= entityLowerName =}.id, { '{= name =}': e.target.checked } )} /> @@ -102,8 +95,8 @@ export class {= listName =} extends React.Component { {this.props.editable && this.isBeingEdited({= entityLowerName =}) ? ( this.update{= entityName =}( - { '{= name =}': e.target.value }, {= entityLowerName =} + onChange={e => this.props.update{= entityName =}( + {= entityLowerName =}.id, { '{= name =}': e.target.value } )} /> ) : ( diff --git a/data/Generator/templates/react-app/src/entities/_entity/state.js b/data/Generator/templates/react-app/src/entities/_entity/state.js index 0d349f426..fee090cf8 100644 --- a/data/Generator/templates/react-app/src/entities/_entity/state.js +++ b/data/Generator/templates/react-app/src/entities/_entity/state.js @@ -25,7 +25,10 @@ const reducer = (state = initialState, action) => { return { ...state, all: state.all.map( - {= entityLowerName =} => {= entityLowerName =}.id === action.id ? action.data : {= entityLowerName =} + {= entityLowerName =} => + {= entityLowerName =}.id === action.id + ? { ...{= entityLowerName =}, ...action.data } + : {= entityLowerName =} ) } diff --git a/examples/todoApp/ext/Todo.js b/examples/todoApp/ext/Todo.js index 98bbe51be..4930ea353 100644 --- a/examples/todoApp/ext/Todo.js +++ b/examples/todoApp/ext/Todo.js @@ -31,12 +31,7 @@ export default class Todo extends React.Component { toggleIsDoneForAllTasks = () => { const areAllDone = this.props.taskList.every(t => t.isDone) - {/* TODO: This feels clumsy / complicated. Is there a better way than using id (maybe not)? - Should we consider passing just data to update, not the whole object, so we don't have to - create new object here? Maybe we can change this update, or have a second update method. */} - this.props.taskList.map( - (t) => this.props.updateTask(t.id, new Task ({ ...t.toData(), isDone: !areAllDone })) - ) + this.props.taskList.map(t => this.props.updateTask(t.id, { isDone: !areAllDone })) } deleteCompletedTasks = () => {