|
| 1 | +function getParticipants() { |
| 2 | + // Define the W3C's group ID |
| 3 | + const GROUP_ID = "83726"; |
| 4 | + |
| 5 | + // Define the base URL for the API, using the group ID |
| 6 | + const BASE_URL = `https://api.w3.org/groups/${GROUP_ID}`; |
| 7 | + |
| 8 | + // Define the ID of the list element where the user information will be inserted |
| 9 | + const LIST_ID = "ack_group"; |
| 10 | + |
| 11 | + /** |
| 12 | + * Fetches users and their affiliations from the API and returns an array of user information. |
| 13 | + * Each element in the array is an object containing the user's title and affiliation. |
| 14 | + * |
| 15 | + * @async |
| 16 | + * @function getUsersInfo |
| 17 | + * @returns {Promise<Array<{title: string, affiliation: string}>>} - A promise that resolves to an array of user information. |
| 18 | + */ |
| 19 | + async function getUsersInfo() { |
| 20 | + // Send a GET request to the users endpoint |
| 21 | + const response = await fetch(`${BASE_URL}/users`); |
| 22 | + // Fetch the JSON data from the response |
| 23 | + const data = await response.json(); |
| 24 | + // Extract the list of users |
| 25 | + const users = data._links.users; |
| 26 | + |
| 27 | + // Initialize an empty array to store user information |
| 28 | + let usersInfo = []; |
| 29 | + // Iterate over each user |
| 30 | + for (const user of users) { |
| 31 | + // Fetch the affiliation of the current user |
| 32 | + const affiliation = await getAffiliation(user); |
| 33 | + // Push an object containing the user's title and affiliation to the usersInfo array |
| 34 | + usersInfo.push({ title: user.title, affiliation: affiliation }); |
| 35 | + } |
| 36 | + // Return the array containing information of all users |
| 37 | + return usersInfo; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Fetches the affiliation of a given user from the API. |
| 42 | + * |
| 43 | + * @async |
| 44 | + * @function getAffiliation |
| 45 | + * @param {Object} user - The user object. |
| 46 | + * @returns {Promise<string>} - A promise that resolves to the title of the user's affiliation. |
| 47 | + */ |
| 48 | + async function getAffiliation(user) { |
| 49 | + // Send a GET request to the affiliations endpoint of the user |
| 50 | + const response = await fetch(user.href + "/affiliations/"); |
| 51 | + |
| 52 | + // Fetch the JSON data from the response |
| 53 | + const affiliations = await response.json(); |
| 54 | + |
| 55 | + // Extract the title of the first affiliation |
| 56 | + const affiliation = affiliations._links.affiliations[0].title; |
| 57 | + |
| 58 | + // Return the title of the affiliation |
| 59 | + return affiliation; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Fetches users and their affiliations, creates a list item for each user with their title and affiliation, |
| 64 | + * and appends these list items to a specified list in the document. |
| 65 | + * |
| 66 | + * @async |
| 67 | + * @function insertUsersInfoIntoDocument |
| 68 | + */ |
| 69 | + async function insertUsersInfoIntoDocument() { |
| 70 | + const usersInfo = await getUsersInfo(); |
| 71 | + const usersList = document.querySelector(`#${LIST_ID} ul`); |
| 72 | + |
| 73 | + for (const user of usersInfo) { |
| 74 | + const li = document.createElement("li"); |
| 75 | + li.textContent = `${user.title} (${user.affiliation})`; |
| 76 | + usersList.appendChild(li); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + insertUsersInfoIntoDocument(); |
| 81 | +} |
0 commit comments