{"size": 2201, "ext": "js", "max_stars_repo_path": "src/App.js", "max_stars_repo_name": "klueless-react-samples/r05-storage", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/App.js", "max_issues_repo_name": "klueless-react-samples/r05-storage", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/App.js", "max_forks_repo_name": "klueless-react-samples/r05-storage", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.2023809524, "max_line_length": 84, "alphanum_fraction": 0.6365288505}
import React, { useState, useEffect } from 'react'
import { getItem, setItem } from './service/storage'

import logo from './logo.svg';
import './App.css';

import { Button, ButtonLabelInput } from './components';

function App() {
  const [count, setCount] = useState(0);
  const [name, setName] = useState('');
  const [labelReset, setLabelReset] = useState('Reset');
  const [labelUp, setLabelUp] = useState('Up');
  const [labelDown, setLabelDown] = useState('Down');
  const [labelMax, setLabelMax] = useState('Max');

  useEffect(() => {
    console.log('count changed changed updated');

    let savedValue = parseInt(localStorage.getItem('count'));

    setCount(savedValue)
    setName(getItem('counter_name', ''));
  }, []);

  function setAndStoreCount(value) {
    setCount(value)
    localStorage.setItem('count', value);
  }

  function onResetButtonClick() {
    setAndStoreCount(0);
  }

  function onUpButtonClick() {
    setAndStoreCount(count + 1);
  }

  function onDownButtonClick() {
    setAndStoreCount(count - 1);
  }

  function onMaxButtonClick() {
    setAndStoreCount(99);
  }

  function onNameChange(e) {
    setName(e.target.value);
    setItem('counter_name', e.target.value);
  }

  return (
    <div className="App">
      <header className="App-header">
        <h1>Storage</h1>
        <img src={logo} className="App-logo" alt="logo" />
      </header>

      <section>
        <input value={name} onChange={onNameChange} placeholder="Name of Counter" />
        <br />

        {name}: { count }
        <br />

        <Button title={labelReset} click={onResetButtonClick} />
        <Button title={labelUp} click={onUpButtonClick} variant='green' />
        <Button title={labelDown} click={onDownButtonClick} variant='red' />
        <Button title={labelMax} click={onMaxButtonClick} variant='blue' />

        <hr />

        <ButtonLabelInput buttonName='reset' set={setLabelReset} />&nbsp;
        <ButtonLabelInput buttonName='up' set={setLabelUp} />&nbsp;
        <ButtonLabelInput buttonName='down' set={setLabelDown} />&nbsp;
        <ButtonLabelInput buttonName='max' set={setLabelMax} />&nbsp;

      </section>
    </div>
  );
}

export default App;
