주특기 5강!
- material UI를 써서 컴포넌트를 만들 수 있다.
- 로딩 스피너를 제작해서 UX 품질을 높일 수 있다.
material UI
yarn add @material-ui/core @material-ui/icons
import {Button, ButtonGroup} from "@material-ui/core";
...
<ButtonGroup>
<Button
variant="outlined"
onClick={() => {
dispatch(deleteBucketFB(bucket_index));
props.history.goBack();
}}
>
삭제하기
</Button>
<Button
variant="outlined"
color="primary"
onClick={() => {
dispatch(updateBucketFB(bucket_index));
props.history.goBack();
}}
>
완료하기
</Button>
</ButtonGroup>
...
로딩 스피너
import React from "react";
import styled from "styled-components";
import {Eco} from "@material-ui/icons";
const Spinner = (props) => {
return (
<Outter>
<Eco style={{fontSize: "150px", color: "#673ab7"}} />
</Outter>
);
};
const Outter = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
display:flex;
align-items: center;
justify-content: center;
background-color: #ede2ff;
`;
export default Spinner;
...
import Spinner from "./Spinner";
...
const mapStateToProps = (state) => ({
bucket_list: state.bucket.list,
is_loaded: state.bucket.is_loaded
});
class App extends React.Component {
...
render() {
return (
<div className="App">
{this.props.is_loaded ?
<>
<Container>
<Title>내 버킷리스트</Title>
...
</> :
<Spinner></Spinner>}
</div>
);
}
}
...
import {firestore} from "../../firebase"
const bucket_db = firestore.collection("bucket")
...
const LOADED = "bucket/LOADED";
const initialState = {
list: [
{text: '영화관 가기', completed: false},
{text: '매일 책읽기', completed: false},
{text: '수영 배우기', completed: false},
],
is_loaded: false,
};
...
export const isLoaded = (loaded) => {
return {type: LOADED, loaded};
}
export const addBucketFB = (bucket) => {
return function (dispatch) {
dispatch(isLoaded(false));
let bucket_data = {text: bucket, completed: false}
bucket_db.add(bucket_data).then(docRef => {
bucket_data = {...bucket_data, id: docRef.id};
dispatch(createBucket(bucket_data))
dispatch(isLoaded(true));
})
}
}
...
export default function reducer(state = initialState, action) {
switch (action.type) {
case "bucket/LOAD": {
if (action.bucket.length) {
return {...state, list: action.bucket, is_loaded: true};
}
return state;
}
case "bucket/CREATE":
const new_bucket_list = [...state.list, action.bucket];
return {...state, list: new_bucket_list};
...
case "bucket/LOADED": {
return {...state, is_loaded: action.loaded}
}
default:
return state;
}
}