diff --git a/frontend/src/components/budget-add.ts b/frontend/src/components/budget-add.ts index cb9f46c..7152d2a 100644 --- a/frontend/src/components/budget-add.ts +++ b/frontend/src/components/budget-add.ts @@ -1,52 +1,84 @@ import { LitElement, html } from 'lit-element' -const INPUT_ID = 'new-budget-uuid' +export class JoinBudgetEvent extends CustomEvent<{ uuid: string }> { } -export class AddBudgetEvent extends CustomEvent<{ uuid: string }> { } +export class CreateBudgetEvent extends CustomEvent<{ amount: number, currency: string }> { } class BudgetAddComponent extends LitElement { - hasError: boolean + hasCreateError: boolean + hasJoinError: boolean static get properties() { return { - hasError: { type: String } + hasCreateError: { type: Boolean }, + hasJoinError: { type: Boolean } } } constructor() { super() - this.hasError = false + this.hasCreateError = false + this.hasJoinError = false } - onSubmit(e: Event) { + onCreate(e: Event) { + e.preventDefault() const root = this.shadowRoot - const inputEl = root.getElementById(INPUT_ID) + const amountInputEl = root.getElementById('create-budget-amount') + const currencyInputEl = root.getElementById('create-budget-currency') - const success = this.dispatchEvent(new AddBudgetEvent( - 'addBudget', + const amount = parseInt(amountInputEl.value) + if (isNaN(amount)) { + this.hasCreateError = true + return + } + + this.dispatchEvent(new CreateBudgetEvent( + 'createBudget', + { detail: { amount, currency: currencyInputEl.value } } + )) + + amountInputEl.value = '' + currencyInputEl.value = '' + } + + onJoin(e: Event) { + e.preventDefault() + const root = this.shadowRoot + const inputEl = root.getElementById('join-budget-uuid') + + this.dispatchEvent(new JoinBudgetEvent( + 'joinBudget', { detail: { uuid: inputEl.value } } )); - e.preventDefault() inputEl.value = '' } render() { return html` -

Join a budget

-
+

Create a budget…

+ + + + + ${this.hasCreateError ? html`

Could not create budget, check the amount and currency.

` : null} +
+ +

… or join existing budget

+

Add below the budget secret that you can get from the person who created the budget.

- - ${this.hasError ? html`

Could not add given budget, please check it is correctly typed.

` : undefined} + + ${this.hasJoinError ? html`

Could not add given budget, please check it is correctly typed.

` : null}
` } diff --git a/frontend/src/components/week-budget.ts b/frontend/src/components/week-budget.ts index 84c24ac..621692e 100644 --- a/frontend/src/components/week-budget.ts +++ b/frontend/src/components/week-budget.ts @@ -1,22 +1,24 @@ import { LitElement, html } from 'lit-element' import { Budget } from '../services/models/budget' -import { AddBudgetEvent } from './budget-add' +import { CreateBudgetEvent, JoinBudgetEvent } from './budget-add' import './budget-add' import './budget-list' import { readBudget, readEvent } from '../services/readers' -import { getBudget, GetError, addEvent, resetBudget } from '../services/api' +import { getBudget, GetError, addEvent, resetBudget, createBudget, CreationError } from '../services/api' import { getFromLocal, storeToLocal } from '../services/storage' import { SpendMoneyEvent, ResetBudgetEvent, RemoveBudgetEvent } from './budget-display' import { ETIME } from 'constants'; class WeekBudgetComponent extends LitElement { budgets: Budget[] = [] - hasAddError: boolean = false + hasCreateError: boolean = false + hasJoinError: boolean = false static get properties() { return { budgets: { type: Array }, - hasAddError: { type: Boolean } + hasCreateError: { type: Boolean }, + hasJoinError: { type: Boolean } } } @@ -33,8 +35,24 @@ class WeekBudgetComponent extends LitElement { } } - async onAddBudget(e: AddBudgetEvent) { - this.hasAddError = false + async onCreateBudget(e: CreateBudgetEvent) { + this.hasCreateError = false + + try { + const budget = await createBudget(e.detail.amount, e.detail.currency) + const budgetObj = readBudget(budget) + this.budgets = [...this.budgets, budgetObj] + storeToLocal(this.budgets) + } + catch (err) { + if (err instanceof CreationError) { + this.hasCreateError = true + } + } + } + + async onJoinBudget(e: JoinBudgetEvent) { + this.hasJoinError = false try { const budget = await this._getBudget(e.detail.uuid) @@ -43,7 +61,7 @@ class WeekBudgetComponent extends LitElement { } catch (err) { if (err instanceof GetError) { - this.hasAddError = true + this.hasJoinError = true } } } @@ -100,7 +118,12 @@ class WeekBudgetComponent extends LitElement { @removeBudget=${this.onRemoveBudget} .budgets=${this.budgets} > - + ` }