forked from obumnwabude/github-invite
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
138 lines (128 loc) · 4.55 KB
/
app.js
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
// get the DOM objects
const inviteForm = document.querySelector('#invite');
const inputOrg = document.querySelector('#org');
const inputToken = document.querySelector('#token');
const inputUsername = document.querySelector('#username');
const submitButton = document.querySelector('button[type="submit"]');
const messageContainer = document.querySelector('#message-container');
const messageHeading = document.querySelector('#message-heading');
const messageBody = document.querySelector('#message-body');
const host = 'https://api.github.com';
/**
* Removes the feedback message from the DOM and makes the submit button clickable.
*/
const reInvite = () => {
if (!(messageContainer.classList.contains('hide'))) messageContainer.classList.add('hide');
if (submitButton.disabled) submitButton.disabled = false;
};
/**
* Adds the response message to the DOM
* @param {Object} data - the data from which the message to be added to the DOM is configured
*/
const displayMessage = (data) => {
if (data.status) {
messageHeading.classList.remove('failure');
messageHeading.classList.add('success');
addToDom(data);
} else {
messageHeading.classList.remove('success');
messageHeading.classList.add('failure');
addToDom(data);
}
submitButton.disabled = false;
};
/**
* Adds the provided message and body to the DOM and expose them
* @param {Object} data - the data to be added to the DOM
*/
const addToDom = (data) => {
messageHeading.innerHTML = data.message;
messageBody.innerHTML = data.body;
messageContainer.classList.remove('hide');
};
/**
* Invites a user to an organisation
* @param {Object} invitation - an object containing org,
* token and username properties which correspond to the
* organisation to which invitation is done, the token of
* an admin making the invitation and the username of the
* invitee.
* @return {Object} data indicating whether the invitation
* was successful or not.
*/
const invite = async (invitation) => {
try {
// check if github account exists with the given username, if no return that the username is invalid
const user = await fetch(`${host}/users/${invitation.username}`)
.then((response) => response.json());
if (!user.id) {
return {
status: false,
message: 'Invalid Username',
body: `GitHub user with username: ${invitation.username} not found. Please check username's spelling and try again. Thank you.`,
};
};
// ensure that GitHub organization exists else return
const organization = await fetch(`${host}/orgs/${invitation.org}`)
.then((response) => response.json());
if (!organization.id) {
return {
status: false,
message: 'Invalid Organization',
body: `GitHub organization with name: ${invitation.org} not found. Please check organization's name and try again. Thank you.`,
};
};
// invite user
return fetch(`${host}/orgs/${invitation.org}/invitations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': `token ${invitation.token}`,
},
body: `{"invitee_id":${user.id}}`
}).then((response) => {
// respond apprioprately
if (response.status == 201) {
return {
status: true,
message: 'Successfully Invited',
body: `Github user ${invitation.username} has been successfully invited to ${invitation.org} organisation.`
};
} else {
return response.json().then(data => {
let messages = [data.message];
if (data.errors) {
for (let error of data.errors) {
messages.push(error.message);
}
}
return {
status: false,
message: response.statusText,
body: messages.join('<br>'),
};
});
}
});
} catch (error) {
return {
status: false,
message: 'An Error Occured',
body: error.toString(),
};
}
};
// ensures that the messageContainer is hidden when data is being entered
inputOrg.addEventListener('focus', reInvite);
inputToken.addEventListener('focus', reInvite);
inputUsername.addEventListener('focus', reInvite);
// capture submit events and send
inviteForm.addEventListener('submit', async (e) => {
e.preventDefault();
submitButton.disabled = true;
// make invitation
const data = await invite(Object.fromEntries(new FormData(inviteForm)));
// update DOM depending on invitation status
displayMessage(data);
});