From 5e31fd05138fa76c1d7a3481a500d05e9ae0dd85 Mon Sep 17 00:00:00 2001 From: lorinjameson Date: Tue, 12 Dec 2023 15:36:23 -0500 Subject: [PATCH] Added Dynamic Text tab --- src/components/DynamicText/DynamicText.tsx | 71 ++++++++++++ src/components/DynamicText/index.ts | 2 + src/components/Main/Main.tsx | 88 +++++++------- .../MultiLanguageTextEntry.tsx | 107 ++++++++++++++++++ .../MultiLanguageTextEntry/index.ts | 2 + src/providers/ConfigToolProvider.tsx | 34 ++++++ src/types/index.ts | 14 +++ 7 files changed, 278 insertions(+), 40 deletions(-) create mode 100644 src/components/DynamicText/DynamicText.tsx create mode 100644 src/components/DynamicText/index.ts create mode 100644 src/components/MultiLanguageTextEntry/MultiLanguageTextEntry.tsx create mode 100644 src/components/MultiLanguageTextEntry/index.ts diff --git a/src/components/DynamicText/DynamicText.tsx b/src/components/DynamicText/DynamicText.tsx new file mode 100644 index 0000000..51efa80 --- /dev/null +++ b/src/components/DynamicText/DynamicText.tsx @@ -0,0 +1,71 @@ +import { useState, useContext, useEffect } from 'react'; +import { FlexColumn, FlexRow } from '../layouts'; +import { Button } from '@mui/material'; +import { Translations } from '../../types'; +import { ConfigToolContext } from '../../providers/ConfigToolProvider'; +import MultiLanguageTextEntry from '../MultiLanguageTextEntry'; + +function DynamicText() { + const { configFile, saveDynamicText } = useContext(ConfigToolContext); + const [publicWarning, setPublicWarning] = useState([]); + + useEffect(() => { + if (configFile) { + setPublicWarning(configFile.dynamic_text.public_document_warning); + } + }, [configFile]); + + const handleSave = () => { + saveDynamicText({ public_document_warning: publicWarning }); + }; + + const handleCancel = () => { + if (configFile) { + setPublicWarning(configFile.dynamic_text.public_document_warning); + } + }; + + const saveEnabled = + publicWarning && + publicWarning.length > 0 && + (configFile + ? publicWarning !== configFile?.dynamic_text.public_document_warning + : false); + + return ( + + + + setPublicWarning( + configFile ? configFile.dynamic_text.public_document_warning : [] + ) + } + onChange={(translations: Translations) => + setPublicWarning(translations) + } + /> + + + + + + + ); +} + +export default DynamicText; diff --git a/src/components/DynamicText/index.ts b/src/components/DynamicText/index.ts new file mode 100644 index 0000000..65567fb --- /dev/null +++ b/src/components/DynamicText/index.ts @@ -0,0 +1,2 @@ +import DynamicText from './DynamicText'; +export default DynamicText; diff --git a/src/components/Main/Main.tsx b/src/components/Main/Main.tsx index 599ce98..8ba35fa 100644 --- a/src/components/Main/Main.tsx +++ b/src/components/Main/Main.tsx @@ -1,21 +1,22 @@ -import AppBar from "@mui/material/AppBar"; -import Toolbar from "@mui/material/Toolbar"; -import Typography from "@mui/material/Typography"; -import { Button } from "@mui/material"; -import { FlexColumn, FlexRow } from "../layouts"; -import { Tab, Tabs, Box } from "@mui/material"; -import { useContext, useEffect, useState } from "react"; -import Policies from "../Policies/Policies"; -import Roles from "../Roles"; -import Groups from "../Groups"; -import LoadConfigDialog from "../LoadConfigDialog"; -import { ConfigFile } from "../../types"; -import { ConfigToolContext } from "../../providers/ConfigToolProvider"; -import { copyObject } from "../../utilities"; -import Admin from "../Admin"; -import Branding from "../Branding"; -import { Authentication } from "../Authentication"; -import UpdateVersionDialog from "../UpdateVersionDialog/UpdateVersionDialog"; +import AppBar from '@mui/material/AppBar'; +import Toolbar from '@mui/material/Toolbar'; +import Typography from '@mui/material/Typography'; +import { Button } from '@mui/material'; +import { FlexColumn, FlexRow } from '../layouts'; +import { Tab, Tabs, Box } from '@mui/material'; +import { useContext, useEffect, useState } from 'react'; +import Policies from '../Policies/Policies'; +import Roles from '../Roles'; +import Groups from '../Groups'; +import LoadConfigDialog from '../LoadConfigDialog'; +import { ConfigFile } from '../../types'; +import { ConfigToolContext } from '../../providers/ConfigToolProvider'; +import { copyObject } from '../../utilities'; +import Admin from '../Admin'; +import Branding from '../Branding'; +import DynamicText from '../DynamicText'; +import { Authentication } from '../Authentication'; +import UpdateVersionDialog from '../UpdateVersionDialog/UpdateVersionDialog'; const Main = () => { const { @@ -27,7 +28,7 @@ const Main = () => { policies, configFile, } = useContext(ConfigToolContext); - const [tab, setTab] = useState("policies"); + const [tab, setTab] = useState('policies'); const [loadOpen, setLoadOpen] = useState(false); const [updateOpen, setUpdateOpen] = useState(false); const [save, setSave] = useState(false); @@ -65,27 +66,27 @@ const Main = () => { return ( <> - + - + Annotation Engine Configuration Tool - {`Config File: ${fileName || "No File Chosen"}`} + {`Config File: ${fileName || 'No File Chosen'}`} {!configFile ? ( ) : ( @@ -93,31 +94,38 @@ const Main = () => { - + setTab(value)} value={tab}> - - - - - + + + + + + - {tab === "policies" && } - {tab === "roles" && } - {tab === "groups" && } - {tab === "admin" && } - {tab === "branding" && } - {tab === "authentication" && } + {tab === 'policies' && } + {tab === 'roles' && } + {tab === 'groups' && } + {tab === 'admin' && } + {tab === 'branding' && } + {tab === 'authentication' && } + {tab === 'dynamic_text' && } setLoadOpen(false)} onLoad={handleConfigChosen} + // @ts-ignore policies={policies} /> { + setLanguage(defaultLanguage); + }, [defaultLanguage]); + + const currentText = () => { + if (language.length === 0) { + return ''; + } + + const trans = translations.find((t) => t.language === language); + + return trans ? trans.text : ''; + }; + + const handleTextChange = (language: string, text: string) => { + const copy: Translations = copyObject(translations); + + const idx = copy.findIndex((t) => t.language === language); + + if (idx > -1) { + copy[idx] = { language, text }; + } else { + copy.push({ language, text }); + } + + props.onChange(copy); + }; + + return ( + + + handleTextChange(language, e.target.value as string)} + /> + + + + Language + + + + + ); +} + +export default MultiLanguageTextEntry; diff --git a/src/components/MultiLanguageTextEntry/index.ts b/src/components/MultiLanguageTextEntry/index.ts new file mode 100644 index 0000000..a6fbd6c --- /dev/null +++ b/src/components/MultiLanguageTextEntry/index.ts @@ -0,0 +1,2 @@ +import MultiLanguageTextEntry from './MultiLanguageTextEntry'; +export default MultiLanguageTextEntry; diff --git a/src/providers/ConfigToolProvider.tsx b/src/providers/ConfigToolProvider.tsx index e37712a..7d778a0 100644 --- a/src/providers/ConfigToolProvider.tsx +++ b/src/providers/ConfigToolProvider.tsx @@ -8,6 +8,7 @@ import { RoleDefinition, AuthenticationMethod, AuthenticationType, + DynamicText, } from '../types'; import { copyObject } from '../utilities'; @@ -35,6 +36,7 @@ export interface IConfigToolContext { saveAdmin(_email: string, _groups: string[]): void; saveVersion(_projectName: string, _author: string, _version: string): void; saveBranding(_branding: Branding): void; + saveDynamicText(_dynamicText: DynamicText): void; addAuthMethod(_method: AuthenticationMethod): void; removeAuthMethod(_name: string, _type: AuthenticationType): void; onSaveConfig(): void; @@ -94,6 +96,9 @@ const ConfigToolContext = createContext({ saveBranding(_branding: Branding) { return; }, + saveDynamicText(_dynamicText: DynamicText) { + return; + }, // eslint-disable-next-line @typescript-eslint/no-unused-vars addAuthMethod(_method: AuthenticationMethod) { return; @@ -283,6 +288,20 @@ const ConfigToolProvider = (props: ConfigToolProviderProps) => { } }; + const saveDynamicText = (dynamicText: DynamicText) => { + if (configFile) { + const copy: ConfigFile = copyObject(configFile); + if (!copy.dynamic_text) { + copy.dynamic_text = { + public_document_warning: [], + }; + } + copy.dynamic_text.public_document_warning = + dynamicText.public_document_warning; + setConfigFile(copy); + } + }; + const saveVersion = ( projectName: string, author: string, @@ -388,6 +407,20 @@ const ConfigToolProvider = (props: ConfigToolProviderProps) => { }; } + if (!configFile.dynamic_text) { + configFile.dynamic_text = { + public_document_warning: [], + }; + } + + if (!configFile.supported_languages) { + configFile.supported_languages = ['en', 'de']; + } + + if (!configFile.default_language) { + configFile.default_language = 'en'; + } + setConfigFile(configFile); }; @@ -413,6 +446,7 @@ const ConfigToolProvider = (props: ConfigToolProviderProps) => { saveAdmin, saveVersion, saveBranding, + saveDynamicText, addAuthMethod, removeAuthMethod, }} diff --git a/src/types/index.ts b/src/types/index.ts index 59953a6..90a33b3 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -50,18 +50,32 @@ export type AuthenticationMethod = { domain: string | undefined; }; +export type Translation = { + language: string; + text: string; +}; + +export type Translations = Translation[]; + +export type DynamicText = { + public_document_warning: Translations; +}; + export type ConfigFile = { project_name: string; author: string; version: string; created_at: string; updated_at: string | undefined; + supported_languages: string[]; + default_language: string; policies: PolicyDefinition[]; roles: RoleDefinition[]; org_groups: GroupDefinition[]; project_groups: GroupDefinition[]; layer_groups: GroupDefinition[]; branding: Branding; + dynamic_text: DynamicText; admin: { admin_email: string;