Simplified updating of entity.

This commit is contained in:
Martin Sosic 2020-02-05 17:53:21 +01:00 committed by Martin Šošić
parent 35fd055fdb
commit a2c7f7252d
4 changed files with 13 additions and 22 deletions

View File

@ -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
})
/**

View File

@ -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
})
@ -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 }
)}
/>
</TableCell>
@ -102,8 +95,8 @@ export class {= listName =} extends React.Component {
{this.props.editable && this.isBeingEdited({= entityLowerName =}) ? (
<TextField
value={{= entityLowerName =}.{= name =}}
onChange={e => this.update{= entityName =}(
{ '{= name =}': e.target.value }, {= entityLowerName =}
onChange={e => this.props.update{= entityName =}(
{= entityLowerName =}.id, { '{= name =}': e.target.value }
)}
/>
) : (

View File

@ -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 =}
)
}

View File

@ -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 = () => {