3
3
envoy ,
4
4
restate ,
5
5
sortContactsByAlias ,
6
+ filterPairedContacts ,
7
+ filterUnpairedContacts ,
6
8
timeago ,
7
9
getAvatar ,
8
10
} from '../helpers/utils.js'
@@ -15,6 +17,7 @@ const initialState = {
15
17
placement : 'contacts' ,
16
18
rendered : null ,
17
19
responsive : true ,
20
+ showUnpaired : false ,
18
21
delay : 500 ,
19
22
wallet : { } ,
20
23
contacts : [ ] ,
@@ -25,7 +28,23 @@ const initialState = {
25
28
) { } ,
26
29
header : state => html `
27
30
< header >
28
- < h6 > Contacts (${ state . contacts . length } )</ h6 >
31
+ < div class ="inline row gap-2 ">
32
+ < h5 class ="lh-2 "> Contacts</ h5 >
33
+ < button
34
+ id ="paired_contacts "
35
+ class ="pill rounded ${ ! state . showUnpaired ? ' active' : '' } "
36
+ title ="Paired Contacts "
37
+ >
38
+ Paired < span class ="indicator "> ${ state . contacts . filter ( filterPairedContacts ) . length } </ span >
39
+ </ button >
40
+ < button
41
+ id ="unpaired_contacts "
42
+ class ="pill rounded ${ state . showUnpaired ? ' active' : '' } "
43
+ title ="Unpaired Contacts "
44
+ >
45
+ Unpaired < span class ="indicator "> ${ state . contacts . filter ( filterUnpairedContacts ) . length } </ span >
46
+ </ button >
47
+ </ div >
29
48
< button
30
49
id ="add_contact "
31
50
class ="pill rounded "
@@ -43,10 +62,17 @@ const initialState = {
43
62
44
63
< div >
45
64
${
46
- state . contacts . length > 0
65
+ state . contacts ? .length > 0
47
66
? (
48
67
await Promise . all (
49
- state . contacts . map ( async c => await state . item ( c ) )
68
+ state . contacts
69
+ . filter (
70
+ state . showUnpaired
71
+ ? filterUnpairedContacts
72
+ : filterPairedContacts
73
+ )
74
+ . sort ( sortContactsByAlias )
75
+ . map ( async c => await state . item ( c ) ) ,
50
76
)
51
77
) . join ( '' )
52
78
: ''
@@ -60,7 +86,6 @@ const initialState = {
60
86
${ await state . footer ( state ) }
61
87
` ,
62
88
item : async c => {
63
- // console.warn('contact list item', c)
64
89
if ( 'string' === typeof c ) {
65
90
return html `
66
91
< article >
@@ -118,6 +143,18 @@ const initialState = {
118
143
< h4 > ${ itemAlias } </ h4 >
119
144
< h5 > ${ itemName } </ h5 >
120
145
</ address >
146
+ <!-- <aside class="inline row">
147
+ <button class="pill rounded">
148
+ <svg width="24" height="24" viewBox="0 0 24 24">
149
+ <use xlink:href="#icon-arrow-circle-up"></use>
150
+ </svg>
151
+ </button>
152
+ <button class="pill rounded">
153
+ <svg width="24" height="24" viewBox="0 0 24 24">
154
+ <use xlink:href="#icon-arrow-circle-down"></use>
155
+ </svg>
156
+ </button>
157
+ </aside> -->
121
158
</ a >
122
159
`
123
160
} ,
@@ -170,30 +207,19 @@ const initialState = {
170
207
elements : {
171
208
} ,
172
209
events : {
173
- // handleChange: state => event => {
174
- // event.preventDefault()
175
- // // console.log(
176
- // // 'handle balance change',
177
- // // [event.target],
178
- // // )
179
- // },
180
210
handleClick : state => event => {
181
211
event . preventDefault ( )
182
- // console.log(
183
- // 'handle contacts click',
184
- // event,
185
- // state,
186
- // )
212
+ event . stopPropagation ( )
213
+ console . log (
214
+ 'handle contacts click' ,
215
+ event ,
216
+ state ,
217
+ )
187
218
} ,
188
219
handleContactsChange : ( newState , oldState ) => {
189
220
if ( newState . contacts !== oldState . contacts ) {
190
- // console.log(
191
- // 'handle contacts update',
192
- // {newState, oldState}
193
- // )
194
-
195
221
newState . render ?. ( {
196
- contacts : newState . contacts ?. sort ( sortContactsByAlias ) ,
222
+ contacts : newState . contacts
197
223
} )
198
224
}
199
225
}
@@ -208,28 +234,19 @@ let state = envoy(
208
234
export async function setupContactsList (
209
235
el , setupState = { }
210
236
) {
211
- console . log (
212
- 'setupContactsList state.contacts' ,
213
- state . contacts
214
- )
215
237
restate ( state , setupState )
216
- // if (setupState?.events?.handleContactsChange) {
217
- // state._listeners = [
218
- // setupState?.events?.handleContactsChange
219
- // ]
220
- // }
221
238
222
239
state . slugs . section = `${ state . name } _${ state . id } `
223
240
. toLowerCase ( ) . replace ( ' ' , '_' )
224
241
225
242
const section = document . createElement ( 'section' )
226
243
227
- state . elements . section = section
228
-
229
244
section . id = state . slugs . section
230
245
section . classList . add ( state . placement || '' )
231
246
section . innerHTML = await state . content ( state )
232
247
248
+ state . elements . section = section
249
+
233
250
function addListener (
234
251
node ,
235
252
event ,
@@ -240,21 +257,8 @@ export async function setupContactsList(
240
257
node . addEventListener ( event , handler , capture )
241
258
}
242
259
243
- function addListeners ( ) {
244
- // addListener(
245
- // section,
246
- // 'close',
247
- // state.events.handleChange(state)
248
- // )
249
- addListener (
250
- section ,
251
- 'click' ,
252
- state . events . handleClick ( state ) ,
253
- )
254
- }
255
-
256
260
function removeAllListeners (
257
- targets = [ section ] ,
261
+ targets = [ state . elements . section ] ,
258
262
) {
259
263
_handlers = _handlers
260
264
. filter ( ( { node, event, handler, capture } ) => {
@@ -266,19 +270,28 @@ export async function setupContactsList(
266
270
} )
267
271
}
268
272
273
+ function addListeners ( ) {
274
+ addListener (
275
+ state . elements . section ,
276
+ 'click' ,
277
+ state . events . handleClick ( state ) ,
278
+ )
279
+ }
280
+
269
281
state . removeAllListeners = removeAllListeners
282
+ state . addListeners = addListeners
270
283
271
284
async function render (
272
285
renderState = { } ,
273
286
position = 'afterbegin' ,
274
287
) {
275
288
await restate ( state , renderState )
276
289
277
- section . id = state . slugs . section
278
- section . innerHTML = await state . content ( state )
290
+ state . elements . section . id = state . slugs . section
291
+ state . elements . section . innerHTML = await state . content ( state )
279
292
280
- removeAllListeners ( )
281
- addListeners ( )
293
+ state . removeAllListeners ( )
294
+ state . addListeners ( )
282
295
283
296
if ( ! state . rendered ) {
284
297
el . insertAdjacentElement ( position , section )
0 commit comments