//Lib import { toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import classes from '../UserSettings.module.css'; import { useEffect, useState } from 'react'; import { useForm } from 'react-hook-form'; import { SpinnerDotted } from 'spinners-react'; import { v4 as uuidv4 } from 'uuid'; import timestampConverter from '../../../helpers/functions/timestampConverter'; import { IconTrash } from '@tabler/icons-react'; //Components import Error from '../../../Components/UI/Error/Error'; import CopyButton from '../../../Components/UI/CopyButton/CopyButton'; import Info from '../../../Components/UI/Info/Info'; export default function Integrations() { //Var const toastOptions = { position: 'top-right', autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, }; const { register, handleSubmit, reset, formState: { errors, isSubmitting, isValid }, } = useForm({ mode: 'onChange', defaultValues: { authorization: 'read' } }); ////State const [isLoading, setIsLoading] = useState(false); const [tokenList, setTokenList] = useState([]); const [error, setError] = useState(); const [lastGeneratedToken, setLastGeneratedToken] = useState(); const [deletingToken, setDeletingToken] = useState(null); const fetchTokenList = async () => { try { const response = await fetch('/api/account/token-manager', { method: 'GET', headers: { 'Content-type': 'application/json', }, }); const tokensArray = await response.json(); setTokenList(tokensArray); } catch (error) { console.log('Fetching token list failed.'); } }; ////LifeCycle useEffect(() => { fetchTokenList(); }, []); //Form submit Handler for ADD a repo const formSubmitHandler = async (data) => { //Remove old error setError(); //Loading button on submit to avoid multiple send. setIsLoading(true); //Generate a UUIDv4 const token = uuidv4(); setLastGeneratedToken({ name: data.tokenName, value: token }); // Post API to send the new token integration try { const response = await fetch('/api/account/token-manager', { method: 'POST', headers: { 'Content-type': 'application/json', }, body: JSON.stringify({ name: data.tokenName, token: token, creation: Math.floor(Date.now() / 1000), expirition: null, permissions: { read: true, write: data.authorization === 'write' ? true : false, }, }), }); const result = await response.json(); if (!response.ok) { setIsLoading(false); reset(); toast.error(result.message, toastOptions); setTimeout(() => setError(), 4000); } else { reset(); fetchTokenList(); setIsLoading(false); toast.success('🔑 Token generated !', toastOptions); } } catch (error) { reset(); setIsLoading(false); toast.error("Can't generate your token. Contact your administrator.", toastOptions); setTimeout(() => setError(), 4000); } }; //Delete token const deleteTokenHandler = async (tokenName) => { try { const response = await fetch('/api/account/token-manager', { method: 'DELETE', headers: { 'Content-type': 'application/json', }, body: JSON.stringify({ name: tokenName, }), }); const result = await response.json(); if (!response.ok) { toast.error(result.message, toastOptions); setTimeout(() => setError(), 4000); } else { fetchTokenList(); toast.success('🗑️ Token deleted !', toastOptions); } } catch (error) { setIsLoading(false); toast.error("Can't delete your token. Contact your administrator.", toastOptions); setTimeout(() => setError(), 4000); } finally { setDeletingToken(null); } }; return ( <>

Generate token

{errors.tokenName && errors.tokenName.type === 'maxLength' && ( 25 characters max. )} {errors.tokenName && errors.tokenName.type === 'pattern' && ( Only alphanumeric characters, dashes, and underscores are allowed (no spaces). )} {error && }
{tokenList && tokenList.length > 0 && (

API Tokens

{tokenList .slice() .sort((a, b) => b.creation - a.creation) .map((token, index) => (
{token.name}

Created at: {timestampConverter(token.creation)}

Permission:

{token.permissions.write ? 'Write' : 'Read'}

{lastGeneratedToken && lastGeneratedToken.name === token.name && ( <>

Token: {lastGeneratedToken.value}

This token will not be shown again. Please save it. )} {deletingToken && deletingToken.name === token.name && (
)}
setDeletingToken(token)} />
))}
)} ); }