-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve swap lang button for language and text swapping #482
base: master
Are you sure you want to change the base?
Changes from 4 commits
4bd86c8
86a85cd
dd4e28b
77e73cf
9af1056
6b797ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ import useLocalStorage from '../../util/useLocalStorage'; | |
import { useLocalization } from '../../util/localization'; | ||
|
||
const recentLangsCount = 3; | ||
const textUrlParam = 'q'; | ||
|
||
const defaultSrcLang = (pairs: Pairs): string => { | ||
const browserLangs = window.navigator.languages; | ||
|
@@ -248,6 +249,10 @@ const Translator = ({ mode: initialMode }: { mode?: Mode }): React.ReactElement | |
const config = React.useContext(ConfigContext); | ||
|
||
const [loading, setLoading] = React.useState(false); | ||
const [srcText, setSrcText] = useLocalStorage('srcText', '', { | ||
overrideValue: getUrlParam(history.location.search, textUrlParam), | ||
}); | ||
const [tgtText, setTgtText] = React.useState(''); | ||
|
||
const [markUnknown, setMarkUnknown] = useLocalStorage('markUnknown', false); | ||
const [instantTranslation, setInstantTranslation] = useLocalStorage('instantTranslation', true); | ||
|
@@ -272,6 +277,14 @@ const Translator = ({ mode: initialMode }: { mode?: Mode }): React.ReactElement | |
|
||
const onTranslate = React.useCallback(() => window.dispatchEvent(new Event(TranslateEvent)), []); | ||
|
||
const swapLangs = React.useCallback(() => { | ||
setSrcText(tgtText); | ||
}, [setSrcText, tgtText]); | ||
|
||
const swapLangText = () => { | ||
swapLangs(); | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function doesn't have any purpose now? I think we can just pass |
||
|
||
return ( | ||
<Form | ||
aria-label={t('Translate')} | ||
|
@@ -307,12 +320,24 @@ const Translator = ({ mode: initialMode }: { mode?: Mode }): React.ReactElement | |
tgtLang, | ||
detectedLang, | ||
loading, | ||
swapLangText, | ||
}} | ||
/> | ||
{(mode === Mode.Text || !mode) && ( | ||
<> | ||
<TextTranslationForm | ||
{...{ instantTranslation, markUnknown, setLoading, srcLang, tgtLang, pairPrefs }} | ||
{...{ | ||
instantTranslation, | ||
markUnknown, | ||
setLoading, | ||
srcLang, | ||
tgtLang, | ||
pairPrefs, | ||
srcText, | ||
tgtText, | ||
setSrcText, | ||
setTgtText, | ||
}} | ||
/> | ||
<Row className="mt-2 mb-3"> | ||
<Col className="d-flex d-sm-block flex-wrap translation-modes" md="6" xs="12"> | ||
|
@@ -377,4 +402,5 @@ const Translator = ({ mode: initialMode }: { mode?: Mode }): React.ReactElement | |
); | ||
}; | ||
|
||
export { textUrlParam }; | ||
export default Translator; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,9 @@ import userEvent from '@testing-library/user-event'; | |
|
||
import { DetectCompleteEvent, DetectEvent, TranslateEvent } from '..'; | ||
import TextTranslationForm, { Props } from '../TextTranslationForm'; | ||
import { getUrlParam } from '../../../util/url'; | ||
import { textUrlParam } from '../Translator'; | ||
import useLocalStorage from '../../../util/useLocalStorage'; | ||
|
||
const renderTextTranslationForm = ( | ||
props_: Partial<Props> = {}, | ||
|
@@ -20,13 +23,34 @@ const renderTextTranslationForm = ( | |
srcLang: 'eng', | ||
tgtLang: 'spa', | ||
pairPrefs: {}, | ||
srcText: '', | ||
tgtText: '', | ||
setSrcText: jest.fn(), | ||
setTgtText: jest.fn(), | ||
setLoading: jest.fn(), | ||
...props_, | ||
}; | ||
|
||
const Wrapper = () => { | ||
const [srcText, setSrcText] = useLocalStorage('srcText', '', { | ||
overrideValue: getUrlParam(history.location.search, textUrlParam), | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to duplicate this code in a test...? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I'm currently stuck here, and I'm not entirely sure why I went with this approach. It seems to be the only way the tests are passing, and I need your help on how to improve this setup. The functionality of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem is that the tests need to be relocated now. Moving the logic that pulls There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I realized that my knowledge of React testing might be a bit lacking. I plan to learn jest, and then I'll resume working on the pull request at a later point in time. I'll focus on other issues for now i.e. #405 |
||
const [tgtText, setTgtText] = React.useState(''); | ||
|
||
return ( | ||
<TextTranslationForm | ||
{...props} | ||
setSrcText={setSrcText} | ||
setTgtText={setTgtText} | ||
srcText={srcText} | ||
tgtText={tgtText} | ||
/> | ||
); | ||
}; | ||
|
||
render( | ||
<Router history={history}> | ||
<TextTranslationForm {...props} /> | ||
<Wrapper /> | ||
</Router>, | ||
); | ||
|
||
|
@@ -149,16 +173,31 @@ describe('translation', () => { | |
srcLang: 'eng', | ||
tgtLang: 'spa', | ||
pairPrefs: {}, | ||
srcText: '', | ||
tgtText: '', | ||
setSrcText: jest.fn(), | ||
setTgtText: jest.fn(), | ||
setLoading: jest.fn(), | ||
}; | ||
|
||
const Container = () => { | ||
const [srcLang, setSrcLang] = React.useState('eng'); | ||
const [srcText, setSrcText] = useLocalStorage('srcText', '', { | ||
overrideValue: getUrlParam(history.location.search, textUrlParam), | ||
}); | ||
const [tgtText, setTgtText] = React.useState(''); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here as above. |
||
return ( | ||
<> | ||
<button onClick={() => setSrcLang('cat')}>ChangeSrcLang</button> | ||
<Router history={history}> | ||
<TextTranslationForm {...props} srcLang={srcLang} /> | ||
<TextTranslationForm | ||
{...props} | ||
setSrcText={setSrcText} | ||
setTgtText={setTgtText} | ||
srcLang={srcLang} | ||
srcText={srcText} | ||
tgtText={tgtText} | ||
/> | ||
</Router> | ||
</> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this line ever get invoked by tests?