Adapt react-client to use the real backend API (#1545)

Co-authored-by: Philip Molares <philip.molares@udo.edu>
Co-authored-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Erik Michelson 2022-04-15 23:03:15 +02:00 committed by GitHub
parent 3399ed2023
commit 26f90505ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
227 changed files with 4726 additions and 2310 deletions

View file

@ -4,32 +4,37 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { Revision } from '../../../../api/revisions/types'
import { getUserById } from '../../../../api/users'
import type { UserResponse } from '../../../../api/users/types'
import type { RevisionDetails } from '../../../../api/revisions/types'
import { getUser } from '../../../../api/users'
import type { UserInfo } from '../../../../api/users/types'
import { download } from '../../../common/download/download'
import { Logger } from '../../../../utils/logger'
const log = new Logger('RevisionsUtils')
const DISPLAY_MAX_USERS_PER_REVISION = 9
export const downloadRevision = (noteId: string, revision: Revision | null): void => {
/**
* Downloads a given revision's content as markdown document in the browser.
* @param noteId The id of the note from which to download the revision.
* @param revision The revision details object containing the content to download.
*/
export const downloadRevision = (noteId: string, revision: RevisionDetails | null): void => {
if (!revision) {
return
}
download(revision.content, `${noteId}-${revision.timestamp}.md`, 'text/markdown')
download(revision.content, `${noteId}-${revision.createdAt}.md`, 'text/markdown')
}
export const getUserDataForRevision = (authors: string[]): UserResponse[] => {
const users: UserResponse[] = []
authors.forEach((author, index) => {
if (index > 9) {
return
}
getUserById(author)
.then((userData) => {
users.push(userData)
})
.catch((error: Error) => log.error(error))
})
/**
* Fetches user details for the given usernames while returning a maximum of 9 users.
* @param usernames The list of usernames to fetch.
* @throws {Error} in case the user-data request failed.
* @return An array of user details.
*/
export const getUserDataForRevision = async (usernames: string[]): Promise<UserInfo[]> => {
const users: UserInfo[] = []
const usersToFetch = Math.min(usernames.length, DISPLAY_MAX_USERS_PER_REVISION) - 1
for (let i = 0; i <= usersToFetch; i++) {
const user = await getUser(usernames[i])
users.push(user)
}
return users
}