Add API configuration

This commit is contained in:
Eric NGUYEN 2022-10-13 16:27:32 +02:00
parent 7a81bbaec6
commit e97720bc24
9 changed files with 23 additions and 4 deletions

View file

@ -33,8 +33,8 @@ export async function FetchConfiguration(): Promise<IConfiguration> {
}); });
} }
export async function SetContainerList(request: ISetContainerListRequest): Promise<ISetContainerListResponse> { export async function SetContainerList(request: ISetContainerListRequest, configurationUrl?: string): Promise<ISetContainerListResponse> {
const url = import.meta.env.VITE_API_SET_CONTAINER_LIST_URL; const url = configurationUrl ?? import.meta.env.VITE_API_SET_CONTAINER_LIST_URL;
const dataParsed = JSON.stringify(request, GetCircularReplacerToDotnet()); const dataParsed = JSON.stringify(request, GetCircularReplacerToDotnet());
// The test library cannot use the Fetch API // The test library cannot use the Fetch API
// @ts-expect-error // @ts-expect-error

View file

@ -6,6 +6,7 @@ import { LoadState } from './Load';
import { DISABLE_API, GetDefaultEditorState } from '../../../utils/default'; import { DISABLE_API, GetDefaultEditorState } from '../../../utils/default';
export function NewEditor( export function NewEditor(
editorState: IEditorState,
setEditorState: Dispatch<SetStateAction<IEditorState>>, setEditorState: Dispatch<SetStateAction<IEditorState>>,
setLoaded: Dispatch<SetStateAction<boolean>> setLoaded: Dispatch<SetStateAction<boolean>>
): void { ): void {
@ -14,6 +15,10 @@ export function NewEditor(
return; return;
} }
if (editorState.configuration !== undefined) {
return;
}
// Fetch the configuration from the API // Fetch the configuration from the API
FetchConfiguration() FetchConfiguration()
.then((configuration: IConfiguration) => { .then((configuration: IConfiguration) => {

View file

@ -130,7 +130,7 @@ export function App(props: IAppProps): JSX.Element {
> >
<MainMenu <MainMenu
newEditor={() => NewEditor( newEditor={() => NewEditor(
setEditorState, setLoaded editorState, setEditorState, setLoaded
)} )}
loadEditor={(files: FileList | null) => LoadEditor( loadEditor={(files: FileList | null) => LoadEditor(
files, files,

View file

@ -44,7 +44,7 @@ export function GetAction(
}; };
/* eslint-enable */ /* eslint-enable */
SetContainerList(request) SetContainerList(request, configuration.APIConfiguration?.apiSetContainerListUrl)
.then((response: ISetContainerListResponse) => { .then((response: ISetContainerListResponse) => {
HandleSetContainerList( HandleSetContainerList(
action, action,

View file

@ -286,6 +286,7 @@ export function Editor(props: IEditorProps): JSX.Element {
availableContainers={configuration.AvailableContainers} availableContainers={configuration.AvailableContainers}
availableSymbols={configuration.AvailableSymbols} availableSymbols={configuration.AvailableSymbols}
categories={configuration.Categories} categories={configuration.Categories}
apiConfiguration={configuration.APIConfiguration}
selectContainer={(container) => setNewHistory( selectContainer={(container) => setNewHistory(
SelectContainer( SelectContainer(
container, container,

View file

@ -19,6 +19,7 @@ import { IMessage } from '../../Interfaces/IMessage';
import { DISABLE_API } from '../../utils/default'; import { DISABLE_API } from '../../utils/default';
import { UseWorker, UseAsync } from './UseWorker'; import { UseWorker, UseAsync } from './UseWorker';
import { FindContainerById } from '../../utils/itertools'; import { FindContainerById } from '../../utils/itertools';
import { IAPIConfiguration } from '../../Interfaces/IAPIConfiguration';
export interface IUIProps { export interface IUIProps {
selectedContainer: IContainerModel | undefined selectedContainer: IContainerModel | undefined
@ -28,6 +29,7 @@ export interface IUIProps {
availableContainers: IAvailableContainer[] availableContainers: IAvailableContainer[]
availableSymbols: IAvailableSymbol[] availableSymbols: IAvailableSymbol[]
categories: ICategory[] categories: ICategory[]
apiConfiguration: IAPIConfiguration | undefined
selectContainer: (containerId: string) => void selectContainer: (containerId: string) => void
deleteContainer: (containerId: string) => void deleteContainer: (containerId: string) => void
onPropertyChange: (key: string, value: string | number | boolean | number[], type?: PropertyType) => void onPropertyChange: (key: string, value: string | number | boolean | number[], type?: PropertyType) => void
@ -73,11 +75,13 @@ export function UI(props: IUIProps): JSX.Element {
if (window.Worker && !DISABLE_API) { if (window.Worker && !DISABLE_API) {
UseWorker( UseWorker(
props.current, props.current,
props.apiConfiguration?.apiGetFeedbackUrl,
setMessages setMessages
); );
} else if (!DISABLE_API) { } else if (!DISABLE_API) {
UseAsync( UseAsync(
props.current, props.current,
props.apiConfiguration?.apiGetFeedbackUrl,
setMessages setMessages
); );
} }

View file

@ -10,6 +10,7 @@ const myWorker = window.Worker && new Worker('workers/message_worker.js');
export function UseWorker( export function UseWorker(
state: IHistoryState, state: IHistoryState,
configurationUrl: string | undefined,
setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void { setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void {
React.useEffect(() => { React.useEffect(() => {
// use webworker for the stringify to avoid freezing // use webworker for the stringify to avoid freezing
@ -30,6 +31,7 @@ export function UseWorker(
} }
export function UseAsync( export function UseAsync(
state: IHistoryState, state: IHistoryState,
configurationUrl: string | undefined,
setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void { setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void {
React.useEffect(() => { React.useEffect(() => {
const request: IGetFeedbackRequest = { const request: IGetFeedbackRequest = {

View file

@ -0,0 +1,5 @@
export interface IAPIConfiguration {
apiFetchUrl?: string
apiSetContainerListUrl?: string
apiGetFeedbackUrl?: string
}

View file

@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
import { IAPIConfiguration } from './IAPIConfiguration';
import { IAvailableContainer } from './IAvailableContainer'; import { IAvailableContainer } from './IAvailableContainer';
import { IAvailableSymbol } from './IAvailableSymbol'; import { IAvailableSymbol } from './IAvailableSymbol';
import { ICategory } from './ICategory'; import { ICategory } from './ICategory';
@ -11,4 +12,5 @@ export interface IConfiguration {
Categories: ICategory[] Categories: ICategory[]
Patterns: IPattern[] Patterns: IPattern[]
MainContainer: IAvailableContainer MainContainer: IAvailableContainer
APIConfiguration?: IAPIConfiguration
} }