Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | /* Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | * |
| 3 | * Use of this source code is governed by a BSD-style |
| 4 | * license that can be found in the LICENSE file or at |
| 5 | * https://developers.google.com/open-source/licenses/bsd |
| 6 | */ |
| 7 | |
| 8 | const parentSelect = document.getElementById('parent_to_invite'); |
| 9 | const createButton = document.getElementById('create_linked_account_invite'); |
| 10 | const acceptButtons = document.querySelectorAll('.incoming_invite'); |
| 11 | const unlinkButtons = document.querySelectorAll('.unlink_account'); |
| 12 | |
| 13 | function CreateLinkedAccountInvite(ev) { |
| 14 | const email = parentSelect.value; |
| 15 | const message = { |
| 16 | email: email, |
| 17 | }; |
| 18 | const inviteCall = window.prpcClient.call( |
| 19 | 'monorail.Users', 'InviteLinkedParent', message); |
| 20 | inviteCall.then((resp) => { |
| 21 | location.reload(); |
| 22 | }).catch((reason) => { |
| 23 | console.error('Inviting failed: ' + reason); |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | function AcceptIncomingInvite(ev) { |
| 28 | const email = ev.target.attributes['data-email'].value; |
| 29 | const message = { |
| 30 | email: email, |
| 31 | }; |
| 32 | const acceptCall = window.prpcClient.call( |
| 33 | 'monorail.Users', 'AcceptLinkedChild', message); |
| 34 | acceptCall.then((resp) => { |
| 35 | location.reload(); |
| 36 | }).catch((reason) => { |
| 37 | console.error('Accepting failed: ' + reason); |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | function UnlinkAccounts(ev) { |
| 43 | const parent = ev.target.dataset.parent; |
| 44 | const child = ev.target.dataset.child; |
| 45 | const message = { |
| 46 | parent: {display_name: parent}, |
| 47 | child: {display_name: child}, |
| 48 | }; |
| 49 | const unlinkCall = window.prpcClient.call( |
| 50 | 'monorail.Users', 'UnlinkAccounts', message); |
| 51 | unlinkCall.then((resp) => { |
| 52 | location.reload(); |
| 53 | }).catch((reason) => { |
| 54 | console.error('Unlinking failed: ' + reason); |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | if (parentSelect) { |
| 60 | parentSelect.onchange = function(e) { |
| 61 | const email = parentSelect.value; |
| 62 | createButton.disabled = email ? '' : 'disabled'; |
| 63 | }; |
| 64 | } |
| 65 | |
| 66 | if (createButton) { |
| 67 | createButton.onclick = CreateLinkedAccountInvite; |
| 68 | } |
| 69 | |
| 70 | if (acceptButtons) { |
| 71 | for (const acceptButton of acceptButtons) { |
| 72 | acceptButton.onclick = AcceptIncomingInvite; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (unlinkButtons) { |
| 77 | for (const unlinkButton of unlinkButtons) { |
| 78 | unlinkButton.onclick = UnlinkAccounts; |
| 79 | } |
| 80 | } |