Improve handling of changeset with defaults
This commit is contained in:
@@ -3,10 +3,9 @@ import PropTypes from "prop-types"
|
|||||||
import ApiClient from "api/Client"
|
import ApiClient from "api/Client"
|
||||||
import Modal, { Content } from "components/Modal"
|
import Modal, { Content } from "components/Modal"
|
||||||
import Form from "components/Form"
|
import Form from "components/Form"
|
||||||
import { observable } from "mobx"
|
import { observable, computed } from "mobx"
|
||||||
import { observer } from "mobx-react"
|
import { observer } from "mobx-react"
|
||||||
import logoUrl from "images/logo.png"
|
import logoUrl from "images/logo.png"
|
||||||
import get from "lodash/get"
|
|
||||||
import { findLastProject, findLastTask, groupedProjectOptions } from "utils"
|
import { findLastProject, findLastTask, groupedProjectOptions } from "utils"
|
||||||
|
|
||||||
@observer
|
@observer
|
||||||
@@ -29,15 +28,32 @@ class Bubble extends Component {
|
|||||||
|
|
||||||
@observable isLoading = true;
|
@observable isLoading = true;
|
||||||
@observable isOpen = false;
|
@observable isOpen = false;
|
||||||
@observable userSettings;
|
|
||||||
@observable projects;
|
@observable projects;
|
||||||
@observable tasks = [];
|
@observable lastProjectId;
|
||||||
@observable changeset = {
|
@observable lastTaskId;
|
||||||
project: null,
|
@observable changeset = {};
|
||||||
task: null,
|
|
||||||
hours: '',
|
@computed get changesetWithDefaults() {
|
||||||
description: ''
|
const { service } = this.props
|
||||||
};
|
|
||||||
|
const project = findLastProject(service.projectId || this.lastProjectId)(
|
||||||
|
this.projects
|
||||||
|
) || this.projects[0]
|
||||||
|
|
||||||
|
const defaults = {
|
||||||
|
id: service.id,
|
||||||
|
name: service.name,
|
||||||
|
project,
|
||||||
|
task: findLastTask(service.taskId || this.lastTaskId)(project),
|
||||||
|
hours: "",
|
||||||
|
description: service.description
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...defaults,
|
||||||
|
...this.changeset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const { settings } = this.props
|
const { settings } = this.props
|
||||||
@@ -59,21 +75,12 @@ class Bubble extends Component {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fetchData = () => {
|
fetchData = () => {
|
||||||
Promise.all([this.apiClient.login(), this.apiClient.projects()])
|
this.apiClient
|
||||||
.then(responses => {
|
.projects()
|
||||||
this.userSettings = get(responses, "[0].data")
|
.then(({ data }) => {
|
||||||
this.projects = groupedProjectOptions(
|
this.projects = groupedProjectOptions(data.projects)
|
||||||
get(responses, "[1].data.projects")
|
this.lastProjectId = data.last_project_id
|
||||||
)
|
this.lastTaskId = data.lastTaskId
|
||||||
|
|
||||||
const {
|
|
||||||
last_project_id: lastProjectId,
|
|
||||||
last_task_id: lastTaskId
|
|
||||||
} = this.userSettings
|
|
||||||
|
|
||||||
this.changeset.project = findLastProject(lastProjectId)(this.projects)
|
|
||||||
this.changeset.task = findLastTask(lastTaskId)(this.changeset.project)
|
|
||||||
|
|
||||||
this.isLoading = false
|
this.isLoading = false
|
||||||
})
|
})
|
||||||
.catch(console.error)
|
.catch(console.error)
|
||||||
@@ -96,7 +103,6 @@ class Bubble extends Component {
|
|||||||
|
|
||||||
if (name === "project") {
|
if (name === "project") {
|
||||||
this.changeset.task = null
|
this.changeset.task = null
|
||||||
this.tasks = value.tasks
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -125,8 +131,7 @@ class Bubble extends Component {
|
|||||||
<Content>
|
<Content>
|
||||||
<Form
|
<Form
|
||||||
projects={this.projects}
|
projects={this.projects}
|
||||||
tasks={this.tasks}
|
changeset={this.changesetWithDefaults}
|
||||||
changeset={this.changeset}
|
|
||||||
isLoading={this.isLoading}
|
isLoading={this.isLoading}
|
||||||
onChange={this.handleChange}
|
onChange={this.handleChange}
|
||||||
onSubmit={this.handleSubmit}
|
onSubmit={this.handleSubmit}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ class Form extends Component {
|
|||||||
hours: PropTypes.string
|
hours: PropTypes.string
|
||||||
}).isRequired,
|
}).isRequired,
|
||||||
projects: PropTypes.array.isRequired,
|
projects: PropTypes.array.isRequired,
|
||||||
tasks: PropTypes.array.isRequired,
|
|
||||||
onChange: PropTypes.func.isRequired,
|
onChange: PropTypes.func.isRequired,
|
||||||
onSubmit: PropTypes.func.isRequired
|
onSubmit: PropTypes.func.isRequired
|
||||||
};
|
};
|
||||||
@@ -34,7 +33,7 @@ class Form extends Component {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
const { projects, tasks, changeset, onChange, onSubmit } = this.props
|
const { projects, changeset, onChange, onSubmit } = this.props
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={onSubmit}>
|
<form onSubmit={onSubmit}>
|
||||||
@@ -49,7 +48,7 @@ class Form extends Component {
|
|||||||
<div className="form-group">
|
<div className="form-group">
|
||||||
<Select
|
<Select
|
||||||
name="task"
|
name="task"
|
||||||
options={tasks}
|
options={changeset.project?.tasks || []}
|
||||||
value={changeset.task}
|
value={changeset.task}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
noOptionsMessage={() => "Zuerst Projekt wählen"}
|
noOptionsMessage={() => "Zuerst Projekt wählen"}
|
||||||
|
|||||||
Reference in New Issue
Block a user