From 3f41f6994743358a457d4c92625c7412de399658 Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Wed, 29 Aug 2018 12:10:53 +0500 Subject: Save options functionality --- src/components/options-form/index.js | 75 +++++++++++++++++++++++++----------- src/containers/options/index.js | 21 ++++++++-- src/redux/preference/actions.js | 4 +- src/redux/preference/reducer.js | 7 +--- src/redux/preference/types.js | 1 - 5 files changed, 74 insertions(+), 34 deletions(-) (limited to 'src') diff --git a/src/components/options-form/index.js b/src/components/options-form/index.js index 393ab93..f83bfe3 100644 --- a/src/components/options-form/index.js +++ b/src/components/options-form/index.js @@ -1,29 +1,60 @@ import React from 'react'; import { Link } from 'react-router-dom'; +import PropTypes from 'prop-types'; import './styles.css'; -const OptionsForm = () => ( -
-
-

Add the Token

-

Generate a token and add it below to avoid hitting the rate limit.

-
-
    -
  • - Go to the Settings Personal Access Tokens of your github profile -
  • -
  • Click Generate New Token. Enter the description and select the scope called public_repo under repo and click Generate Token.
  • -
  • You will be presented with the generated token. Copy the token and add it below
  • -
- -
- - Go Home -
-); +class OptionsForm extends React.Component { + + state = { + token: this.props.options.token + }; + + saveOptions = () => { + this.props.updateOptions({ + token: this.state.token + }); + }; + + onChange = (e) => { + this.setState({ + [e.target.name]: e.target.value + }); + }; + + render() { + return ( +
+
+

Add the Token

+

Generate a token and add it below to avoid hitting the rate limit.

+
+
    +
  • + Go to the Settings Personal Access Tokens of your github profile +
  • +
  • Click Generate New Token. Enter the description and select the scope called public_repo under repo and click Generate Token.
  • +
  • You will be presented with the generated token. Copy the token and add it below
  • +
+ +
+ + Go Home +
+ ); + } +} + +OptionsForm.propTypes = { + updateOptions: PropTypes.func.isRequired, + options: PropTypes.object.isRequired +}; export default OptionsForm; diff --git a/src/containers/options/index.js b/src/containers/options/index.js index d986f6e..588861d 100644 --- a/src/containers/options/index.js +++ b/src/containers/options/index.js @@ -1,9 +1,11 @@ import React from 'react'; +import { connect } from 'react-redux'; +import { Link } from 'react-router-dom'; import './styles.css'; import Logo from '../../components/icons/logo'; -import { Link } from 'react-router-dom'; import OptionsForm from '../../components/options-form'; +import { updateOptions } from '../../redux/preference/actions'; class OptionsContainer extends React.Component { render() { @@ -21,11 +23,24 @@ class OptionsContainer extends React.Component {
- +
); } } -export default OptionsContainer; +const mapStateToProps = (store) => { + return { + preference: store.preference + }; +}; + +const mapDispatchToProps = { + updateOptions +}; + +export default connect(mapStateToProps, mapDispatchToProps)(OptionsContainer); diff --git a/src/redux/preference/actions.js b/src/redux/preference/actions.js index 57ab13a..54b8259 100644 --- a/src/redux/preference/actions.js +++ b/src/redux/preference/actions.js @@ -1,10 +1,10 @@ import { UPDATE_DATE_TYPE, UPDATE_LANGUAGE, UPDATE_OPTIONS, UPDATE_VIEW_TYPE } from './types'; -export const updateOptions = function (setting) { +export const updateOptions = function (options) { return dispatch => { dispatch({ type: UPDATE_OPTIONS, - payload: setting, + payload: options, }); }; }; diff --git a/src/redux/preference/reducer.js b/src/redux/preference/reducer.js index e634104..089d828 100644 --- a/src/redux/preference/reducer.js +++ b/src/redux/preference/reducer.js @@ -1,4 +1,4 @@ -import { RESET_OPTIONS, UPDATE_DATE_TYPE, UPDATE_LANGUAGE, UPDATE_OPTIONS, UPDATE_VIEW_TYPE } from './types'; +import { UPDATE_DATE_TYPE, UPDATE_LANGUAGE, UPDATE_OPTIONS, UPDATE_VIEW_TYPE } from './types'; const initialState = { viewType: 'grid', @@ -16,11 +16,6 @@ export default function reducer(state = initialState, action) { ...state, options: action.payload }; - case RESET_OPTIONS: - return { - ...state, - options: initialState.options - }; case UPDATE_DATE_TYPE: return { ...state, diff --git a/src/redux/preference/types.js b/src/redux/preference/types.js index e5e52a4..1681e8f 100644 --- a/src/redux/preference/types.js +++ b/src/redux/preference/types.js @@ -1,5 +1,4 @@ export const UPDATE_OPTIONS = 'UPDATE_OPTIONS'; -export const RESET_OPTIONS = 'RESET_OPTIONS'; export const UPDATE_VIEW_TYPE = 'UPDATE_VIEW_TYPE'; export const UPDATE_DATE_TYPE = 'UPDATE_DATE_TYPE'; export const UPDATE_LANGUAGE = 'UPDATE_LANGUAGE'; -- cgit v1.3