Create activity

This commit is contained in:
Manuel Bouza
2019-02-11 10:14:09 +01:00
parent c9f589f223
commit 7b405a6de3
6 changed files with 60 additions and 14 deletions

View File

@@ -11,6 +11,7 @@
}, },
"dependencies": { "dependencies": {
"axios": "^0.18.0", "axios": "^0.18.0",
"date-fns": "^1.30.1",
"lodash": "^4.17.11", "lodash": "^4.17.11",
"mobx": "^5.5.0", "mobx": "^5.5.0",
"mobx-react": "^5.2.8", "mobx-react": "^5.2.8",

View File

@@ -1,8 +1,8 @@
import axios from "axios" import axios from "axios"
export default class Client { export default class Client {
#client #client;
#apiKey #apiKey;
constructor({ subdomain, apiKey, clientVersion }) { constructor({ subdomain, apiKey, clientVersion }) {
this.#apiKey = apiKey this.#apiKey = apiKey
@@ -20,7 +20,9 @@ export default class Client {
}) })
} }
login = () => this.#client.post("session", { api_key: this.#apiKey }) login = () => this.#client.post("session", { api_key: this.#apiKey });
projects = () => this.#client.get("projects") projects = () => this.#client.get("projects");
createActivity = activity => this.#client.post("activities", { activity });
} }

View File

@@ -6,7 +6,12 @@ import Form from "components/Form"
import { observable, computed } 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 { findLastProject, findLastTask, groupedProjectOptions } from "utils" import {
findLastProject,
findLastTask,
groupedProjectOptions,
currentDate
} from "utils"
@observer @observer
class Bubble extends Component { class Bubble extends Component {
@@ -26,6 +31,8 @@ class Bubble extends Component {
}) })
}; };
#apiClient;
@observable isLoading = true; @observable isLoading = true;
@observable isOpen = false; @observable isOpen = false;
@observable projects; @observable projects;
@@ -36,9 +43,9 @@ class Bubble extends Component {
@computed get changesetWithDefaults() { @computed get changesetWithDefaults() {
const { service } = this.props const { service } = this.props
const project = findLastProject(service.projectId || this.lastProjectId)( const project =
this.projects findLastProject(service.projectId || this.lastProjectId)(this.projects) ||
) || this.projects[0] this.projects[0]
const defaults = { const defaults = {
id: service.id, id: service.id,
@@ -55,9 +62,32 @@ 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() {
const { settings } = this.props const { settings } = this.props
this.apiClient = new ApiClient(settings) this.#apiClient = new ApiClient(settings)
this.fetchData() this.fetchData()
window.addEventListener("keydown", this.handleKeyDown) window.addEventListener("keydown", this.handleKeyDown)
} }
@@ -75,7 +105,7 @@ class Bubble extends Component {
}; };
fetchData = () => { fetchData = () => {
this.apiClient this.#apiClient
.projects() .projects()
.then(({ data }) => { .then(({ data }) => {
this.projects = groupedProjectOptions(data.projects) this.projects = groupedProjectOptions(data.projects)
@@ -108,7 +138,10 @@ class Bubble extends Component {
handleSubmit = event => { handleSubmit = event => {
event.preventDefault() event.preventDefault()
this.close() this.#apiClient
.createActivity(this.activityParams)
.then(() => this.close())
.catch(error => console.log(error))
}; };
// RENDER ------------------------------------------------------------------- // RENDER -------------------------------------------------------------------

View File

@@ -9,6 +9,7 @@ import {
find, find,
curry curry
} from "lodash/fp" } from "lodash/fp"
import { format } from "date-fns"
const nilToArray = input => input || [] const nilToArray = input => input || []
@@ -25,9 +26,10 @@ export const findLastTask = id =>
) )
function taskOptions(tasks) { function taskOptions(tasks) {
return tasks.map(({ id, name }) => ({ return tasks.map(({ id, name, billable }) => ({
label: name, label: name,
value: id value: id,
billable
})) }))
} }
@@ -55,3 +57,6 @@ export const trace = curry((tag, value) => {
console.log(tag, value) console.log(tag, value)
return value return value
}) })
export const currentDate = (locale = "de") =>
format(new Date(), "YYYY-MM-DD", { locale })

View File

@@ -34,7 +34,7 @@ export const createEnhancer = document => services => (key, url) => {
...service, ...service,
key, key,
url, url,
id: evaluate(service.id), id: evaluate(service.id) || match.id,
description: evaluate(service.description), description: evaluate(service.description),
projectId: evaluate(service.projectId), projectId: evaluate(service.projectId),
taskId: evaluate(service.taskId), taskId: evaluate(service.taskId),

View File

@@ -2116,6 +2116,11 @@ data-urls@^1.0.0:
whatwg-mimetype "^2.2.0" whatwg-mimetype "^2.2.0"
whatwg-url "^7.0.0" whatwg-url "^7.0.0"
date-fns@^1.30.1:
version "1.30.1"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.30.1.tgz#2e71bf0b119153dbb4cc4e88d9ea5acfb50dc05c"
integrity sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==
date-now@^0.1.4: date-now@^0.1.4:
version "0.1.4" version "0.1.4"
resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b" resolved "https://registry.yarnpkg.com/date-now/-/date-now-0.1.4.tgz#eaf439fd4d4848ad74e5cc7dbef200672b9e345b"