Make LanguageChooser isolated in funcitonality

and only set cookie client side, rather than using the post request
This commit is contained in:
Matthew Taylor 2015-10-16 11:01:40 -04:00
parent d307b21d3d
commit 567067ce88
3 changed files with 19 additions and 49 deletions

View file

@ -6,7 +6,6 @@
"/accounts/", "/accounts/",
"/csrf_token/", "/csrf_token/",
"/get_image/", "/get_image/",
"/i18n/",
"/session/", "/session/",
"/site-api/" "/site-api/"
] ]

View file

@ -1,43 +1,11 @@
var React = require('react'); var React = require('react');
var log = require('../../log.js');
var Api = require('../../mixins/api.jsx');
var CookieMixinFactory = require('../../mixins/cookieMixinFactory.jsx');
var LanguageChooser = require('../languagechooser/languagechooser.jsx'); var LanguageChooser = require('../languagechooser/languagechooser.jsx');
require('./footer.scss'); require('./footer.scss');
var Footer = React.createClass({ var Footer = React.createClass({
type: 'Footer', type: 'Footer',
mixins: [
Api,
// Provides useScratchlanguage
CookieMixinFactory('scratchlanguage', '/site-api/i18n/set-preferred-language/')
],
getInitialState: function () {
return {
language: 'en'
};
},
componentDidMount: function () {
this.updateLanguage();
},
updateLanguage: function () {
this.useScratchlanguage(function (err, language) {
if (err) return log.error(err);
this.setState({language: language});
}.bind(this));
},
handleSetLanguage: function (e) {
if (e) e.preventDefault();
this.api({
host: '',
uri: '/i18n/setlang/'
}, function (err, body) {
if (body) this.updateLanguage();
}.bind(this));
},
render: function () { render: function () {
return ( return (
<div className="inner"> <div className="inner">
@ -86,7 +54,7 @@ var Footer = React.createClass({
</dl> </dl>
</div> </div>
<LanguageChooser onSetLanguage={this.handleSetLanguage} choice={this.state.language} /> <LanguageChooser />
<div className="copyright"> <div className="copyright">
<p>Scratch is a project of the Lifelong Kindergarten Group at the MIT Media Lab</p> <p>Scratch is a project of the Lifelong Kindergarten Group at the MIT Media Lab</p>

View file

@ -1,39 +1,42 @@
var classNames = require('classnames'); var classNames = require('classnames');
var React = require('react'); var React = require('react');
var jar = require('../../lib/jar.js');
var languages = require('../../../languages.json');
var Select = require('../forms/select.jsx'); var Select = require('../forms/select.jsx');
require('./languagechooser.scss'); require('./languagechooser.scss');
var LanguageChooser = React.createClass({ var LanguageChooser = React.createClass({
type: 'LanguageChooser', type: 'LanguageChooser',
propTypes: { getInitialState: function () {
choice: React.PropTypes.string, return {
languages: React.PropTypes.object choice: window._locale
};
}, },
getDefaultProps: function () { getDefaultProps: function () {
return { return {
choice: 'en', languages: languages
languages: require('./languages.json')
}; };
}, },
onSetLanguage: function (e) {
e.preventDefault();
jar.set('scratchlanguage', e.target.value);
document.location.reload(true);
},
render: function () { render: function () {
var classes = classNames( var classes = classNames(
'language-chooser', 'language-chooser',
this.props.className this.props.className
); );
return ( return (
<form <form ref="languageForm" className={classes}>
ref="languageForm" <Select name="language" defaultValue={this.state.choice} onChange={this.onSetLanguage}>
method="post"
action="/i18n/setlang/"
{... this.props}
className={classes}
onSubmit={this.props.onSetLanguage}>
<Select name="language" value={this.props.choice} onChange={this.props.onSetLanguage}>
{Object.keys(this.props.languages).map(function (value) { {Object.keys(this.props.languages).map(function (value) {
return <option value={value} key={value}>{this.props.languages[value]}</option>; return <option value={value} key={value}>
{this.props.languages[value]}
</option>;
}.bind(this))} }.bind(this))}
</Select> </Select>
</form> </form>