From 533be5a417a6c383fe85111a90a57557ec81d3fb Mon Sep 17 00:00:00 2001 From: Manuel Bouza Date: Thu, 14 Feb 2019 14:26:48 +0100 Subject: [PATCH] Fetch and show booked hours for service --- src/css/main.scss | 2 ++ src/js/api/Client.js | 5 +++++ src/js/components/Bubble.js | 36 +++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/css/main.scss b/src/css/main.scss index d5461aa..327054d 100644 --- a/src/css/main.scss +++ b/src/css/main.scss @@ -14,8 +14,10 @@ border-radius: 50%; box-shadow: -1px -1px 15px 4px rgba(0, 0, 0, 0.05), 2px 2px 15px 4px rgba(0, 0, 0, 0.05); + padding: 5px; display: flex; + flex-direction: column; justify-content: space-around; align-items: center; diff --git a/src/js/api/Client.js b/src/js/api/Client.js index 8e130f5..87125dd 100644 --- a/src/js/api/Client.js +++ b/src/js/api/Client.js @@ -24,5 +24,10 @@ export default class Client { projects = () => this.#client.get("projects"); + bookedHours = service => + this.#client.get("activities/tags", { + params: { selection: [service.id], remote_service: service.name } + }); + createActivity = activity => this.#client.post("activities", { activity }); } diff --git a/src/js/components/Bubble.js b/src/js/components/Bubble.js index 7996915..a1832bb 100644 --- a/src/js/components/Bubble.js +++ b/src/js/components/Bubble.js @@ -40,6 +40,7 @@ class Bubble extends Component { @observable projects; @observable lastProjectId; @observable lastTaskId; + @observable bookedHours; @observable changeset = {}; @observable formErrors = {}; @@ -58,7 +59,7 @@ class Bubble extends Component { remote_url: window.location.href, date: currentDate(), assignment_id: project?.value, - task_id: task?.value, + task_id: task?.value, billable: task?.billable, hours: "", description: service.description @@ -70,18 +71,28 @@ class Bubble extends Component { } } + constructor(props) { + super(props) + this.#apiClient = new ApiClient(props.settings) + } + componentDidMount() { disposeOnUnmount( this, reaction( - () => - this.hasMissingConfiguration() ? null : this.props.settings, - this.fetchData, + () => (this.hasMissingConfiguration() ? null : this.props.settings), + this.fetchProjects, { fireImmediately: true } ) ) + disposeOnUnmount( + this, + reaction(() => this.props.service, this.fetchBookedHours, { + fireImmediately: true + }) + ) window.addEventListener("keydown", this.handleKeyDown) } @@ -102,7 +113,7 @@ class Bubble extends Component { return ["subdomain", "apiKey", "version"].some(key => !settings[key]) }; - fetchData = settings => { + fetchProjects = settings => { if (!settings) { return } @@ -121,6 +132,16 @@ class Bubble extends Component { .finally(() => (this.isLoading = false)) }; + fetchBookedHours = service => { + this.isLoading = true + + this.#apiClient + .bookedHours(service) + .then(({ data }) => (this.bookedHours = data[0]?.hours)) + .catch(console.error) + .finally(() => (this.isLoading = false)) + }; + // EVENT HANDLERS ----------------------------------------------------------- handleKeyDown = event => { @@ -157,13 +178,13 @@ class Bubble extends Component { if (error.response?.status === 422) { this.formErrors = error.response.data } - } + }; // RENDER ------------------------------------------------------------------- render() { if (this.isLoading) { - return null + return "Loading..." } let content @@ -191,6 +212,7 @@ class Bubble extends Component { src={chrome.extension.getURL(logoUrl)} width="50%" /> + {this.bookedHours && {this.bookedHours}} {this.isOpen && ( {content}