안형욱

feat: search logic 추가

...@@ -134,9 +134,7 @@ const Header = () => { ...@@ -134,9 +134,7 @@ const Header = () => {
134 /> 134 />
135 </DropDownWrap> 135 </DropDownWrap>
136 </DropDownContainer> 136 </DropDownContainer>
137 - <InputBlock color="blue" fontsize="20px" width="850px" display=""> 137 + <InputBlock color="blue" fontsize="20px" width="850px" display="" />
138 - <input />
139 - </InputBlock>
140 </SearchContainer> 138 </SearchContainer>
141 <MenuContainer> 139 <MenuContainer>
142 <ul> 140 <ul>
......
...@@ -2,8 +2,11 @@ import React, { useState, useEffect } from 'react'; ...@@ -2,8 +2,11 @@ import React, { useState, useEffect } from 'react';
2 import { TextInput } from '@mantine/core'; 2 import { TextInput } from '@mantine/core';
3 import styled from 'styled-components'; 3 import styled from 'styled-components';
4 import { useHistory, useLocation } from 'react-router-dom'; 4 import { useHistory, useLocation } from 'react-router-dom';
5 +import { useDispatch } from 'react-redux';
5 import SearchBox from './SearchBox'; 6 import SearchBox from './SearchBox';
6 import { inputColorMap } from '../../lib/styles/palette'; 7 import { inputColorMap } from '../../lib/styles/palette';
8 +import { esApi } from '../../lib/api/elasticsearch';
9 +import { setParsedDocuments } from '../../features/parsedDocumentsSlice';
7 10
8 const InputBlock = styled.div` 11 const InputBlock = styled.div`
9 width: ${props => props.width}; 12 width: ${props => props.width};
...@@ -40,10 +43,16 @@ const MyInput = ({ ...@@ -40,10 +43,16 @@ const MyInput = ({
40 const history = useHistory(); 43 const history = useHistory();
41 const search = useLocation(); 44 const search = useLocation();
42 const name = search.search.substring(7); 45 const name = search.search.substring(7);
46 + const dispatch = useDispatch();
47 +
43 useEffect(() => { 48 useEffect(() => {
49 + const setSearchDatas = async () => {
50 + const { results } = await esApi.search(name);
51 + dispatch(setParsedDocuments(results));
52 + };
44 setQuery(name); 53 setQuery(name);
45 - }, []); 54 + setSearchDatas();
46 - 55 + }, [name]);
47 return ( 56 return (
48 <InputBlock 57 <InputBlock
49 color={color} 58 color={color}
......
1 +import { createSlice } from '@reduxjs/toolkit';
2 +
3 +const parsedDocumentsSlice = createSlice({
4 + name: 'parsedDocuments',
5 + initialState: {
6 + documents: [],
7 + },
8 + reducers: {
9 + setParsedDocuments: (state, action) => {
10 + state.documents = action.payload;
11 + },
12 + },
13 +});
14 +
15 +export const { setParsedDocuments } = parsedDocumentsSlice.actions;
16 +
17 +export default parsedDocumentsSlice.reducer;
1 import React from 'react'; 1 import React from 'react';
2 import ReactDOM from 'react-dom'; 2 import ReactDOM from 'react-dom';
3 +import { configureStore } from '@reduxjs/toolkit';
4 +import { Provider } from 'react-redux';
3 import App from './App'; 5 import App from './App';
6 +import rootReducer from './reducers';
7 +
8 +const store = configureStore({
9 + reducer: rootReducer,
10 +});
4 11
5 ReactDOM.render( 12 ReactDOM.render(
6 <React.StrictMode> 13 <React.StrictMode>
7 - <App /> 14 + <Provider store={store}>
15 + <App />
16 + </Provider>
8 </React.StrictMode>, 17 </React.StrictMode>,
9 document.getElementById('root') 18 document.getElementById('root')
10 ); 19 );
......
...@@ -9,7 +9,6 @@ const Main = styled.div` ...@@ -9,7 +9,6 @@ const Main = styled.div`
9 display: flex; 9 display: flex;
10 align-items: center; 10 align-items: center;
11 justify-content: center; 11 justify-content: center;
12 - padding-left: 7%;
13 `; 12 `;
14 13
15 const Container = styled.div` 14 const Container = styled.div`
......
1 import React from 'react'; 1 import React from 'react';
2 +import { useSelector } from 'react-redux';
2 import Header from '../components/Header'; 3 import Header from '../components/Header';
3 -import Main from '../components/document/index'; 4 +import Main from '../components/document';
4 5
5 const SearchPage = () => { 6 const SearchPage = () => {
7 + const parsedDocuments = useSelector(state => state.parsedDocuments);
8 +
9 + if (parsedDocuments.length === 0) {
10 + return (
11 + <>
12 + <Header />
13 + <div>검색 결과가 없습니다.</div>
14 + </>
15 + );
16 + }
6 return ( 17 return (
7 <> 18 <>
8 <Header /> 19 <Header />
......
1 +import { combineReducers } from 'redux';
2 +import parsedDocumentsReducer from '../features/parsedDocumentsSlice';
3 +
4 +export default combineReducers({
5 + parsedDocuments: parsedDocumentsReducer,
6 +});