-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathia-user-list-settings.ts
More file actions
203 lines (181 loc) · 5.12 KB
/
ia-user-list-settings.ts
File metadata and controls
203 lines (181 loc) · 5.12 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { html, css, LitElement } from 'lit';
import { property, customElement, query } from 'lit/decorators.js';
import { Result } from '@internetarchive/result-type';
import IAButtonStyles from './style/ia-button';
import { UserListsService } from './user-lists-service/user-lists-service';
import { UserListsError } from './user-lists-service/user-lists-error';
import { UserList, UserListOptions } from './user-lists-service/models';
export interface UserListModel {
id?: string;
list_name: string;
description?: string;
is_private?: boolean;
date_created?: Date;
date_updated?: Date;
}
@customElement('iaux-userlist-settings')
export class IAUserListSettings extends LitElement {
/**
* contains userlist information
*/
@property({ type: Object }) userList?: UserListModel;
/**
* User lists service to use for creating/updating lists
*/
@property({ type: Object }) userListsService: UserListsService;
@query('#id') private listId: HTMLInputElement;
@query('#name') private listName: HTMLInputElement;
@query('#description') private listDescription: HTMLInputElement;
@query('#private') private listPrivate: HTMLInputElement;
private async saveListDetails(event: Event) {
event.preventDefault();
const submitButton = (event.target as HTMLElement)?.querySelector(
'button#save-list-settings'
);
submitButton?.setAttribute('disabled', 'true');
this.dispatchEvent(new Event('userListSaving'));
try {
const userListData: UserListOptions = {
list_name: this.listName.value,
description: this.listDescription.value,
is_private: this.listPrivate.checked,
};
let response: Result<UserList, UserListsError>;
if (this.userList?.id) {
response = await this.userListsService?.updateList(
this.userList.id,
userListData
);
} else {
response = await this.userListsService?.createList(userListData);
}
if (response.success) {
this.dispatchEvent(
new CustomEvent<UserList>('userListSaved', {
detail: response.success,
})
);
} else {
throw response.error;
}
} catch (error) {
this.dispatchEvent(
new CustomEvent('userListError', {
detail: { error },
})
);
// eslint-disable-next-line no-console
console.log('error', error);
submitButton?.removeAttribute('disabled');
}
}
private emitCloseModalEvent(e: Event) {
e.preventDefault();
this.dispatchEvent(new Event('listModalClosed'));
}
render() {
return html`
<section class="new-list">
<form id="user-list-form" @submit=${this.saveListDetails}>
<div class="field">
<input type="hidden" id="id" .value=${this.userList?.id ?? ''} />
<label for="name">List name*</label>
<input
type="text"
id="name"
.value=${this.userList?.list_name ?? ''}
required
/>
</div>
<div class="field">
<label for="description">Description</label>
<textarea
id="description"
.value=${this.userList?.description ?? ''}
></textarea>
</div>
<div class="field">
<label for="private">Private list</label>
<input
type="checkbox"
id="private"
.checked="${this.userList?.is_private ?? false}"
/>
</div>
<div class="footer field">
<button
type="button"
class="ia-button dark"
id="cancel"
@click=${this.emitCloseModalEvent}
>
Cancel
</button>
<button
type="submit"
id="save-list-settings"
class="ia-button primary"
>
Save
</button>
</div>
</form>
</section>
`;
}
static styles = [
IAButtonStyles,
css`
:host {
display: block;
padding: 15px 0 0 0;
font-size: 14px;
position: relative;
}
.field {
margin-bottom: 10px;
display: flex;
align-items: center;
}
label {
display: inline-block;
width: 90px;
text-align: left;
margin-right: 10px;
font-weight: 700;
}
input[type='text'],
textarea {
line-height: 20px;
padding: 5px;
border-radius: 5px;
width: 270px;
font-family: inherit;
font-size: inherit;
resize: none;
border-style: solid;
outline: revert;
border-radius: 4px;
border-width: 1px;
border-color: #999;
}
input[type='checkbox'] {
cursor: pointer;
margin: 0;
}
label[for='private'] {
cursor: pointer;
}
.footer {
display: block;
text-align: center;
margin: 10px 0 0 0;
}
.ia-button {
display: initial;
padding: 0 15px;
height: 3.5rem;
}
`,
];
}