-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomponent.ts
77 lines (66 loc) · 1.51 KB
/
component.ts
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/****************
* UI COMPONENT *
****************/
import { ApolloQuery, css, customElement, html } from '@apollo-elements/lit-apollo';
import { gql } from '@apollo/client/core';
interface Launch {
id: string;
missionName: string;
selected: boolean;
}
interface Data {
launches: readonly Launch[];
}
@customElement('query-element')
class QueryElement extends ApolloQuery<Data, null> {
query = gql`
query LaunchesQuery {
launches(limit: 10) {
id
missionName: mission_name
selected @client
}
}
`;
static styles = css`
:host {
display: grid;
}
`;
render() {
const launches = this.data?.launches ?? [];
return html`
${this.loading ? '...loading' : ''}
<ol>
${launches.map(x => html`
<li>
<label>
<input type="checkbox" .checked="${x.selected}" data-id="${x.id}" @input="${this.onInput}"/>
${x.missionName}
</label>
</li>
`)}
</ol>
`;
}
/**
* When user clicks the checkbox, update the stored state
*/
onInput(event: Event & { target: HTMLInputElement }) {
const fragment = gql`
fragment selected on Launch {
selected @client
}
`;
const id =
`Launch:${event.target.dataset.id}`;
const selected =
event.target.checked;
this.client.cache.writeFragment({ id, fragment, data: { selected } })
}
}
declare global {
interface HTMLElementTagNameMap {
'query-element': QueryElement
}
}