1 |
|
2 |
|
3 | import * as React from 'react';
|
4 | import { connect } from 'react-redux';
|
5 |
|
6 | import type { Element } from 'react';
|
7 | import type { LoadingScreenReducerStateType } from './LoadingScreenReducer';
|
8 |
|
9 | import './LoadingScreen.css';
|
10 |
|
11 | type AppStateType = {|
|
12 | loadingScreen: LoadingScreenReducerStateType,
|
13 | |};
|
14 |
|
15 | type MappedStatePropsType = {|
|
16 | percentage: ?number,
|
17 | |};
|
18 | type OwnPropsType = {|
|
19 | open?: boolean,
|
20 | hideLogo?: boolean,
|
21 | hidePercentage?: boolean,
|
22 | hideBar?: boolean,
|
23 | transition?: ?string,
|
24 | minPercentage?: ?number,
|
25 | mainColor?: string,
|
26 | barColor?: string,
|
27 | logo?: Object | string,
|
28 | |};
|
29 | type PropsType = MappedStatePropsType & OwnPropsType;
|
30 |
|
31 | class LoadingScreen extends React.Component<PropsType> {
|
32 | static defaultProps = {
|
33 | open: true,
|
34 | hideLogo: false,
|
35 | hidePercentage: false,
|
36 | hideBar: false,
|
37 | transition: 'width .1s ease-in-out',
|
38 | minPercentage: 10,
|
39 | mainColor: '#6EC8F1',
|
40 | barColor: 'white',
|
41 | logo: '/assets/img/adsum-logo.png',
|
42 | };
|
43 |
|
44 | getPercentage() {
|
45 | const { percentage, minPercentage } = this.props;
|
46 |
|
47 | if (percentage < minPercentage) return minPercentage;
|
48 |
|
49 | if (percentage > 100) return 100;
|
50 |
|
51 | return percentage;
|
52 | }
|
53 |
|
54 | shouldDisplay() {
|
55 | const { open } = this.props;
|
56 | if (!open) return false;
|
57 |
|
58 | const percentage = this.getPercentage();
|
59 | if (percentage === 100) return false;
|
60 |
|
61 | return true;
|
62 | }
|
63 |
|
64 | renderLogo() {
|
65 | const { logo, hideLogo } = this.props;
|
66 |
|
67 | return hideLogo ? null : (
|
68 | <div className="logo-wrapper">
|
69 | <img src={logo} alt="loading screen logo" />
|
70 | </div>
|
71 | );
|
72 | }
|
73 |
|
74 | renderBar() {
|
75 | const { transition, barColor, hideBar } = this.props;
|
76 | const percentage = this.getPercentage();
|
77 |
|
78 | if (hideBar) return null;
|
79 |
|
80 | const barWrapperStyle = {
|
81 | border: `solid 3px ${barColor}`,
|
82 | };
|
83 |
|
84 | const barStyle = {
|
85 | width: `${percentage}%`,
|
86 | backgroundColor: barColor,
|
87 | transition,
|
88 | };
|
89 |
|
90 | return (
|
91 | <div className="progression-bar-wrapper" style={barWrapperStyle}>
|
92 | {
|
93 | percentage === null ? null
|
94 | : <div className="progression-bar" style={barStyle} />
|
95 | }
|
96 | </div>
|
97 | );
|
98 | }
|
99 |
|
100 | renderPercentage() {
|
101 | const { barColor, hidePercentage } = this.props;
|
102 | const percentage = this.getPercentage();
|
103 |
|
104 | if (hidePercentage) return null;
|
105 |
|
106 | const percentageStyle = {
|
107 | color: barColor,
|
108 | };
|
109 |
|
110 | return percentage === null ? null
|
111 | : (
|
112 | <div className="percentage-number" style={percentageStyle}>
|
113 | {`${percentage} %`}
|
114 | </div>
|
115 | );
|
116 | }
|
117 |
|
118 |
|
119 | render(): Element<'div'> {
|
120 | const { mainColor } = this.props;
|
121 |
|
122 | const loadingScreenStyle = {
|
123 | backgroundColor: mainColor,
|
124 | };
|
125 |
|
126 | return !this.shouldDisplay() ? null : (
|
127 | <div className="loadingScreen" style={loadingScreenStyle}>
|
128 | { this.renderLogo() }
|
129 | { this.renderBar() }
|
130 | { this.renderPercentage() }
|
131 | </div>
|
132 | );
|
133 | }
|
134 | }
|
135 |
|
136 | const mapStateToProps = (state: AppStateType): MappedStatePropsType => ({
|
137 | percentage: state.loadingScreen.percentage,
|
138 | });
|
139 |
|
140 | export default connect(
|
141 | mapStateToProps,
|
142 | null,
|
143 | )(LoadingScreen);
|