Global

Methods

# abandonModule(id)

Helper function for abandoning a module

Parameters:
Name Type Description
id string

The id of the module to be abandoned

To Do:
  • Further integration of this is needed in later release

View Source components/helpers.js, line 58

# calculateRating(feedback) → {number}

Helper function to calculate the average of the feedback scores provided by students. The function takes an array of feedback scores and returns the average or null if the array is empty or the average cannot be calculated.

Parameters:
Name Type Description
feedback Array.<Object>

The array of feedback objects

rating number

The rating that the student gave the module

feedback string

The feedback/review that the student gave the module

View Source components/helpers.js, line 316

  • module:round_to_precision

Returns the average of the feedback scores provided by students rounded to the nearest 0.5 decimal.

number
Example
const feedback = [
	{"rating": 7, ...},
	{"rating": 8, ...},
	{"rating": 9, ...},
	{"rating": 10, ...}
]

// returns 8
calculateRating(feedback)

# checkForToken() → {Boolean|Error}

Helper function that handles the checking of the JWT token in the local storage. This function DOESN'T verify or decode the token.

View Source components/helpers.js, line 68

Returns true if the token is present in the local storage, false if not. If there is an error, the function returns an Error object.

Boolean | Error

# checkTaken(entered_email) → {Object|Error}

This function is a helper function that check if the email passed in is present in the DB or not. See the Register component for implementation and more details.

Parameters:
Name Type Description
entered_email String

The email to be checked for uniqueness.

View Source utils/checkTaken.js, line 1

The result of the check.

Object | Error
Example
checkTaken('validEmail@gmail.com')
//returns {error: false, message: '', email: 'validEmail@gmail.com'}

checkTaken('invalidEmail@gmail.com')
//returns {error: true, message: 'Account with this email already exists. Please log in', email: 'invalidEmail@gmail.com'}

# convert(timestamp) → {string}

Helper function to format full unix date into a single string that includes the time in a 12-hour format

Parameters:
Name Type Description
timestamp number

The duration of the lesson

Deprecated:
  • Yes

View Source components/helpers.js, line 136

Returns the percentage of the remaining time left in a lesson

string
Example
// returns 2020-05-18, 4:00 AM
convert(1589788800)

# decoder() → {string|null}

Helper function to decode the JWT token present in local storage

View Source components/helpers.js, line 115

Returns the user's ID if the token is valid, null otherwise

string | null

# async getModule(id)

Hook that allows us to fetch module data from the API given a module's ID.

Parameters:
Name Type Description
id Object

The module object

identifier string

The module's ID

Deprecated:
  • Yes

View Source components/helpers.js, line 254

  • module:axios

# getRandomNum(min, max) → {number}

Helper function to generate a random number between two numbers (inclusive), given as arguments

Parameters:
Name Type Description
min number

The minimum number

max number

The maximum number

View Source components/helpers.js, line 289

Returns a random integer between the two given numbers

number
Example
// returns 7
gerRandomNum(0, 10)

# getToken() → {String|Error}

Helper function for getting the JWT token from local storage and adds Bearer in front of it to be used as an Authorization header

View Source components/helpers.js, line 87

Returns the JWT token in a Bearer format

String | Error

# isAuthenticated() → {Boolean}

Helper function to check if a user is authenticated or not. It uses getToken() to get the JWT token from local storage and verify it. If the token is not valid, the function returns false. If the token is present and valid, the function sends a GET request to the /api/users/verify REST route with the token as an Authorization header. If the API responds with a HTTP code 200, the function returns true. If the response is not successful, the function returns false.

Deprecated:
  • Yes

View Source components/helpers.js, line 179

Returns true if the user is authenticated, false otherwise

Boolean

# loader() → {React.ReactElement}

Helper function to display a loading spinner

View Source components/helpers.js, line 216

  • module:react-feather.Loader

Returns the loader component

React.ReactElement

# async profileCheck(token, history, params)

Helper function to check if the user's ID from local storage is the same as the user's ID from the URL params. If not, the user is redirected to their own profile page.

WIP

Parameters:
Name Type Description
token string

The user's JWT token from local storage

history object

React router DOM history object

params object

Object containing the user's ID from the URL params

id string

The user's ID from the URL params

View Source components/helpers.js, line 236

  • module:jsonwebtoken.decode

# progress(dur, rem) → {number}

Helper function to calculate the percentage of the remaining time left in a lesson

Parameters:
Name Type Description
dur number

The duration of the lesson

rem number

The remaining time left of the lesson

Deprecated:
  • Yes

View Source components/helpers.js, line 45

Returns the percentage of the remaining time left in a lesson

number

# rating(arr) → {number}

Helper function to calculate the average score of the feedbacks

Parameters:
Name Type Description
arr Array

The array that we want the average of

Deprecated:
  • Yes

View Source components/helpers.js, line 26

Returns either the average of the array elements or 0 if the array is empty

number

# refreshPage()

Helper function to refresh the page. We use this function to refresh the page after a successful login or signup to trigger a compleat re-render of the page.

View Source components/helpers.js, line 168

# removeToken()

Helper function to log out the user by removing their local storage JWT

View Source components/helpers.js, line 105

# round_to_precision(x, precision) → {number}

Parameters:
Name Type Description
x number

The number to be rounded

precision number

The decimal position to round to

View Source components/helpers.js, line 13

rounded float number

number

Type Definitions

# useState

Properties:
Name Type Description
useState Array

Tuple that is returned from the useState() function.

state *

The read-only variable that represents our current state

resolver function

The update function that can be destructured from the useState() function. This is the only valid way to update our individual states.

View Source App.js, line 41