Preselect default task (#22)

This commit is contained in:
Manuel Bouza 2019-09-18 12:54:54 +02:00 committed by Tobias Miesel
parent 5e62e16751
commit 8a72f242f9
4 changed files with 95 additions and 47 deletions

View File

@ -14,6 +14,7 @@ import {
findProjectByValue, findProjectByValue,
findProjectByIdentifier, findProjectByIdentifier,
findTask, findTask,
defaultTask,
formatDate, formatDate,
} from "utils" } from "utils"
import { parseISO } from "date-fns" import { parseISO } from "date-fns"
@ -73,7 +74,7 @@ class App extends Component {
const { service, lastTaskId } = this.props const { service, lastTaskId } = this.props
return ( return (
findTask(this.changeset.task_id || service?.taskId || lastTaskId)(this.project) || findTask(this.changeset.task_id || service?.taskId || lastTaskId)(this.project) ||
head(this.project?.tasks) defaultTask(this.project?.tasks)
) )
} }
@ -121,7 +122,7 @@ class App extends Component {
if (name === "assignment_id") { if (name === "assignment_id") {
const project = findProjectByValue(value)(projects) const project = findProjectByValue(value)(projects)
this.changeset.task_id = head(project?.tasks)?.value this.changeset.task_id = defaultTask(project?.tasks)?.value
} }
} }

View File

@ -10,6 +10,8 @@ import {
find, find,
curry, curry,
pick, pick,
head,
defaultTo,
} from "lodash/fp" } from "lodash/fp"
import { format } from "date-fns" import { format } from "date-fns"
@ -42,11 +44,19 @@ export const findTask = id =>
get("tasks"), get("tasks"),
) )
export const defaultTask = tasks =>
compose(
defaultTo(head(tasks)),
find(pathEq("isDefault", true)),
nilToArray,
)(tasks)
function taskOptions(tasks) { function taskOptions(tasks) {
return tasks.map(({ id, name, billable }) => ({ return tasks.map(({ id, name, billable, default: isDefault }) => ({
label: billable ? name : `(${name})`, label: billable ? name : `(${name})`,
value: id, value: id,
billable, billable,
isDefault,
})) }))
} }

View File

@ -9,14 +9,16 @@ export const projects = [
{ {
id: 2733682, id: 2733682,
name: "Bugfixing", name: "Bugfixing",
billable: true billable: true,
default: false,
}, },
{ {
id: 2733681, id: 2733681,
name: "Development", name: "Development",
billable: true billable: true,
} default: true,
] },
],
}, },
{ {
id: 944724773, id: 944724773,
@ -28,24 +30,28 @@ export const projects = [
{ {
id: 1621304, id: 1621304,
name: "Roadmap Features", name: "Roadmap Features",
billable: true billable: true,
default: false,
}, },
{ {
id: 1621310, id: 1621310,
name: "Bugfixing", name: "Bugfixing",
billable: true billable: true,
default: false,
}, },
{ {
id: 1621305, id: 1621305,
name: "Quickwins", name: "Quickwins",
billable: true billable: true,
default: true,
}, },
{ {
id: 1621323, id: 1621323,
name: "Refactorings", name: "Refactorings",
billable: true billable: true,
} default: false,
] },
],
}, },
{ {
id: 944837106, id: 944837106,
@ -57,24 +63,28 @@ export const projects = [
{ {
id: 2500080, id: 2500080,
name: "Intercom & Mails", name: "Intercom & Mails",
billable: false billable: false,
default: false,
}, },
{ {
id: 2500081, id: 2500081,
name: "Demos", name: "Demos",
billable: false billable: false,
default: true,
}, },
{ {
id: 2506050, id: 2506050,
name: "Calls", name: "Calls",
billable: false billable: false,
default: false,
}, },
{ {
id: 2500084, id: 2500084,
name: "Importe", name: "Importe",
billable: false billable: false,
} default: false,
] },
],
}, },
{ {
id: 944621413, id: 944621413,
@ -86,23 +96,27 @@ export const projects = [
{ {
id: 874014, id: 874014,
name: "Entwicklung", name: "Entwicklung",
billable: true billable: true,
default: false,
}, },
{ {
id: 874015, id: 874015,
name: "Grafik", name: "Grafik",
billable: true billable: true,
default: false,
}, },
{ {
id: 874016, id: 874016,
name: "Konzept", name: "Konzept",
billable: true billable: true,
default: false,
}, },
{ {
id: 874017, id: 874017,
name: "Projektleitung", name: "Projektleitung",
billable: true billable: true,
} default: false,
] },
} ],
},
] ]

View File

@ -3,16 +3,25 @@ import {
findProjectByValue, findProjectByValue,
findProjectByIdentifier, findProjectByIdentifier,
findTask, findTask,
defaultTask,
groupedProjectOptions, groupedProjectOptions,
extractAndSetTag extractAndSetTag,
} from "../../src/js/utils" } from "../../src/js/utils"
import { map } from "lodash/fp" import { map, compose } from "lodash/fp"
const getProjectBy = finder => key =>
compose(
finder(key),
groupedProjectOptions,
)(projects)
const getProjectByValue = getProjectBy(findProjectByValue)
const getProjectByIdentifier = getProjectBy(findProjectByIdentifier)
describe("utils", () => { describe("utils", () => {
describe("findProjectByValue", () => { describe("findProjectByValue", () => {
it("finds an existing project", () => { it("finds an existing project", () => {
const options = groupedProjectOptions(projects) const project = getProjectByValue(944837106)
const project = findProjectByValue(944837106)(options)
expect(project.value).toEqual(944837106) expect(project.value).toEqual(944837106)
expect(project.label).toEqual("Support") expect(project.label).toEqual("Support")
expect(project.customerName).toEqual("MOCO APP") expect(project.customerName).toEqual("MOCO APP")
@ -20,14 +29,12 @@ describe("utils", () => {
}) })
it("returns undefined if project is not found", () => { it("returns undefined if project is not found", () => {
const options = groupedProjectOptions(projects) const project = getProjectByValue(123)
const project = findProjectByValue(123)(options)
expect(project).toBe(undefined) expect(project).toBe(undefined)
}) })
it("returns undefined for undefined id", () => { it("returns undefined for undefined id", () => {
const options = groupedProjectOptions(projects) const project = getProjectByValue(undefined)
const project = findProjectByValue(undefined)(options)
expect(project).toBe(undefined) expect(project).toBe(undefined)
}) })
}) })
@ -57,16 +64,14 @@ describe("utils", () => {
describe("findTask", () => { describe("findTask", () => {
it("find an existing task", () => { it("find an existing task", () => {
const options = groupedProjectOptions(projects) const project = getProjectByValue(944837106)
const project = findProjectByValue(944837106)(options)
const task = findTask(2506050)(project) const task = findTask(2506050)(project)
expect(task.value).toEqual(2506050) expect(task.value).toEqual(2506050)
expect(task.label).toEqual("(Calls)") expect(task.label).toEqual("(Calls)")
}) })
it("returns undefined if task is not found", () => { it("returns undefined if task is not found", () => {
const options = groupedProjectOptions(projects) const project = getProjectByValue(944837106)
const project = findProjectByValue(944837106)(options)
const task = findTask(123)(project) const task = findTask(123)(project)
expect(task).toBe(undefined) expect(task).toBe(undefined)
}) })
@ -77,14 +82,32 @@ describe("utils", () => {
}) })
}) })
describe("defaultTask", () => {
it("find a default task", () => {
const project = getProjectByValue(944837106)
const task = defaultTask(project.tasks)
expect(task.label).toBe("(Demos)")
})
it("returns first task if no default is defined", () => {
const project = getProjectByValue(944621413)
const task = defaultTask(project.tasks)
expect(task.label).toBe("Entwicklung")
})
it("return undefined if no tasks given", () => {
let task = defaultTask(null)
expect(task).toBeUndefined()
task = defaultTask([])
expect(task).toBeUndefined()
})
})
describe("groupedProjectOptions", () => { describe("groupedProjectOptions", () => {
it("transforms projects into grouped options by company", () => { it("transforms projects into grouped options by company", () => {
const result = groupedProjectOptions(projects) const result = groupedProjectOptions(projects)
expect(map("label", result)).toEqual([ expect(map("label", result)).toEqual(["Simplificator", "MOCO APP", "sharoo"])
"Simplificator",
"MOCO APP",
"sharoo"
])
}) })
}) })
@ -92,19 +115,19 @@ describe("utils", () => {
it("sets the correct tag and updates description", () => { it("sets the correct tag and updates description", () => {
const changeset = { const changeset = {
description: "#meeting Lorem ipsum", description: "#meeting Lorem ipsum",
tag: "" tag: "",
} }
expect(extractAndSetTag(changeset)).toEqual({ expect(extractAndSetTag(changeset)).toEqual({
description: "Lorem ipsum", description: "Lorem ipsum",
tag: "meeting" tag: "meeting",
}) })
}) })
it("only matches tag at the beginning", () => { it("only matches tag at the beginning", () => {
const changeset = { const changeset = {
description: "Lorem #meeting ipsum", description: "Lorem #meeting ipsum",
tag: "" tag: "",
} }
expect(extractAndSetTag(changeset)).toEqual(changeset) expect(extractAndSetTag(changeset)).toEqual(changeset)
@ -113,7 +136,7 @@ describe("utils", () => {
it("returns the changeset if not tag is set", () => { it("returns the changeset if not tag is set", () => {
const changeset = { const changeset = {
description: "Without tag", description: "Without tag",
tag: "" tag: "",
} }
expect(extractAndSetTag(changeset)).toEqual(changeset) expect(extractAndSetTag(changeset)).toEqual(changeset)