Skip to content

Commit b1a3fd9

Browse files
authored
Merge pull request #393 from cornell-dti/add-utils-documentation
Add utils documentation
2 parents 4d17f78 + e968d58 commit b1a3fd9

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

frontend/src/utils/adminTool.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import { admins } from '../constants/HomeConsts';
2+
3+
/**
4+
* isAdmin – Checks whether a given user is an admin.
5+
*
6+
* @remarks
7+
* This function verifies if the user provided has an email that matches one of the predefined admin emails.
8+
*
9+
* @param {firebase.User | null} user – The Firebase user object to check. Can be `null`.
10+
*
11+
* @returns {boolean} – `true` if the user is an admin, otherwise `false`.
12+
*/
13+
214
export const isAdmin = (user: firebase.User | null) => {
315
if (user && typeof user?.email === 'string' && admins.includes(user?.email)) return true;
416
return false;

frontend/src/utils/average.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { ReviewWithId } from '../../../common/types/db-types';
22

3+
/**
4+
* getAverageRating – Calculates the average overall rating from an array of reviews.
5+
*
6+
* @remarks
7+
* This function sums the `overallRating` values of all reviews where the rating is greater than 0
8+
* and divides the sum by the total number of reviews in the array. If all reviews have a rating of 0,
9+
* the function returns 0.
10+
*
11+
* @param {ReviewWithId[]} reviewData – Array of ReviewWithId objects (reviews)
12+
*
13+
* @returns {number} – The average overall rating of the provided reviews.
14+
*/
15+
316
const getAverageRating = (reviewData: ReviewWithId[]) =>
417
reviewData.reduce(
518
(currSum, { overallRating }) => (overallRating > 0 ? currSum + overallRating : currSum),

frontend/src/utils/call.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ export type GetOptions<T> = {
55
errorHandler?: () => void;
66
body?: any;
77
};
8+
/**
9+
* get – Sends a GET request to the specified backend API endpoint.
10+
*
11+
* @remarks
12+
* This function simplifies the process of making a GET request using Axios by only requiring the route as it will
13+
* handle the callback, error console log, and error handling for you. This function doesn't return a value but will
14+
* execute the callback function (if provided) with the response data if successful.
15+
*
16+
* @param {string} route – The API endpoint to send the GET request to.
17+
* @param {GetOptions<T>} options – Optional parameters including: callback for handling the response, error handler
18+
* for handling request failures, and an optional request body.
19+
* @param {AxiosRequestConfig} inputConfig – Optional Axios configuration to customize the request.
20+
*/
821

922
const get = <T>(route: string, options: GetOptions<T> = {}, inputConfig?: AxiosRequestConfig) => {
1023
const { callback, errorHandler, body } = options;

0 commit comments

Comments
 (0)