From 9839d0563c817f53a25e47f43db91af67900eda5 Mon Sep 17 00:00:00 2001 From: DDuckyee Date: Wed, 7 Jun 2023 05:57:36 +0900 Subject: [PATCH 1/2] Basic CRUD --- toy/package-lock.json | 2 +- toy/src/App.js | 185 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 167 insertions(+), 20 deletions(-) diff --git a/toy/package-lock.json b/toy/package-lock.json index 129470b..f296c75 100644 --- a/toy/package-lock.json +++ b/toy/package-lock.json @@ -6,7 +6,7 @@ "packages": { "": { "name": "toy", - "version": "0.1.0", + "version": "0.1.0", "dependencies": { "@testing-library/jest-dom": "^5.16.5", "@testing-library/react": "^13.4.0", diff --git a/toy/src/App.js b/toy/src/App.js index 3784575..a9797dc 100644 --- a/toy/src/App.js +++ b/toy/src/App.js @@ -1,25 +1,172 @@ -import logo from './logo.svg'; import './App.css'; +import { useState } from 'react'; + +function Header(props) { + return
+

{ + event.preventDefault(); + props.onChangeMode(); + }}>{props.title}

+
+} + +function Nav(props) { + const lis = [] + for(let i = 0; i < props.topics.length; i++){ + let t = props.topics[i]; + lis.push(
  • + { + event.preventDefault(); + props.onChangeMode(Number(event.target.id)); + }}>{t.title} +
  • ); + } + return +} + +function Article(props) { + return
    +

    {props.title}

    + {props.body} +
    +} + +function Create(props) { + return
    +

    Create

    +
    { + event.preventDefault(); + const title = event.target.title.value; + const body = event.target.body.value; + props.onCreate(title, body); + }}> +

    +

    +

    +
    +
    +} + +function Update(props) { + const [title, setTitle] = useState(props.title); + const [body, setBody] = useState(props.body); + return
    +

    Update

    +
    { + event.preventDefault(); + const title = event.target.title.value; + const body = event.target.body.value; + props.onUpdate(title, body); + }}> +

    { + setTitle(event.target.value); + }}/>

    +

    +

    +
    +
    +} function App() { - return ( -
    -
    - logo -

    - Edit src/App.js and save to reload. -

    - - Learn React - -
    -
    - ); + // const _mode = useState('WELCOME'); + // const mode = _mode[0]; + // const setMode = _mode[1]; + const [mode, setMode] = useState('WELCOME'); + const [id, setId] = useState('null'); + const [nextId, setNextId] = useState(4); + const [topics, setTopics] = useState([ + {id:1, title: 'html', body: 'html is ...'}, + {id:2, title: 'css', body: 'css is ...'}, + {id:3, title: 'javascript', body: 'javascript is ...'} + ]); + let content = null; + let contextControl = null; + + if(mode === 'WELCOME'){ + content =
    + } else if(mode === 'READ'){ + let title, body = null; + for(let i = 0; i < topics.length; i++) { + if(topics[i].id === id) { + title = topics[i].title; + body = topics[i].body + } + } + content =
    + contextControl = <> +
  • { + event.preventDefault(); + setMode('UPDATE'); + }}>Update
  • +
  • { + const newTopics = [] + for(let i = 0; i < topics.length; i++) { + if(topics[i].id !== id) { + newTopics.push(topics[i]) + } + } + setTopics(newTopics); + setMode('WELCOME'); + }}/>
  • + + } else if(mode === 'CREATE'){ + content = { + const newTopic = {id:nextId, title:_title, body:_body}; + const newTopics = [...topics]; + newTopics.push(newTopic); + setTopics(newTopics); + setId(nextId) + setNextId(nextId+1); + setMode('READ'); + }}> + } else if(mode === 'UPDATE'){ + let title, body = null; + for(let i = 0; i < topics.length; i++) { + if(topics[i].id === id) { + title = topics[i].title; + body = topics[i].body + } + } + content = { + const newTopics = [...topics]; + const updatedTopic = {id:id, title:title, body:body} + for(let i = 0; i < newTopics.length; i++) { + if(newTopics[i].id === id) { + newTopics[i] = updatedTopic; + break + } + } + setTopics(newTopics); + setMode('READ'); + }}> + } + + return ( +
    +
    { + setMode('WELCOME'); + }}>
    + + {content} + + +
    + ); } export default App; From 887e88e7c100bf54c836f0d9b050d64267fa688b Mon Sep 17 00:00:00 2001 From: DDuckyee Date: Fri, 9 Jun 2023 11:26:19 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[refactor]=20pretier=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toy/src/App.js | 352 +++++++++++++++++++++++++++++++------------------ 1 file changed, 221 insertions(+), 131 deletions(-) diff --git a/toy/src/App.js b/toy/src/App.js index a9797dc..521e7ed 100644 --- a/toy/src/App.js +++ b/toy/src/App.js @@ -1,172 +1,262 @@ -import './App.css'; -import { useState } from 'react'; +import "./App.css"; +import { useState } from "react"; function Header(props) { - return
    -

    { - event.preventDefault(); - props.onChangeMode(); - }}>{props.title}

    -
    + return ( +
    +

    + { + event.preventDefault(); + props.onChangeMode(); + }} + > + {props.title} + +

    +
    + ); } function Nav(props) { - const lis = [] - for(let i = 0; i < props.topics.length; i++){ - let t = props.topics[i]; - lis.push(
  • - { - event.preventDefault(); - props.onChangeMode(Number(event.target.id)); - }}>{t.title} -
  • ); - } - return + const lis = []; + for (let i = 0; i < props.topics.length; i++) { + let t = props.topics[i]; + lis.push( +
  • + { + event.preventDefault(); + props.onChangeMode(Number(event.target.id)); + }} + > + {t.title} + +
  • + ); + } + return ( + + ); } function Article(props) { - return
    -

    {props.title}

    - {props.body} -
    + return ( +
    +

    {props.title}

    + {props.body} +
    + ); } function Create(props) { - return
    -

    Create

    -
    { - event.preventDefault(); - const title = event.target.title.value; - const body = event.target.body.value; - props.onCreate(title, body); - }}> -

    -

    -

    -
    -
    + return ( +
    +

    Create

    +
    { + event.preventDefault(); + const title = event.target.title.value; + const body = event.target.body.value; + props.onCreate(title, body); + }} + > +

    + +

    +

    + +

    +

    + +

    +
    +
    + ); } function Update(props) { const [title, setTitle] = useState(props.title); const [body, setBody] = useState(props.body); - return
    -

    Update

    -
    { - event.preventDefault(); - const title = event.target.title.value; - const body = event.target.body.value; - props.onUpdate(title, body); - }}> -

    { - setTitle(event.target.value); - }}/>

    -

    -

    -
    -
    + return ( +
    +

    Update

    +
    { + event.preventDefault(); + const title = event.target.title.value; + const body = event.target.body.value; + props.onUpdate(title, body); + }} + > +

    + { + setTitle(event.target.value); + }} + /> +

    +

    + +

    +

    + +

    +
    +
    + ); } function App() { // const _mode = useState('WELCOME'); // const mode = _mode[0]; // const setMode = _mode[1]; - const [mode, setMode] = useState('WELCOME'); - const [id, setId] = useState('null'); + const [mode, setMode] = useState("WELCOME"); + const [id, setId] = useState("null"); const [nextId, setNextId] = useState(4); - const [topics, setTopics] = useState([ - {id:1, title: 'html', body: 'html is ...'}, - {id:2, title: 'css', body: 'css is ...'}, - {id:3, title: 'javascript', body: 'javascript is ...'} - ]); + const [topics, setTopics] = useState([ + { id: 1, title: "html", body: "html is ..." }, + { id: 2, title: "css", body: "css is ..." }, + { id: 3, title: "javascript", body: "javascript is ..." }, + ]); let content = null; let contextControl = null; - if(mode === 'WELCOME'){ - content =
    - } else if(mode === 'READ'){ - let title, body = null; - for(let i = 0; i < topics.length; i++) { - if(topics[i].id === id) { + if (mode === "WELCOME") { + content =
    ; + } else if (mode === "READ") { + let title, + body = null; + for (let i = 0; i < topics.length; i++) { + if (topics[i].id === id) { title = topics[i].title; - body = topics[i].body + body = topics[i].body; } } - content =
    - contextControl = <> -
  • { - event.preventDefault(); - setMode('UPDATE'); - }}>Update
  • -
  • { - const newTopics = [] - for(let i = 0; i < topics.length; i++) { - if(topics[i].id !== id) { - newTopics.push(topics[i]) - } - } - setTopics(newTopics); - setMode('WELCOME'); - }}/>
  • - - } else if(mode === 'CREATE'){ - content = { - const newTopic = {id:nextId, title:_title, body:_body}; - const newTopics = [...topics]; - newTopics.push(newTopic); - setTopics(newTopics); - setId(nextId) - setNextId(nextId+1); - setMode('READ'); - }}> - } else if(mode === 'UPDATE'){ - let title, body = null; - for(let i = 0; i < topics.length; i++) { - if(topics[i].id === id) { + content =
    ; + contextControl = ( + <> +
  • + { + event.preventDefault(); + setMode("UPDATE"); + }} + > + Update + +
  • +
  • + { + const newTopics = []; + for (let i = 0; i < topics.length; i++) { + if (topics[i].id !== id) { + newTopics.push(topics[i]); + } + } + setTopics(newTopics); + setMode("WELCOME"); + }} + /> +
  • + + ); + } else if (mode === "CREATE") { + content = ( + { + const newTopic = { id: nextId, title: _title, body: _body }; + const newTopics = [...topics]; + newTopics.push(newTopic); + setTopics(newTopics); + setId(nextId); + setNextId(nextId + 1); + setMode("READ"); + }} + > + ); + } else if (mode === "UPDATE") { + let title, + body = null; + for (let i = 0; i < topics.length; i++) { + if (topics[i].id === id) { title = topics[i].title; - body = topics[i].body + body = topics[i].body; } } - content = { - const newTopics = [...topics]; - const updatedTopic = {id:id, title:title, body:body} - for(let i = 0; i < newTopics.length; i++) { - if(newTopics[i].id === id) { - newTopics[i] = updatedTopic; - break - } - } - setTopics(newTopics); - setMode('READ'); - }}> + content = ( + { + const newTopics = [...topics]; + const updatedTopic = { id: id, title: title, body: body }; + for (let i = 0; i < newTopics.length; i++) { + if (newTopics[i].id === id) { + newTopics[i] = updatedTopic; + break; + } + } + setTopics(newTopics); + setMode("READ"); + }} + > + ); } - return ( -
    -
    { - setMode('WELCOME'); - }}>
    - + return ( +
    +
    { + setMode("WELCOME"); + }} + >
    + {content} -
    - ); +
    + ); } export default App;