summaryrefslogtreecommitdiff
path: root/src/main/resources/META-INF/resources/admin/AdminElement.js
blob: 1fdfbe740fa2e3c05064fa6c73979fa5bac3e3a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import {html, render} from "../web_modules/lit-html.js";
import {createStore} from "../web_modules/redux.js";

export class AdminElement extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({mode: "open"});
        this.store = createStore(this.update.bind(this), {switched: false});
        this.store.subscribe(this.render.bind(this));
    }

    static get observedAttributes() {
        return ["greetee"];
    }

    connectedCallback () {
        this.render();
    }

    attributeChangedCallback(name, oldValue, newValue) {
        this.render();
    }

    onSwitchClicked(event) {
        this.store.dispatch({ type: "SWITCH" });
    }

    update(state, action) {
        console.log(`Processing action: ${JSON.stringify(action)}`);
        switch (action.type) {
            case "SWITCH":
                state.switched = !state.switched;
                break;
        }
        return state;
    }

    render() {
        let state = this.store.getState();
        let greetee = state.switched ? "Andreas" : this.getAttribute("greetee");

        const template = html`
            <p>Hello ${greetee}! <button @click=${this.onSwitchClicked.bind(this)}>Switch</button></p>
            `;

        render(template, this.shadowRoot);
    }
}

customElements.define("mlk-admin", AdminElement);