Settings.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. function camelize(str) {
  4. return str.replace(/-([a-z])/g, function (g) { return g[1].toUpperCase(); });
  5. }
  6. class Settings extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. iframeSrc: ''
  11. };
  12. this.onChange = this.onChange.bind(this);
  13. this.onSubmit = this.onSubmit.bind(this);
  14. }
  15. componentDidMount() {
  16. this.restoreFromLS();
  17. }
  18. restoreFromLS() {
  19. const savedSettings = localStorage.getItem('rpywSettings');
  20. if(savedSettings) {
  21. try {
  22. const settings = JSON.parse(savedSettings);
  23. console.log('restore settings', settings);
  24. this.setState(settings);
  25. }
  26. catch(e) {
  27. console.error('Error while trying to restore settings from localStorage', e);
  28. }
  29. }
  30. }
  31. saveToLS() {
  32. const settingsJson = JSON.stringify(this.state);
  33. console.log('saving', settingsJson);
  34. localStorage.setItem('rpywSettings', settingsJson);
  35. }
  36. onChange(e) {
  37. const { state } = this;
  38. const { name, value } = e.target;
  39. const camelName = camelize(name);
  40. console.log(name, camelName, value);
  41. this.setState(
  42. Object.assign({ ...state }, { [camelName]: value })
  43. );
  44. // console.log(camelize(e.target.name));
  45. }
  46. onSubmit(e) {
  47. e.preventDefault();
  48. const { iframeSrc } = this.state;
  49. console.log('iframe src', iframeSrc);
  50. $('#iframe').attr('src', iframeSrc);
  51. this.saveToLS();
  52. }
  53. render() {
  54. const { iframeSrc } = this.state;
  55. console.log(this.state, iframeSrc);
  56. return (
  57. <div className="row">
  58. <form className="col s12" onSubmit={this.onSubmit}>
  59. <div className="input-field">
  60. <input name="iframe-src" type="text" className="validate" value={iframeSrc} onChange={this.onChange} />
  61. <label htmlFor="iframe-src">Iframe source URL</label>
  62. </div>
  63. <a type="submit" className="waves-effect waves-light btn">Save</a>
  64. </form>
  65. </div>
  66. );
  67. }
  68. }
  69. export default Settings;