Files: 55335207126eb15c3a0f1834ffb8bbcbd32658a8 / app / page / network / invite-pub.js
1715 bytesRaw
1 | const { h, Value, resolve, onceTrue, computed } = require('mutant') |
2 | const { isInvite } = require('ssb-ref') |
3 | |
4 | module.exports = function InvitePub ({ connection }) { |
5 | const state = { |
6 | invite: Value(), |
7 | inviteProcessing: Value(false), |
8 | inviteResult: Value(null) |
9 | } |
10 | |
11 | const body = h('InvitePub', [ |
12 | h('input', { |
13 | 'placeholder': 'invite code for a remote peer (pub)', |
14 | 'ev-input': handleInviteInput |
15 | }), |
16 | computed([state.invite, state.inviteProcessing], (invite, processing) => { |
17 | if (processing) return h('i.fa.fa-spinner.fa-pulse') |
18 | if (invite) return h('button -primary', { 'ev-click': useInvite }, 'use invite') |
19 | |
20 | return h('button', { disabled: 'disabled', title: 'not a valid invite code' }, 'use invite') |
21 | }), |
22 | computed(state.inviteResult, result => { |
23 | if (result === null) return |
24 | |
25 | return result |
26 | ? h('i.fa.fa-check') |
27 | : h('i.fa.fa-times') |
28 | }) |
29 | ]) |
30 | |
31 | function handleInviteInput (ev) { |
32 | state.inviteResult.set(null) |
33 | const invite = ev.target.value.replace(/^\s*"?/, '').replace(/"?\s*$/, '') |
34 | if (!isInvite(invite)) { |
35 | state.invite.set() |
36 | return |
37 | } |
38 | |
39 | ev.target.value = invite |
40 | state.invite.set(invite) |
41 | } |
42 | |
43 | function useInvite () { |
44 | state.inviteProcessing.set(true) |
45 | |
46 | onceTrue(connection, server => { |
47 | server.invite.accept(resolve(state.invite), (err, data) => { |
48 | state.inviteProcessing.set(false) |
49 | state.invite.set() |
50 | |
51 | if (err) { |
52 | state.inviteResult.set(false) |
53 | console.error(err) |
54 | return |
55 | } |
56 | state.inviteResult.set(true) |
57 | console.log(data) |
58 | }) |
59 | }) |
60 | } |
61 | |
62 | return { |
63 | title: 'pub invites (classic)', |
64 | body |
65 | } |
66 | } |
67 |
Built with git-ssb-web