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

Save to localstorage #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
123 changes: 112 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,69 @@ import ReactDOM from 'react-dom';
import Slider from './Components/Slider';
import Table from './Components/Table';
import mandalartImage from '../src/assets/images/mandalart_otani.jpeg';
import { useState } from 'react';
import { useEffect, useState } from 'react';

function Portal({ children }) {
return ReactDOM.createPortal(children, document.getElementById('modal'));
}

const MAIN = [...new Array(9)].fill('');
const SUB = [...new Array(9)].map(() => [...new Array(9)].fill(''));

function App() {
const [mainInput, setMainInput] = useState([...new Array(9)].fill(''));
const [mainInput, setMainInput] = useState(MAIN);
const [subInput, setSubInput] = useState(SUB);
const [name, setName] = useState('');
const [isModalOpen, setIsModalOpen] = useState(false);

const setInputValue = (tableKey, index, value) => {
const id = `box-${tableKey}-${index}`;
const element = document.getElementById(id);
if (element) {
element.textContent = value;
}
};

// initialize main/sub input with localStorage
useEffect(() => {
const m = localStorage.getItem('mainInput');
const s = localStorage.getItem('subInput');
if (m) {
setMainInput(JSON.parse(m));
for (var j = 0; j < 9; j++) {
setInputValue('main', j, JSON.parse(m)[j]);
}
}
if (s) {
setSubInput(JSON.parse(s));
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
setInputValue(i + 1, j, JSON.parse(s)[i][j]);
}
}
}
}, []);

const handleReset = () => {
setName('');
setIsModalOpen(false);

// localstoage
localStorage.setItem('mainInput', JSON.stringify(MAIN));
localStorage.setItem('subInput', JSON.stringify(SUB));

// input values
setMainInput(MAIN);
setSubInput(SUB);
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
setInputValue(i + 1, j, '');
}
setInputValue('main', i, '');
}
alert('초기화 되었습니다.');
};
Comment on lines +55 to +73
Copy link
Owner

Choose a reason for hiding this comment

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

I think this is already great, but what do you think about putting one more hurdle before initialization? 🤩
Just in case that button is pressed against by people's intension.
things like..

  const handleReset = () => {
    if (window.confirm('초기화 하시겠습니까?') {
       setName('')
       ...
       alert('초기화 되었습니다.')
    }
  }


const openModal = () => setIsModalOpen(true);
const closeModal = () => setIsModalOpen(false);

Expand All @@ -31,7 +83,16 @@ function App() {
setMainInput((prevState) => {
const newState = [...prevState];
newState.splice(index, 1, value);
localStorage.setItem('mainInput', JSON.stringify(newState));
return newState;
});
}

function addSubInput(mainIndex, index, value) {
setSubInput((prevState) => {
const newState = [...prevState];
newState[mainIndex][index] = value;
localStorage.setItem('subInput', JSON.stringify(newState));
return newState;
});
}
Expand All @@ -45,26 +106,66 @@ function App() {
</Modal>
</Portal>
<Description />
<Nav onClickExample={openModal} name={name} />
<Nav onClickExample={openModal} name={name} reset={handleReset} />
<div className='App' id='capture'>
<Header handleUserName={handleUserName} />
<Slider>
<div className='tables first-row'>
<Table tableKey='1' addMainInput={addMainInput} centerInput={mainInput[0]} />
<Table tableKey='2' addMainInput={addMainInput} centerInput={mainInput[1]} />
<Table tableKey='3' addMainInput={addMainInput} centerInput={mainInput[2]} />
<Table
tableKey='1'
addMainInput={addMainInput}
centerInput={mainInput[0]}
addSubInput={(index, value) => addSubInput(0, index, value)}
/>
<Table
tableKey='2'
addMainInput={addMainInput}
centerInput={mainInput[1]}
addSubInput={(index, value) => addSubInput(1, index, value)}
/>
<Table
tableKey='3'
addMainInput={addMainInput}
centerInput={mainInput[2]}
addSubInput={(index, value) => addSubInput(2, index, value)}
/>
</div>
<div className='tables second-row'>
<Table tableKey='4' addMainInput={addMainInput} centerInput={mainInput[3]} />
<Table
tableKey='4'
addMainInput={addMainInput}
centerInput={mainInput[3]}
addSubInput={(index, value) => addSubInput(3, index, value)}
/>
<MainTable addMainInput={addMainInput} />
<Table tableKey='5' addMainInput={addMainInput} centerInput={mainInput[4]} />
<Table
tableKey='5'
addMainInput={addMainInput}
centerInput={mainInput[4]}
addSubInput={(index, value) => addSubInput(4, index, value)}
/>
<div className='shader left'></div>
<div className='shader right'></div>
</div>
<div className='tables third-row'>
<Table tableKey='6' addMainInput={addMainInput} centerInput={mainInput[5]} />
<Table tableKey='7' addMainInput={addMainInput} centerInput={mainInput[6]} />
<Table tableKey='8' addMainInput={addMainInput} centerInput={mainInput[7]} />
<Table
tableKey='6'
addMainInput={addMainInput}
centerInput={mainInput[5]}
addSubInput={(index, value) => addSubInput(5, index, value)}
/>
<Table
tableKey='7'
addMainInput={addMainInput}
centerInput={mainInput[6]}
addSubInput={(index, value) => addSubInput(6, index, value)}
/>
<Table
tableKey='8'
addMainInput={addMainInput}
centerInput={mainInput[7]}
addSubInput={(index, value) => addSubInput(7, index, value)}
/>
</div>
</Slider>
<Footer />
Expand Down
3 changes: 2 additions & 1 deletion src/Components/Nav/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Button from '../Button';
import NavWrapper from './index.styles';
import { saveToImage } from '../../utils/util';

function Nav({ name, onClickExample }) {
function Nav({ name, onClickExample, reset }) {
return (
<NavWrapper>
<Button customClass='save' onClick={() => saveToImage(name)}>
Expand All @@ -11,6 +11,7 @@ function Nav({ name, onClickExample }) {
<Button customClass='example-modal' onClick={onClickExample}>
예시 보기
</Button>
<Button onClick={reset}>초기화</Button>
</NavWrapper>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Components/Table/MainTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const MainTableWrapper = styled.div`
function MainTable({ addMainInput }) {
return (
<MainTableWrapper>
<Table customClass='main' addMainInput={addMainInput} main={true} />
<Table tableKey={'main'} customClass='main' addMainInput={addMainInput} main={true} />
</MainTableWrapper>
);
}
Expand Down
37 changes: 25 additions & 12 deletions src/Components/Table/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { Cover, TableWrapper } from './index.styles';

function Table({ tableKey, customClass, addMainInput, centerInput, main }) {
function Table({ tableKey, customClass, addMainInput, centerInput, main, addSubInput }) {
const onChangeMainInput = ({ target }) => {
addMainInput(target.dataset.index, target.innerText);
addMainInput(Number(target.dataset.index), target.innerText);
};

const onChangeSubInput = ({ target }) => {
addSubInput(Number(target.dataset.index), target.innerText);
};

const onClickSpan = (/** @type {ClickEvent}*/ e) => {
Expand Down Expand Up @@ -39,35 +43,38 @@ function Table({ tableKey, customClass, addMainInput, centerInput, main }) {
<div className='boxes'>
<div onClick={onClickSpan} className='hide-input first-first'>
<span
id={`box-${tableKey}-0`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={0}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 1' : ''}
onKeyDown={onPreventShiftTab}
/>
></span>
Comment on lines -50 to +55
Copy link
Owner

Choose a reason for hiding this comment

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

I think self-closing tag could be better because there is nothing between them! 😁
how do you think of this?

</div>
<div onClick={onClickSpan} className='hide-input first-second'>
<span
id={`box-${tableKey}-1`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={1}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 2' : ''}
/>
</div>

<div onClick={onClickSpan} className='hide-input first-third'>
<span
id={`box-${tableKey}-2`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={2}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 3' : ''}
/>
Expand All @@ -76,22 +83,24 @@ function Table({ tableKey, customClass, addMainInput, centerInput, main }) {
<div className='boxes'>
<div onClick={onClickSpan} className='hide-input second-first'>
<span
id={`box-${tableKey}-3`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={3}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 4' : ''}
/>
</div>
<div onClick={onClickSpan} className='hide-input second-second'>
<span
id={`box-${tableKey}-8`}
contentEditable={main ? true : false}
onPaste={onPreventStylePaste}
data-index={8}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
readOnly={main ? false : true}
data-placeholder={main ? '최종목표' : `목표 ${tableKey}`}
Expand All @@ -101,11 +110,12 @@ function Table({ tableKey, customClass, addMainInput, centerInput, main }) {
</div>
<div onClick={onClickSpan} className='hide-input second-third'>
<span
id={`box-${tableKey}-4`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={4}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 5' : ''}
/>
Expand All @@ -114,33 +124,36 @@ function Table({ tableKey, customClass, addMainInput, centerInput, main }) {
<div className='boxes'>
<div onClick={onClickSpan} className='hide-input third-first'>
<span
id={`box-${tableKey}-5`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={5}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 6' : ''}
/>
</div>
<div onClick={onClickSpan} className='hide-input third-second'>
<span
id={`box-${tableKey}-6`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={6}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 7' : ''}
/>
</div>
<div onClick={onClickSpan} className='hide-input third-third'>
<span
id={`box-${tableKey}-7`}
contentEditable={true}
onPaste={onPreventStylePaste}
data-index={7}
className={`box ${customClass}`}
onBlur={main ? onChangeMainInput : null}
onBlur={main ? onChangeMainInput : onChangeSubInput}
autoComplete='off'
data-placeholder={main ? '목표 8' : ''}
onKeyDown={onPreventTab}
Expand Down