From 76d67c151cf55b30d1f2c8e51def4318c326eeb9 Mon Sep 17 00:00:00 2001 From: TomDijkema Date: Fri, 6 Dec 2024 15:53:00 +0100 Subject: [PATCH] Implement function for retrieving environment variables Implement function for retrieving environment variables --- src/api/annotation/DeleteAnnotation.ts | 5 ++++- src/api/annotation/PatchAnnotation.ts | 5 ++++- .../digitalMedia/GetDigitalMediaMasJobRecords.ts | 5 ++++- src/api/digitalMedia/ScheduleDigitalMediaMas.ts | 5 ++++- .../GetDigitalSpecimenMasJobRecords.ts | 5 ++++- .../digitalSpecimen/ScheduleDigitalSpecimenMas.ts | 5 ++++- src/app/Utilities.ts | 13 +++++++++++-- src/app/config/table/SearchResultsTableConfig.tsx | 5 ++++- src/components/demo/DOITooltipDemo.tsx | 9 ++++++--- .../contentBlock/components/DigitalMediaFrame.tsx | 5 ++++- .../contentBlock/components/DigitalMediaList.tsx | 7 +++++-- .../digitalMedia/components/topBar/TopBar.tsx | 13 ++++++++----- .../components/DigitalSpecimenDigitalMedia.tsx | 4 ++-- .../digitalSpecimen/components/topBar/TopBar.tsx | 9 +++++---- .../annotationSidePanel/AnnotationSidePanel.tsx | 7 +++++-- .../components/AnnotationCard.tsx | 3 ++- .../components/masMenu/MasMenu.tsx | 5 ++++- .../masMenu/components/MasScheduleMenu.tsx | 7 +++++-- src/components/elements/breadCrumbs/BreadCrumbs.tsx | 11 +++++++---- src/components/elements/digitalMedia/ImagePopup.tsx | 5 ++++- .../components/advancedSearch/AdvancedSearch.tsx | 7 +++++-- src/components/search/CompareDigitalSpecimen.tsx | 5 ++++- .../CompareDigitalSpecimenMenu.tsx | 5 ++++- src/components/search/components/idCard/IdCard.tsx | 11 ++++++----- 24 files changed, 115 insertions(+), 46 deletions(-) diff --git a/src/api/annotation/DeleteAnnotation.ts b/src/api/annotation/DeleteAnnotation.ts index 43e0846e..3727be73 100644 --- a/src/api/annotation/DeleteAnnotation.ts +++ b/src/api/annotation/DeleteAnnotation.ts @@ -2,6 +2,9 @@ import axios from 'axios'; import KeycloakService from 'app/Keycloak'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Types */ import { Dict } from 'app/Types'; @@ -22,7 +25,7 @@ const DeleteAnnotation = async ({ annotationId }: { annotationId: string }): Pro try { const result = await axios({ method: 'delete', - url: `annotation/${annotationId.replace(import.meta.env.VITE_HANDLE_URL, '')}`, + url: `annotation/${annotationId.replace(RetrieveEnvVariable('HANDLE_URL'), '')}`, responseType: 'json', headers: { 'Content-type': 'application/json', diff --git a/src/api/annotation/PatchAnnotation.ts b/src/api/annotation/PatchAnnotation.ts index 6d55cd7f..a19f62bb 100644 --- a/src/api/annotation/PatchAnnotation.ts +++ b/src/api/annotation/PatchAnnotation.ts @@ -2,6 +2,9 @@ import axios from 'axios'; import KeycloakService from 'app/Keycloak'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Types */ import { Annotation } from 'app/types/Annotation'; import { AnnotationTemplate, JSONResult } from 'app/Types'; @@ -37,7 +40,7 @@ const PatchAnnotation = async ({ annotationId, updatedAnnotation }: { annotation try { const result = await axios({ method: 'patch', - url: `annotation/${annotationId.replace(import.meta.env.VITE_HANDLE_URL, '')}`, + url: `annotation/${annotationId.replace(RetrieveEnvVariable('HANDLE_URL'), '')}`, responseType: 'json', data: patchAnnotation, headers: { diff --git a/src/api/digitalMedia/GetDigitalMediaMasJobRecords.ts b/src/api/digitalMedia/GetDigitalMediaMasJobRecords.ts index 3389b6c8..7e060048 100644 --- a/src/api/digitalMedia/GetDigitalMediaMasJobRecords.ts +++ b/src/api/digitalMedia/GetDigitalMediaMasJobRecords.ts @@ -1,6 +1,9 @@ /* Import Dependencies */ import axios from 'axios'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Types */ import { MasJobRecord, JSONResultArray, Dict } from 'app/Types'; @@ -23,7 +26,7 @@ const GetDigitalMediaMasJobRecords = async ({ handle, pageSize, pageNumber, stat }; if (handle) { - const endPoint: string = `/digital-media/${handle.replace(import.meta.env.VITE_DOI_URL, '')}/mjr`; + const endPoint: string = `/digital-media/${handle.replace(RetrieveEnvVariable('DOI_URL'), '')}/mjr`; try { const result = await axios({ diff --git a/src/api/digitalMedia/ScheduleDigitalMediaMas.ts b/src/api/digitalMedia/ScheduleDigitalMediaMas.ts index d71ed8b1..433819f2 100644 --- a/src/api/digitalMedia/ScheduleDigitalMediaMas.ts +++ b/src/api/digitalMedia/ScheduleDigitalMediaMas.ts @@ -2,6 +2,9 @@ import axios from 'axios'; import KeycloakService from 'app/Keycloak'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Exceptions */ import { PostException } from 'app/Exceptions'; @@ -42,7 +45,7 @@ const ScheduleDigitalMediaMas = async ({ handle, masList }: { handle: string, ma try { const result = await axios({ method: 'post', - url: `digital-media/${handle.replace(import.meta.env.VITE_DOI_URL, '')}/mas`, + url: `digital-media/${handle.replace(RetrieveEnvVariable('DOI_URL'), '')}/mas`, responseType: 'json', data: masRecord, headers: { diff --git a/src/api/digitalSpecimen/GetDigitalSpecimenMasJobRecords.ts b/src/api/digitalSpecimen/GetDigitalSpecimenMasJobRecords.ts index 4a5596bd..1ece7f43 100644 --- a/src/api/digitalSpecimen/GetDigitalSpecimenMasJobRecords.ts +++ b/src/api/digitalSpecimen/GetDigitalSpecimenMasJobRecords.ts @@ -1,6 +1,9 @@ /* Import Dependencies */ import axios from 'axios'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Types */ import { MasJobRecord, JSONResultArray, Dict } from 'app/Types'; @@ -23,7 +26,7 @@ const GetDigitalSpecimenMasJobRecords = async ({ handle, pageSize, pageNumber, s }; if (handle) { - const endPoint: string = `/digital-specimen/${handle.replace(import.meta.env.VITE_DOI_URL, '')}/mjr`; + const endPoint: string = `/digital-specimen/${handle.replace(RetrieveEnvVariable('DOI_URL'), '')}/mjr`; try { const result = await axios({ diff --git a/src/api/digitalSpecimen/ScheduleDigitalSpecimenMas.ts b/src/api/digitalSpecimen/ScheduleDigitalSpecimenMas.ts index 69d08277..e9821e61 100644 --- a/src/api/digitalSpecimen/ScheduleDigitalSpecimenMas.ts +++ b/src/api/digitalSpecimen/ScheduleDigitalSpecimenMas.ts @@ -2,6 +2,9 @@ import axios from 'axios'; import KeycloakService from 'app/Keycloak'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Exceptions */ import { PostException } from 'app/Exceptions'; @@ -42,7 +45,7 @@ const ScheduleDigitalSpecimenMas = async ({ handle, masList }: { handle: string, try { const result = await axios({ method: 'post', - url: `digital-specimen/${handle.replace(import.meta.env.VITE_DOI_URL, '')}/mas`, + url: `digital-specimen/${handle.replace(RetrieveEnvVariable('DOI_URL'), '')}/mas`, responseType: 'json', data: masRecord, headers: { diff --git a/src/app/Utilities.ts b/src/app/Utilities.ts index 70bdd32c..5836398b 100644 --- a/src/app/Utilities.ts +++ b/src/app/Utilities.ts @@ -1,5 +1,5 @@ /* Import Dependencies */ -import { startCase } from "lodash"; +import { startCase, toUpper } from "lodash"; /** @@ -40,7 +40,16 @@ const MobileCheck = (): boolean => { return isMobile; }; +/** + * Function for retieving an environment variable by name + * @param name The name of the environment variable + */ +const RetrieveEnvVariable = (name: string) => { + return import.meta.env[`VITE_${toUpper(name)}`]; +}; + export { MobileCheck, - MakeReadableString + MakeReadableString, + RetrieveEnvVariable }; \ No newline at end of file diff --git a/src/app/config/table/SearchResultsTableConfig.tsx b/src/app/config/table/SearchResultsTableConfig.tsx index eacbcc9e..b39aa430 100644 --- a/src/app/config/table/SearchResultsTableConfig.tsx +++ b/src/app/config/table/SearchResultsTableConfig.tsx @@ -2,6 +2,9 @@ import { createColumnHelper } from '@tanstack/react-table'; import { useState } from 'react'; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Hooks */ import { useAppSelector } from 'app/Hooks'; @@ -74,7 +77,7 @@ const SearchResultsTableConfig = () => { } }), columnHelper.accessor('DOI', { - cell: info => info.getValue()?.replace(import.meta.env.VITE_DOI_URL as string, ''), + cell: info => info.getValue()?.replace(RetrieveEnvVariable('DOI_URL') as string, ''), meta: { widthInRem: 10, pinned: true diff --git a/src/components/demo/DOITooltipDemo.tsx b/src/components/demo/DOITooltipDemo.tsx index fc0417fc..41397080 100644 --- a/src/components/demo/DOITooltipDemo.tsx +++ b/src/components/demo/DOITooltipDemo.tsx @@ -1,6 +1,9 @@ /* Import Dependencies */ import { useState, useEffect, useRef } from "react"; +/* Import Utilities */ +import { RetrieveEnvVariable } from 'app/Utilities'; + /* Import Styles */ import './DOITooltip.css'; @@ -105,14 +108,14 @@ const DOITooltipDemo = (props: Props) => { if (doi.includes('TEST') || doi.includes('SANDBOX')) { let environment: string = doi.includes('SANDBOX') ? 'sandbox' : 'dev'; - const response = await fetch(`https://${environment}.dissco.tech/handle-manager/api/v1/pids/${doi.replace(import.meta.env.VITE_DOI_URL as string, '')}`); + const response = await fetch(`https://${environment}.dissco.tech/handle-manager/api/v1/pids/${doi.replace(RetrieveEnvVariable('DOI_URL') as string, '')}`); const record = await response.json(); if (record.data) { setRecord(record); } } else if (doi.includes('20.5000.1025')) { - const respone = await fetch(`https://hdl.handle.net/api/handles/${doi.replace(import.meta.env.VITE_DOI_URL as string, '')}`); + const respone = await fetch(`https://hdl.handle.net/api/handles/${doi.replace(RetrieveEnvVariable('DOI_URL') as string, '')}`); const responseRecord = await respone.json(); if (responseRecord.values.length) { @@ -121,7 +124,7 @@ const DOITooltipDemo = (props: Props) => { setRecord(record); } } else if (doi.includes('10.3535')) { - const response = await fetch(`https://doi.org/api/handles/${doi.replace(import.meta.env.VITE_DOI_URL as string, '')}`); + const response = await fetch(`https://doi.org/api/handles/${doi.replace(RetrieveEnvVariable('DOI_URL') as string, '')}`); const responseRecord = await response.json(); if (responseRecord.values.length) { diff --git a/src/components/digitalMedia/components/contentBlock/components/DigitalMediaFrame.tsx b/src/components/digitalMedia/components/contentBlock/components/DigitalMediaFrame.tsx index 1f021b05..9b918671 100644 --- a/src/components/digitalMedia/components/contentBlock/components/DigitalMediaFrame.tsx +++ b/src/components/digitalMedia/components/contentBlock/components/DigitalMediaFrame.tsx @@ -1,6 +1,9 @@ /* Import Dependencies */ import { Annotorious } from "@annotorious/react"; +/* Import Utilities */ +import { RetrieveEnvVariable } from "app/Utilities"; + /* Import Components */ import { ImageViewer } from "components/elements/Elements"; @@ -34,7 +37,7 @@ const DigitalMediaFrame = (props: Props) => { GetDigitalMediaAnnotations({ handle: digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL, '') })} + GetAnnotations={() => GetDigitalMediaAnnotations({ handle: digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL'), '') })} SetAnnotoriousMode={SetAnnotoriousMode} /> diff --git a/src/components/digitalMedia/components/contentBlock/components/DigitalMediaList.tsx b/src/components/digitalMedia/components/contentBlock/components/DigitalMediaList.tsx index a95a9bcf..96cc3a62 100644 --- a/src/components/digitalMedia/components/contentBlock/components/DigitalMediaList.tsx +++ b/src/components/digitalMedia/components/contentBlock/components/DigitalMediaList.tsx @@ -1,6 +1,9 @@ /* Import Dependencies */ import { useState } from "react"; +/* Import Utilities */ +import { RetrieveEnvVariable } from "app/Utilities"; + /* Import Hooks */ import { useNavigate } from "react-router-dom"; import { useFetch } from "app/Hooks"; @@ -44,7 +47,7 @@ const DigitalMediaList = (props: Props) => { params: { handle: digitalMedia["ods:hasEntityRelationships"]?.find( entityRelationship => entityRelationship['dwc:relationshipOfResource'] === 'hasDigitalSpecimen' - )?.["dwc:relatedResourceID"].replace(import.meta.env.VITE_DOI_URL, '') + )?.["dwc:relatedResourceID"].replace(RetrieveEnvVariable('DOI_URL'), '') }, Method: GetDigitalSpecimenDigitalMedia, Handler: (digitalSpecimenDigitalMedia: DigitalMedia[]) => setDigitalSpecimenDigitalMedia(digitalSpecimenDigitalMedia) @@ -60,7 +63,7 @@ const DigitalMediaList = (props: Props) => { diff --git a/src/components/digitalMedia/components/topBar/TopBar.tsx b/src/components/digitalMedia/components/topBar/TopBar.tsx index 13d7e8d4..41ea4e9a 100644 --- a/src/components/digitalMedia/components/topBar/TopBar.tsx +++ b/src/components/digitalMedia/components/topBar/TopBar.tsx @@ -4,6 +4,9 @@ import { useState } from "react"; import { Row, Col } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; +/* Import Utilities */ +import { RetrieveEnvVariable } from "app/Utilities"; + /* Import Hooks */ import { useFetch } from "app/Hooks"; @@ -71,7 +74,7 @@ const TopBar = (props: Props) => { /* OnLoad: fetch digital media versions */ fetch.Fetch({ params: { - handle: digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL, '') + handle: digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL'), '') }, Method: GetDigitalMediaVersions, Handler: (versions: number[]) => { @@ -84,7 +87,7 @@ const TopBar = (props: Props) => { */ const ViewDigitalMediaJSON = () => { window.open(`${window.location.protocol}//${window.location.hostname}${window.location.port ? ':' + window.location.port : ''}` + - `/api/v1/digital-media/${digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL, '')}` + `/api/v1/digital-media/${digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL'), '')}` ); }; @@ -102,7 +105,7 @@ const TopBar = (props: Props) => { const link = document.createElement("a"); link.href = URL.createObjectURL(jsonFile); - link.download = `${digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL as string, '')}_${digitalMedia['ods:version']}.json`; + link.download = `${digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL') as string, '')}_${digitalMedia['ods:version']}.json`; link.click(); }; @@ -113,7 +116,7 @@ const TopBar = (props: Props) => {

- {digitalMedia["dcterms:title"] ?? digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL, '')} + {digitalMedia["dcterms:title"] ?? digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL'), '')}

@@ -136,7 +139,7 @@ const TopBar = (props: Props) => { borderRadius: '999px' }} OnChange={(dropdownItem: DropdownItem) => - navigate(`/dm/${digitalMedia["@id"].replace(import.meta.env.VITE_DOI_URL, '')}/${dropdownItem.value}`)} + navigate(`/dm/${digitalMedia["@id"].replace(RetrieveEnvVariable('DOI_URL'), '')}/${dropdownItem.value}`)} /> } diff --git a/src/components/digitalSpecimen/components/contentBlock/components/DigitalSpecimenDigitalMedia.tsx b/src/components/digitalSpecimen/components/contentBlock/components/DigitalSpecimenDigitalMedia.tsx index 73524744..2a816291 100644 --- a/src/components/digitalSpecimen/components/contentBlock/components/DigitalSpecimenDigitalMedia.tsx +++ b/src/components/digitalSpecimen/components/contentBlock/components/DigitalSpecimenDigitalMedia.tsx @@ -3,7 +3,7 @@ import { Row, Col, Card } from 'react-bootstrap'; import { useNavigate } from 'react-router-dom'; /* Import Utilities */ -import { MakeReadableString } from 'app/Utilities'; +import { MakeReadableString, RetrieveEnvVariable } from 'app/Utilities'; /* Import Types */ import { DigitalMedia } from 'app/types/DigitalMedia'; @@ -44,7 +44,7 @@ const DigitalSpecimenDigitalMedia = (props: Props) => { @@ -240,7 +241,7 @@ const IdCard = () => {