-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const isDev = process.env.NODE_ENV !== 'production'; | ||
|
||
module.exports = { | ||
styledComponents: { | ||
fileName: isDev, | ||
displayName: isDev, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import axios from 'axios'; | ||
|
||
export default { | ||
getBeerList: () => { | ||
return axios.get(`https://api.punkapi.com/v2/beers`); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useFetchData = callback => { | ||
const [isFetched, setIsFetched] = useState(false); | ||
const [data, setData] = useState(null); | ||
const [error, setError] = useState(null); | ||
|
||
useEffect(() => { | ||
callback() | ||
.then(res => { | ||
setData(res.data); | ||
setIsFetched(true); | ||
}) | ||
.catch(err => { | ||
setError(err); | ||
}); | ||
}, [callback]); | ||
|
||
return [isFetched, data, error]; | ||
}; | ||
|
||
export default useFetchData; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const initialState = { | ||
loading: false, | ||
data: null, | ||
error: null, | ||
}; | ||
|
||
export const GET_BEER_LIST_REQUEST = 'beerlist/GET_BEER_LIST_REQUEST'; | ||
export const GET_BEER_LIST_SUCCESS = 'beerlist/GET_BEER_LIST_SUCCESS'; | ||
export const GET_BEER_LIST_FAILED = 'beerlist/GET_BEER_LIST_FAILED'; | ||
|
||
export const getBeerListRequest = () => ({ | ||
type: GET_BEER_LIST_REQUEST, | ||
}); | ||
export const getBeerListSuccess = payload => ({ | ||
type: GET_BEER_LIST_SUCCESS, | ||
payload, | ||
}); | ||
export const getBeerListFailed = payload => ({ | ||
type: GET_BEER_LIST_FAILED, | ||
payload, | ||
}); | ||
|
||
const beerListReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case GET_BEER_LIST_REQUEST: | ||
return { | ||
...state, | ||
loading: true, | ||
}; | ||
case GET_BEER_LIST_SUCCESS: | ||
return { | ||
...state, | ||
loading: false, | ||
data: action.payload, | ||
}; | ||
case GET_BEER_LIST_FAILED: | ||
return { | ||
...state, | ||
loading: false, | ||
error: action.payload, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default beerListReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { call, put, takeLatest } from 'redux-saga/effects'; | ||
import { | ||
GET_BEER_LIST_REQUEST, | ||
getBeerListSuccess, | ||
getBeerListFailed, | ||
} from '../beerList'; | ||
import APIs from '../../APIs'; | ||
|
||
function* getBeerList() { | ||
try { | ||
const { data } = yield call(APIs.getBeerList); // yield call은 결과 반환시까지 기다려줌 | ||
yield put(getBeerListSuccess(data)); // action dispatch | ||
} catch (err) { | ||
yield put(getBeerListFailed(err)); | ||
} | ||
} | ||
export default function* getBeerListSaga() { | ||
yield takeLatest(GET_BEER_LIST_REQUEST, getBeerList); | ||
// yield takeLatest(...) : 가장 마지막 요청에 대해 어떤 함수를 실행시킬지 지정 | ||
// REQUEST_DATA 액션 객체가 들어오면 getApiData 실행 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components/macro'; | ||
|
||
const Beers = () => { | ||
const BeerList = () => { | ||
return <h1>Beer List</h1>; | ||
}; | ||
|
||
export default Beers; | ||
export default BeerList; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import styled from 'styled-components/macro'; | ||
|
||
const Home = () => { | ||
return <h1>Home</h1>; | ||
const beerList = useSelector(s => s.beerListReducer.data); | ||
|
||
return <h1>{`저장된 맥주 정보 ${beerList?.length || 0}개`}</h1>; | ||
}; | ||
|
||
export default Home; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters