Skip to content

Commit

Permalink
history 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
seobbang committed Nov 11, 2022
1 parent bfb5c1d commit ff68d96
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 18 deletions.
7 changes: 6 additions & 1 deletion week4/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import MainPage from "./pages/MainPage";
import UserInfo from "./components/UserInfo";

function App() {
const [userId, setUserId] = useState("");
//const [history, setHistory] = useState(["hi", "me"]);
//const arrString = JSON.stringify(history);

//window.localStorage.setItem("history", arrString);

//console.log(arrString);

return (
<BrowserRouter>
Expand Down
52 changes: 52 additions & 0 deletions week4/src/components/History.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import styled from "styled-components";

export default function History({ setShow, item, history, setHistory }) {
const navigate = useNavigate();
const handleClick = (e) => {
const userId = e.target.innerText;
navigate(`/search/${userId}`);
setShow(false);
};
const handleDelete = (e) => {
setHistory(history.filter((item) => item !== e.target.id));
};
return (
<Container>
<HistoryList onClick={handleClick}>{item}</HistoryList>
<DelHistory id={item} onClick={handleDelete}>
X
</DelHistory>
</Container>
);
}

const Container = styled.div`
display: flex;
z-index: 1;
`;

const HistoryList = styled.button`
display: flex;
align-items: center;
background-color: black;
opacity: 40%;
text-align: center;
color: white;
font-size: 16px;
width: 88%;
height: 40px;
border: none;
`;

const DelHistory = styled.button`
color: white;
background-color: black;
width: 12%;
`;
58 changes: 46 additions & 12 deletions week4/src/components/SearchBox.jsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,63 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import React, { useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import styled from "styled-components";
import History from "./History";

export default function SearchBox() {
const navigate = useNavigate();

const [history, setHistory] = useState([]);
const [show, setShow] = useState(false);

const handleOnKeyPress = (e) => {
if (e.key == "Enter") {
const userId = e.target.value;
if (!history.includes(userId)) {
setHistory([userId, ...history]);
}
navigate(`/search/${userId}`);
setShow(false);
}
};

// 렌더링
const historyList = history.map((item) => (
<History
setShow={setShow}
item={item}
history={history}
setHistory={setHistory}
key={item}
/>
));

return (
<Container>
<Title>Github Profile Finder</Title>
<SearchBar
placeholder="Github username..."
onKeyPress={handleOnKeyPress}
></SearchBar>
<SearchContainer>
<SearchBar
placeholder="Github username..."
onKeyPress={handleOnKeyPress}
onFocus={() => {
setShow(true);
}}
/>
{show ? historyList : null}
</SearchContainer>
</Container>
);
}

const Container = styled.div`
display: flex;
flex-direction: column;
justify-content: center;
flex-wrap: wrap;
align-items: center;
position: relative;
top: 50px;
background-color: #000000;
opacity: 30%;
background-color: #f0b2ad;
font-size: 32px;
font-weight: bold;
Expand All @@ -46,16 +70,26 @@ const Container = styled.div`
const Title = styled.h1`
color: #ffffff;
margin: 0;
margin-top: 17px;
margin: 25px 0;
`;

const SearchContainer = styled.div`
display: flex;
flex-direction: column;
`;

const SearchBar = styled.input`
box-sizing: border-box;
background-color: white;
background-color: black;
opacity: 70%;
font-size: 18px;
color: white;
width: 290px;
height: 40px;
padding: 10px;
border: none;
border-radius: 5px;
`;
9 changes: 5 additions & 4 deletions week4/src/components/UserInfo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import styled from "styled-components";
import AccountInfo from "./AccountInfo";
import axios from "axios";
import { Navigate, useNavigate, useParams } from "react-router-dom";
import { useNavigate, useParams } from "react-router-dom";

export default function UserInfo() {
const navigate = useNavigate();
Expand Down Expand Up @@ -61,9 +61,9 @@ const Container = styled.div`
position: relative;
top: 70px;
background-color: #000000;
opacity: 30%;
background-color: #f0b2ad;
font-size: 32px;
font-weight: bold;
width: 600px;
Expand All @@ -81,6 +81,7 @@ const CloseButton = styled.button`
font-size: 20px;
background: none;
border: none;
`;

const Image = styled.img`
Expand Down Expand Up @@ -113,6 +114,7 @@ const VisitBt = styled.button`
&:hover {
background-color: #686868;
font-weight: bold;
}
`;

Expand All @@ -121,7 +123,6 @@ const Container2 = styled.div`
flex-direction: row;
justify-content: space-between;
background-color: black;
width: 500px;
height: 120px;
margin: 10px 0;
Expand Down
2 changes: 1 addition & 1 deletion week4/src/pages/MainPage.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useNavigate } from "react";
import React, { useState } from "react";
import { Outlet } from "react-router-dom";
import styled from "styled-components";
import SearchBox from "../components/SearchBox";
Expand Down

0 comments on commit ff68d96

Please sign in to comment.