Skip to content
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

Ka 2020 04 translate org fields #1799

Merged
merged 3 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion euth/dashboard/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def untranslated(self):
"""
return [field for field in self if '__' not in field.html_name]

def prefiled_languages(self):
def prefilled_languages(self):
"""
Return languages tabs that need to be displayed.
"""
Expand Down
51 changes: 34 additions & 17 deletions euth/dashboard/static/language_switch/react_language_switch.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var PropTypes = require('prop-types')
var React = require('react')
var ReactDOM = require('react-dom')
var $ = require('jquery')

class LanguageSwitch extends React.Component {
constructor (props) {
Expand All @@ -11,33 +10,32 @@ class LanguageSwitch extends React.Component {
}
}

switchLanguage (e) {
addLanguage (e) {
var languageCode = e.target.textContent
var index = this.state.activeLanguages.indexOf(languageCode)
var newActiveLanguages = this.state.activeLanguages.concat([])
if (index === -1) {
// adding language
newActiveLanguages.push(languageCode)
} else {
newActiveLanguages.splice(index, 1)
}

this.setState({
activeLanguages: newActiveLanguages
}, function () {
var $checkbox = $('#' + languageCode + '_language-switch')
// language was active
if (!$checkbox.is(':checked')) {
$(this.refs.checkboxList).find(':checked').first().next('a').tab('show')
} else {
$checkbox.next('a').tab('show')
}
})
}

componentDidMount () {
$(this.refs.toggleButton).dropdown()
$(this.refs.checkboxList).find('.a').tab()
removeLanguage (e) {
var languageCode = e.target.textContent
var index = this.state.activeLanguages.indexOf(languageCode)
var newActiveLanguages = this.state.activeLanguages.concat([])
if (index !== -1) {
// removing language
newActiveLanguages.splice(index, 1)
}

this.setState({
activeLanguages: newActiveLanguages
})
}

render () {
Expand Down Expand Up @@ -68,10 +66,29 @@ class LanguageSwitch extends React.Component {
</button>
<ul className="dropdown-menu">
{
this.props.languages.map(languageCode => {
this.props.languages.map((languageCode, i) => {
return (
<span key={languageCode}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phillimorland I am a bit unhappy with returning an empty span for the languages that shouldn't be in the list, but couldn't think of a better way. (Apart from also having a list of the inactive languages in the state, but that would be a bit annoying to always update two lists, then.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is ok for now to be honest

{this.state.activeLanguages.indexOf(languageCode) === -1 &&
<li key={languageCode}>
<button type="button" onClick={this.addLanguage.bind(this)}>{languageCode}</button>
</li>}
</span>
)
})
}
</ul>
</div>
<div className="dropdown">
<button className="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown" ref="toggleButton">
<i className="fa fa-minus" />
</button>
<ul className="dropdown-menu">
{
this.state.activeLanguages.map(languageCode => {
return (
<li key={languageCode}>
<button type="button" onClick={this.switchLanguage.bind(this)}>{languageCode}</button>
<button type="button" onClick={this.removeLanguage.bind(this)}>{languageCode}</button>
</li>
)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ <h1 class="dashboard-content-heading">{% trans "Edit your organisation details"
id="language-switch-list"
data-euth-widget="language-switch"
data-languages="{{ form.languages|join:' ' }}"
data-active-languages="{{ form.prefiled_languages|join:' ' }}"
data-active-languages="{{ form.prefilled_languages|join:' ' }}"
></div>

{% for lang_code, fields in form.translated %}
Expand Down
2 changes: 1 addition & 1 deletion tests/dashboard/test_dashboard_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def test_dashboard_update_organisation(client, organisation):

response = client.get(url)
form = response.context_data['form']
assert form.prefiled_languages() == ['en']
assert form.prefilled_languages() == ['en']
assert len(form.untranslated()) == 9
assert len(form.translated()) == 10
assert form.translated()[0][0] == 'en'
Expand Down