-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for multiple languages in project edit view
- Loading branch information
1 parent
c2c0944
commit 6446c5c
Showing
5 changed files
with
175 additions
and
59 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
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,100 @@ | ||
import React, { useState, useLayoutEffect, useCallback, useContext } from 'react'; | ||
import { Button } from '../../components/button'; | ||
import { htmlFromMarkdown } from '../../utils/htmlFromMarkdown'; | ||
import { StateContext, styleClasses } from '../../views/projectEdit'; | ||
|
||
export const InputLocale = props => { | ||
const { projectInfo, setProjectInfo } = useContext(StateContext); | ||
const [language, setLanguage] = useState(null); | ||
const [value, setValue] = useState(''); | ||
const [preview, setPreview] = useState(null); | ||
|
||
const locales = projectInfo.projectInfoLocales; | ||
const handleChange = e => { | ||
let selected = locales.filter(f => f.locale === language); | ||
let data = null; | ||
if (selected.length === 0) { | ||
data = { locale: language, [e.target.name]: e.target.value }; | ||
} else { | ||
data = selected[0]; | ||
data[e.target.name] = e.target.value; | ||
} | ||
// create element with new locale. | ||
let newLocales = locales.filter(f => f.locale !== language); | ||
newLocales.push(data); | ||
setProjectInfo({ ...projectInfo, projectInfoLocales: newLocales }); | ||
}; | ||
|
||
const getValue = useCallback(() => { | ||
const data = locales.filter(l => l.locale === language); | ||
if (data.length > 0) { | ||
return data[0][props.name]; | ||
} else { | ||
return ''; | ||
} | ||
}, [language, locales, props.name]); | ||
|
||
useLayoutEffect(() => { | ||
if (language === null) { | ||
if (projectInfo.defaultLocale) { | ||
setLanguage(projectInfo.defaultLocale); | ||
} | ||
} | ||
}, [projectInfo, language]); | ||
|
||
useLayoutEffect(() => { | ||
const fieldValue = getValue(); | ||
setValue(fieldValue); | ||
}, [getValue]); | ||
|
||
const renderMarkdown = value => { | ||
const html = htmlFromMarkdown(value); | ||
setPreview(html); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<ul className="list mb4 pa0 w-100 flex flex-wrap ttu"> | ||
{props.languages === null | ||
? null | ||
: props.languages.map(l => ( | ||
<li | ||
onClick={() => setLanguage(l.code)} | ||
className={ | ||
(l.code !== language ? 'bg-white blue-dark' : 'bg-navy white') + | ||
' ph2 mb2 pv1 f7 mr2 pointer' | ||
} | ||
> | ||
{l.code} | ||
</li> | ||
))} | ||
</ul> | ||
{props.children} | ||
{props.type === 'text' ? ( | ||
<input | ||
type="text" | ||
className={styleClasses.inputClass} | ||
name={props.name} | ||
value={value} | ||
onChange={handleChange} | ||
/> | ||
) : ( | ||
<textarea | ||
className={styleClasses.inputClass} | ||
rows={styleClasses.numRows} | ||
type="text" | ||
name={props.name} | ||
value={value} | ||
onChange={handleChange} | ||
></textarea> | ||
)} | ||
{props.preview !== false ? ( | ||
<Button onClick={() => renderMarkdown(value)} className="bg-navy white mr1 f6 pv2 ttu"> | ||
preview | ||
</Button> | ||
) : null} | ||
|
||
{preview ? <div dangerouslySetInnerHTML={preview} /> : null} | ||
</div> | ||
); | ||
}; |
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
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
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