Skip to content
Snippets Groups Projects
Verified Commit 2e3ec13e authored by MickGe's avatar MickGe :tiger2:
Browse files

comment functions

parent 0dfd3b8b
Branches dev
No related tags found
No related merge requests found
......@@ -18,16 +18,26 @@ actionBar.appendChild(reloadButton);
reloadButton.innerHTML = "⟳";
reloadButton.title="Cliquer pour recharger";
/**
* The reverseSort function toggles the 'reverse' class on the news-feed-el element.
*/
const reverseSort = () => {
const newsFeedEl = document.getElementById("news-feed-el");
newsFeedEl.classList.toggle('reverse');
}
/**
* The function `cleanCookies` clears the values of the `token` and `instanceURL` cookies.
*/
const cleanCookies = () => {
document.cookie = 'token=""; expires=Thu, 01 Jan 1970 00:00:01 GMT"; samesite=lax; secure';
document.cookie = 'instanceURL=""; expires=Thu, 01 Jan 1970 00:00:01 GMT"; samesite=lax; secure';
}
/**
* The `reload` function removes the `news-feed-el` element from the `app` element and then calls the
* `loadContent` function.
*/
const reload = () => {
const newsFeedEl = document.getElementById('news-feed-el');
document.getElementById('app').removeChild(newsFeedEl);
......
......@@ -16,6 +16,10 @@ const instanceURL = new URL(promptURL);
document.cookie = `token=${token}; max-age=604800; samesite=lax; secure`;
document.cookie = `instanceURL=${encodeURIComponent(instanceURL)}; max-age=604800; samesite=lax; secure`;
/**
* The `loadContent` function retrieves data from an API, appends it to the DOM, updates the bookmarks
* count, and sets the document title based on the bookmarks count.
*/
export const loadContent = async () => {
const content = await getData(instanceURL, token);
document.getElementById('app').appendChild(content?.newsFeedEl || content);
......@@ -24,11 +28,13 @@ export const loadContent = async () => {
document.title = `${content.bookmarksNb} Mastodon unBookmarks`;
}
/**
* The function appends an element with the id 'app' to the document and then calls the loadContent
* function.
*/
const appendChildren = () => {
document.getElementById('app').appendChild(actionBar);
loadContent();
}
appendChildren();
// YaehBqDsKZwFtM7Nqa0ZO7TPE9RKQ_kMgzRNoRu7PZ0
\ No newline at end of file
appendChildren();
\ No newline at end of file
......@@ -2,6 +2,18 @@ const newsFeedEl = document.createElement('div');
newsFeedEl.classList.add('news-feed-el');
newsFeedEl.id = 'news-feed-el';
/**
* The function `newsFetch` is an asynchronous function that fetches news data from a given URL using a
* token for authorization and returns the response along with additional links extracted from the
* response headers.
* @param url - The `url` parameter is the URL of the API endpoint from which you want to fetch the
* news data. It should be a string representing the URL.
* @param token - The `token` parameter is a string that represents an authorization token. It is used
* to authenticate the request to the specified URL.
* @returns The function `newsFetch` returns a promise that resolves to an array. The array contains
* the JSON response from the fetch request as the first element, followed by an array of links
* extracted from the "link" header of the response.
*/
const newsFetch = async (url, token) => {
const response = await fetch(url, {mode: "cors", headers: { Authorization: `Bearer ${token}`}})
.then(
......@@ -16,6 +28,18 @@ const newsFetch = async (url, token) => {
return response;
};
/**
* The `unbookmark` function is used to remove a bookmark from a Mastodon status and update the UI
* accordingly.
* @param url - The `url` parameter is the base URL of the API endpoint you are making the request to.
* It should include the protocol (e.g., "https://") and the domain name.
* @param token - The `token` parameter is a string that represents the authentication token used for
* making API requests. It is typically obtained after a user has successfully logged in and
* authenticated with the server.
* @param id - The `id` parameter represents the ID of the status or post that you want to unbookmark.
* @param card - The `card` parameter is a reference to an HTML element that represents the bookmark
* card. It is used to manipulate the card's appearance by adding the 'disappeared' class to it.
*/
const unbookmark = async (url, token, id, card) => {
const unbookmarksURL = `${url}api/v1/statuses/${id}/unbookmark`
await fetch(unbookmarksURL, {mode: "cors", method: "POST", headers: { Authorization: `Bearer ${token}`}})
......@@ -33,8 +57,24 @@ const unbookmark = async (url, token, id, card) => {
})
}
/**
* The function `capitalizeFirstLetter` takes a string as input and returns the same string with the
* first letter capitalized.
* @param string - The parameter "string" is a string value that represents the input text that you
* want to capitalize the first letter of.
*/
const capitalizeFirstLetter = (string) => string.charAt(0).toUpperCase() + string.slice(1); // To capitalize weekday first letter
/**
* The function `htmlToot` takes a `toot` object and an array of `attachments` and returns an HTML
* string representing a toot with its content, attachments, and metadata.
* @param toot - The `toot` parameter is an object that represents a toot (a post) on a social media
* platform. It has the following properties:
* @param attachments - The `attachments` parameter is an array of strings representing the attachments
* (such as images or videos) associated with the `toot`. Each string in the array should be in HTML
* format.
* @returns The function `htmlToot` returns an HTML string that represents a toot (a post on Mastodon).
*/
const htmlToot = (toot, attachments) => {
return (`<div id="${toot.id}" class="toot">
<div class="banner">
......@@ -49,6 +89,16 @@ const htmlToot = (toot, attachments) => {
</div>`)
};
/**
* The `getData` function retrieves data from a specified URL using an API token, processes the data,
* and displays it in a news feed format.
* @param url - The `url` parameter is the base URL of the API endpoint you want to fetch data from. It
* should be a string.
* @param token - The `token` parameter is a string that represents the authentication token required
* to access the API. It is used to authenticate the user and authorize the API requests.
* @returns The function `getData` returns an object with two properties: `newsFeedEl` and
* `bookmarksNb`.
*/
export const getData = async (url, token) => {
const bookmarksURL = `${url}api/v1/bookmarks/?limit=40`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment