Refactor so that changeset can be used as activity params

This commit is contained in:
Manuel Bouza
2019-02-13 10:28:30 +01:00
parent a4f3049671
commit eb154be04a
4 changed files with 52 additions and 43 deletions

View File

@@ -41,7 +41,7 @@ class Bubble extends Component {
@observable lastProjectId; @observable lastProjectId;
@observable lastTaskId; @observable lastTaskId;
@observable changeset = {}; @observable changeset = {};
@observable errors = {}; @observable formErrors = {};
@computed get changesetWithDefaults() { @computed get changesetWithDefaults() {
const { service } = this.props const { service } = this.props
@@ -50,11 +50,16 @@ class Bubble extends Component {
findLastProject(service.projectId || this.lastProjectId)(this.projects) || findLastProject(service.projectId || this.lastProjectId)(this.projects) ||
head(this.projects) head(this.projects)
const task = findLastTask(service.taskId || this.lastTaskId)(project)
const defaults = { const defaults = {
id: service.id, remote_service: service.name,
name: service.name, remote_id: service.id,
project, remote_url: window.location.href,
task: findLastTask(service.taskId || this.lastTaskId)(project), date: currentDate(),
assignment_id: project?.value,
task_id: task?.value,
billable: task?.billable,
hours: "", hours: "",
description: service.description description: service.description
} }
@@ -65,29 +70,6 @@ class Bubble extends Component {
} }
} }
@computed get activityParams() {
const {
id,
name,
hours,
description,
project,
task
} = this.changesetWithDefaults
return {
date: currentDate(),
hours,
description,
assignment_id: project.value,
task_id: task.value,
billable: task.billable,
remote_service: name,
remote_id: id,
remote_url: window.location.href
}
}
componentDidMount() { componentDidMount() {
disposeOnUnmount( disposeOnUnmount(
this, this,
@@ -154,7 +136,7 @@ class Bubble extends Component {
this.changeset[name] = value this.changeset[name] = value
if (name === "project") { if (name === "assignment_id") {
this.changeset.task = null this.changeset.task = null
} }
}; };
@@ -162,11 +144,20 @@ class Bubble extends Component {
handleSubmit = event => { handleSubmit = event => {
event.preventDefault() event.preventDefault()
this.#apiClient this.#apiClient
.createActivity(this.activityParams) .createActivity(this.changesetWithDefaults)
.then(() => this.close()) .then(() => {
.catch(error => console.log(error)) this.close()
this.formErrors = {}
})
.catch(this.handleSubmitError)
}; };
handleSubmitError = error => {
if (error.response?.status === 422) {
this.formErrors = error.response.data
}
}
// RENDER ------------------------------------------------------------------- // RENDER -------------------------------------------------------------------
render() { render() {

View File

@@ -21,7 +21,7 @@ class Form extends Component {
isValid = () => { isValid = () => {
const { changeset } = this.props const { changeset } = this.props
return ["project", "task", "hours", "description"] return ["assignment_id", "task_id", "hours", "description"]
.map(prop => changeset[prop]) .map(prop => changeset[prop])
.every(Boolean) .every(Boolean)
}; };
@@ -34,22 +34,23 @@ class Form extends Component {
} }
const { projects, changeset, onChange, onSubmit } = this.props const { projects, changeset, onChange, onSubmit } = this.props
const project = Select.findOptionByValue(projects, changeset.assignment_id)
return ( return (
<form onSubmit={onSubmit}> <form onSubmit={onSubmit}>
<div className="form-group"> <div className="form-group">
<Select <Select
name="project" name="assignment_id"
options={projects} options={projects}
value={changeset.project} value={changeset.assignment_id}
onChange={onChange} onChange={onChange}
/> />
</div> </div>
<div className="form-group"> <div className="form-group">
<Select <Select
name="task" name="task_id"
options={changeset.project?.tasks || []} options={project?.tasks || []}
value={changeset.task} value={changeset.task_id}
onChange={onChange} onChange={onChange}
noOptionsMessage={() => "Zuerst Projekt wählen"} noOptionsMessage={() => "Zuerst Projekt wählen"}
/> />

View File

@@ -8,7 +8,9 @@ import {
join, join,
filter, filter,
compose, compose,
property property,
flatMap,
pathEq
} from "lodash/fp" } from "lodash/fp"
const customTheme = theme => ({ const customTheme = theme => ({
@@ -43,19 +45,33 @@ const filterOption = createFilter({
export default class Select extends Component { export default class Select extends Component {
static propTypes = { static propTypes = {
name: PropTypes.string.isRequired, name: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
options: PropTypes.array,
onChange: PropTypes.func.isRequired onChange: PropTypes.func.isRequired
};
static findOptionByValue = (selectOptions, value) => {
const options = flatMap(
option => (option.options ? option.options : option),
selectOptions
)
return options.find(pathEq("value", value))
} }
handleChange = value => { handleChange = option => {
const { name, onChange } = this.props const { name, onChange } = this.props
const { value } = option
onChange({ target: { name, value } }) onChange({ target: { name, value } })
} };
render() { render() {
const passThroughProps = this.props const { value, ...passThroughProps } = this.props
return ( return (
<ReactSelect <ReactSelect
{...passThroughProps} {...passThroughProps}
value={Select.findOptionByValue(this.props.options, value)}
onChange={this.handleChange} onChange={this.handleChange}
filterOption={filterOption} filterOption={filterOption}
theme={customTheme} theme={customTheme}

View File

@@ -61,4 +61,5 @@ export const trace = curry((tag, value) => {
export const currentDate = (locale = "de") => export const currentDate = (locale = "de") =>
format(new Date(), "YYYY-MM-DD", { locale }) format(new Date(), "YYYY-MM-DD", { locale })
export const extensionSettingsUrl = () => `chrome://extensions/?id=${chrome.runtime.id}` export const extensionSettingsUrl = () =>
`chrome://extensions/?id=${chrome.runtime.id}`