-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
278 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Translations>([]); | ||
|
||
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 ( | ||
<FlexColumn width={1200} padTop={32}> | ||
<FlexRow fullWidth> | ||
<MultiLanguageTextEntry | ||
languages={configFile ? configFile.supported_languages : []} | ||
defaultLanguage={configFile ? configFile.default_language : ''} | ||
translations={publicWarning} | ||
label='Public Document Wanrning' | ||
multiline={true} | ||
multilineRows={5} | ||
onCancel={() => | ||
setPublicWarning( | ||
configFile ? configFile.dynamic_text.public_document_warning : [] | ||
) | ||
} | ||
onChange={(translations: Translations) => | ||
setPublicWarning(translations) | ||
} | ||
/> | ||
</FlexRow> | ||
<FlexRow fullWidth spaceBetween padTop={32}> | ||
<Button variant='contained' color='secondary' onClick={handleCancel}> | ||
Cancel | ||
</Button> | ||
<Button | ||
variant='contained' | ||
onClick={handleSave} | ||
disabled={!saveEnabled} | ||
> | ||
Save | ||
</Button> | ||
</FlexRow> | ||
</FlexColumn> | ||
); | ||
} | ||
|
||
export default DynamicText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import DynamicText from './DynamicText'; | ||
export default DynamicText; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/components/MultiLanguageTextEntry/MultiLanguageTextEntry.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { FlexCenter, FlexRow } from '../layouts'; | ||
import { | ||
TextField, | ||
Select, | ||
MenuItem, | ||
SelectChangeEvent, | ||
FormControl, | ||
InputLabel, | ||
} from '@mui/material'; | ||
import { copyObject } from '../../utilities'; | ||
import { Translations } from '../../types'; | ||
|
||
interface MultiLanguageTextEntryProps { | ||
languages: string[]; | ||
defaultLanguage: string; | ||
translations: Translations; | ||
label: string; | ||
multiline?: boolean; | ||
multilineRows?: number; | ||
onCancel(): void; | ||
onChange(translations: Translations): void; | ||
} | ||
|
||
function MultiLanguageTextEntry(props: MultiLanguageTextEntryProps) { | ||
const { | ||
languages, | ||
defaultLanguage, | ||
label, | ||
multiline, | ||
translations, | ||
multilineRows, | ||
} = props; | ||
|
||
const [language, setLanguage] = useState(''); | ||
|
||
useEffect(() => { | ||
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 ( | ||
<FlexRow fullWidth padding={5}> | ||
<FlexRow width='80%'> | ||
<TextField | ||
autoFocus | ||
margin='dense' | ||
id='text' | ||
label={label} | ||
type='text' | ||
fullWidth | ||
multiline={multiline} | ||
rows={multiline ? multilineRows || 3 : undefined} | ||
variant='outlined' | ||
value={currentText()} | ||
onChange={(e) => handleTextChange(language, e.target.value as string)} | ||
/> | ||
</FlexRow> | ||
<FlexCenter height='100%' padLeft={3}> | ||
<FormControl sx={{ m: 1, minWidth: 120 }}> | ||
<InputLabel id='language-label'>Language</InputLabel> | ||
<Select | ||
labelId='language-label' | ||
value={language} | ||
label='Language' | ||
onChange={(event: SelectChangeEvent) => | ||
setLanguage(event.target.value as string) | ||
} | ||
> | ||
{languages.map((l) => { | ||
return ( | ||
<MenuItem value={l} key={l}> | ||
{l} | ||
</MenuItem> | ||
); | ||
})} | ||
</Select> | ||
</FormControl> | ||
</FlexCenter> | ||
</FlexRow> | ||
); | ||
} | ||
|
||
export default MultiLanguageTextEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import MultiLanguageTextEntry from './MultiLanguageTextEntry'; | ||
export default MultiLanguageTextEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.