diff --git a/src/APIFunctions/PermissionRequest.js b/src/APIFunctions/PermissionRequest.js new file mode 100644 index 000000000..85adcc35a --- /dev/null +++ b/src/APIFunctions/PermissionRequest.js @@ -0,0 +1,59 @@ +import { ApiResponse } from './ApiResponses'; +import { BASE_API_URL } from '../Enums'; + +export async function getPermissionRequest(type, token) { + const status = new ApiResponse(); + const url = new URL('/api/PermissionRequest/get', BASE_API_URL); + url.searchParams.append('type', type); + + try { + const res = await fetch(url.toString(), { + headers: { + Authorization: `Bearer ${token}`, + }, + }); + + if (res.ok) { + const data = await res.json(); + status.responseData = data; + } else if (res.status === 404) { + status.responseData = null; + } else { + status.error = true; + } + } catch (err) { + status.responseData = err; + status.error = true; + } + + return status; +} + +export async function createPermissionRequest(type, token) { + const status = new ApiResponse(); + const url = new URL('/api/PermissionRequest/create', BASE_API_URL); + + try { + const res = await fetch(url.toString(), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify({ type }), + }); + + if (res.ok) { + const data = await res.json(); + status.responseData = data; + } else { + status.error = true; + } + } catch (err) { + status.responseData = err; + status.error = true; + } + + return status; +} + diff --git a/src/Pages/LedSign/LedSign.js b/src/Pages/LedSign/LedSign.js index a0cd1dfe7..1b9b1baed 100644 --- a/src/Pages/LedSign/LedSign.js +++ b/src/Pages/LedSign/LedSign.js @@ -1,6 +1,8 @@ import React, { useState, useEffect } from 'react'; import { healthCheck, updateSignText } from '../../APIFunctions/LedSign'; +import { getPermissionRequest, createPermissionRequest } from '../../APIFunctions/PermissionRequest'; import { useSCE } from '../../Components/context/SceContext'; +import { membershipState } from '../../Enums'; import './ledsign.css'; @@ -20,6 +22,9 @@ function LedSign() { const [awaitingSignResponse, setAwaitingSignResponse] = useState(false); const [requestSuccessful, setRequestSuccessful] = useState(); const [stopRequestSuccesful, setStopRequestSuccesful] = useState(); + const [permissionRequest, setPermissionRequest] = useState(null); + const [checkingPermission, setCheckingPermission] = useState(false); + const [requestingPermission, setRequestingPermission] = useState(false); const inputArray = [ { title: 'Sign Text:', @@ -211,11 +216,24 @@ function LedSign() { } setLoading(false); } + + async function checkPermission() { + if (user.accessLevel < membershipState.OFFICER) { + setCheckingPermission(true); + const result = await getPermissionRequest('LED_SIGN', user.token); + if (!result.error && result.responseData) { + setPermissionRequest(result.responseData); + } + setCheckingPermission(false); + } + } + checkSignHealth(); + checkPermission(); // eslint-disable-next-line }, []) - if (loading) { + if (loading || checkingPermission) { return ( @@ -231,6 +249,60 @@ function LedSign() { ); } + async function handleRequestAccess() { + setRequestingPermission(true); + const result = await createPermissionRequest('LED_SIGN', user.token); + if (!result.error) { + setPermissionRequest(result.responseData); + } + setRequestingPermission(false); + } + + function renderPermissionRequestUI() { + if (user.accessLevel >= membershipState.OFFICER) { + return null; + } + + if (checkingPermission) { + return ( +
Checking access...
++ You requested access to the sign on {getFormattedTime(permissionRequest.createdAt)}. +
++ Drop a message in Discord to speed up the process! +
++ You need permission to access the LED sign. +
+ ++ Drop a message in Discord to speed up the process +
+