|
| 1 | +import { Subscription } from '@hilla/frontend'; |
| 2 | +import { LitElement, html } from 'lit'; |
| 3 | +import { customElement, query } from 'lit/decorators.js'; |
| 4 | +import { FluxEndpoint } from '../generated/endpoints'; |
| 5 | + |
| 6 | +@customElement('test-flux') |
| 7 | +export class TestFlux extends LitElement { |
| 8 | + @query('#content') |
| 9 | + private content!: HTMLElement; |
| 10 | + |
| 11 | + render() { |
| 12 | + return html` |
| 13 | + <div style="display:flex;flex-direction:column"> |
| 14 | + <form method="POST" action="login"> |
| 15 | + <input id="username" name="username" /> |
| 16 | + <input id="password" name="password" /> |
| 17 | + <input id="login" type="submit" /> |
| 18 | + </form> |
| 19 | +
|
| 20 | + <button id="denied" @click="${this.denied}">denied()</button> |
| 21 | + <button id="hello" @click="${this.hello}">hello()</button> |
| 22 | + <button id="helloAnonymous" @click="${this.helloAnonymous}">helloAnonymous()</button> |
| 23 | + <button id="helloAdmin" @click="${this.helloAdmin}">helloAdmin()</button> |
| 24 | + <button id="checkUser" @click="${this.checkUser}">checkUser()</button> |
| 25 | + <button id="countTo" @click=${() => this.countTo(5)}>countTo(5)</button> |
| 26 | + <button id="countEvenTo" @click="${() => this.countEvenTo(10)}">countEvenTo(10)</button> |
| 27 | + <button id="countThrowError" @click="${() => this.countThrowError(10)}">countThrowError(10)</button> |
| 28 | +
|
| 29 | + <pre id="content"></pre> |
| 30 | + </div> |
| 31 | + `; |
| 32 | + } |
| 33 | + |
| 34 | + private testEndpoint(sub: Subscription<any>) { |
| 35 | + this.content.textContent = ''; |
| 36 | + sub |
| 37 | + .onNext((value) => { |
| 38 | + this.content.textContent += 'Value: ' + value + '\n'; |
| 39 | + }) |
| 40 | + .onComplete(() => { |
| 41 | + this.content.textContent += 'Completed' + '\n'; |
| 42 | + }) |
| 43 | + .onError(() => { |
| 44 | + this.content.textContent += 'Error' + '\n'; |
| 45 | + }); |
| 46 | + } |
| 47 | + private denied() { |
| 48 | + this.testEndpoint(FluxEndpoint.denied()); |
| 49 | + } |
| 50 | + private helloAnonymous() { |
| 51 | + this.testEndpoint(FluxEndpoint.helloAnonymous()); |
| 52 | + } |
| 53 | + private hello() { |
| 54 | + this.testEndpoint(FluxEndpoint.hello('John', 'Manager')); |
| 55 | + } |
| 56 | + private helloAdmin() { |
| 57 | + this.testEndpoint(FluxEndpoint.helloAdmin()); |
| 58 | + } |
| 59 | + private checkUser() { |
| 60 | + this.testEndpoint(FluxEndpoint.checkUser()); |
| 61 | + } |
| 62 | + private countTo(to: number) { |
| 63 | + this.testEndpoint(FluxEndpoint.countTo(to)); |
| 64 | + } |
| 65 | + private countEvenTo(to: number) { |
| 66 | + this.testEndpoint(FluxEndpoint.countEvenTo(to)); |
| 67 | + } |
| 68 | + private countThrowError(to: number) { |
| 69 | + this.testEndpoint(FluxEndpoint.countThrowError(to)); |
| 70 | + } |
| 71 | +} |
0 commit comments