Skip to content

Commit daed4cf

Browse files
committed
choosing page codes refactored again:)
1 parent ef9dd96 commit daed4cf

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

src/js/views/components/ChoosingPage.js

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,47 @@ const Spinner = require('./Spinner');
55

66
class ChoosingPage extends React.Component{
77

8-
componentDidMount(){
8+
constructor(props){
9+
super(props);
10+
11+
this.state = {
12+
loadingShow: false,
13+
addressWrapperShow: false,
14+
selected: false,
15+
createSelected: false,
16+
joinSelected: false,
17+
choosingPageFadeOut: false,
18+
choosingPageNone: false,
19+
addressInputHasError: false
20+
};
921

10-
this.refs.create.onclick = async ()=>{
22+
}
1123

12-
await this.refs.create.classList.add('choosingPage__item--selected');
24+
componentDidMount(){
1325

14-
this.refs.join.classList.add('choosingPage__item--not-selected');
26+
this.refs.create.onclick = async ()=>{
1527

16-
this.refs.loading.classList.add('choosingPage__loading--show');
28+
this.setState({
29+
createSelected: true,
30+
selected: true,
31+
loadingShow: true
32+
})
1733

1834
// Let's create
1935

2036
socketHandler.createServer().then(() => {
2137

2238
setTimeout(() => {
23-
this.refs.loading.classList.remove('choosingPage__loading--show');
24-
25-
this.refs.choosingPage.classList.add('choosingPage--fadeOut');
39+
40+
this.setState({
41+
loadingShow: false,
42+
choosingPageFadeOut: true
43+
})
2644

2745
setTimeout(() => {
28-
this.refs.choosingPage.classList.add('none');
46+
this.setState({
47+
choosingPageNone: true
48+
})
2949
}, 500)
3050

3151
}, 500)
@@ -36,15 +56,18 @@ class ChoosingPage extends React.Component{
3656

3757
this.refs.join.onclick = async()=>{
3858

39-
await this.refs.join.classList.add('choosingPage__item--selected');
40-
41-
this.refs.create.classList.add('choosingPage__item--not-selected');
59+
this.setState({
60+
joinSelected: true,
61+
selected: true
62+
})
4263

4364
// Let's join
4465

4566
setTimeout(() => {
4667

47-
this.refs.addressWrapper.classList.add('choosingPage__address-wrapper--show');
68+
this.setState({
69+
addressWrapperShow: true
70+
})
4871

4972
let ipAddressInput = this.refs.addressWrapper.children[0];
5073

@@ -58,31 +81,39 @@ class ChoosingPage extends React.Component{
5881

5982
let ipAddress = ipAddressInput.value;
6083

61-
this.refs.loading.classList.add('choosingPage__loading--show');
84+
this.setState({
85+
loadingShow: true
86+
})
6287

6388
socketHandler.createClient(ipAddress).then(() => {
6489

65-
ipAddressInput.classList.remove('input--error');
90+
this.setState({
91+
addressInputHasError: false
92+
})
6693

6794
setTimeout(() => {
6895

69-
this.refs.loading.classList.remove('choosingPage__loading--show');
70-
71-
this.refs.choosingPage.classList.add('choosingPage--fadeOut');
96+
this.setState({
97+
loadingShow: false,
98+
choosingPageFadeOut: true
99+
})
72100

73101
setTimeout(() => {
74102

75-
this.refs.choosingPage.classList.add('none');
103+
this.setState({
104+
choosingPageNone: true
105+
})
76106

77107
}, 500)
78108

79109
}, 500)
80110

81111
}).catch(() => {
82112

83-
this.refs.loading.classList.remove('choosingPage__loading--show');
84-
85-
this.refs.addressWrapper.children[0].classList.add('input--error');
113+
this.setState({
114+
loadingShow: false,
115+
addressInputHasError: true
116+
})
86117

87118
});
88119
}
@@ -92,13 +123,16 @@ class ChoosingPage extends React.Component{
92123
}, 500)
93124

94125
}
126+
95127
}
96128

97129
render (){
98130
return (
99-
<div ref='choosingPage' className="choosingPage">
131+
<div className={`choosingPage ${this.state.choosingPageFadeOut ? 'choosingPage--fadeOut' : ''} ${this.state.choosingPageNone ? 'none' : ''}`}>
100132

101-
<div ref="create" className="choosingPage__item choice">
133+
<div className={`choosingPage__item choice
134+
${this.state.createSelected ? 'choosingPage__item--selected' : ''}
135+
${this.state.selected && this.state.joinSelected ? 'choosingPage__item--not-selected' : ''}`}>
102136

103137
<img src="../images/networking.png" alt="" />
104138

@@ -108,7 +142,9 @@ class ChoosingPage extends React.Component{
108142

109143
</div>
110144

111-
<div ref="join" className="choosingPage__item choice">
145+
<div className={`choosingPage__item choice
146+
${this.state.joinSelected ? 'choosingPage__item--selected' : ''}
147+
${this.state.selected && this.state.createSelected ? 'choosingPage__item--not-selected' : ''}`}>
112148

113149
<img src="../images/networking.png" alt="" />
114150

@@ -118,15 +154,15 @@ class ChoosingPage extends React.Component{
118154

119155
</div>
120156

121-
<div ref='loading' className="choosingPage__loading">
157+
<div className={`choosingPage__loading ${this.state.loadingShow ? 'choosingPage__loading--show' : ''}`}>
122158

123159
<Spinner />
124160

125161
</div>
126162

127-
<div ref='addressWrapper' className="choosingPage__address-wrapper">
163+
<div ref='addressWrapper' className={`choosingPage__address-wrapper ${this.state.addressWrapperShow ? 'choosingPage__address-wrapper--show': ''}`}>
128164

129-
<Input center={true} placeholder='IP Address' />
165+
<Input className={this.state.addressInputHasError ? 'input--error':''} center={true} placeholder='IP Address' />
130166

131167
</div>
132168

src/js/views/components/Input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Input extends React.Component{
88
type={this.props.type ? this.props.type : 'text'}
99
defaultValue={this.props.value ? this.props.value : ''}
1010
placeholder={this.props.placeholder ? this.props.placeholder : ''}
11-
className={`input${this.props.center ? ' input--center' : ''}`}
11+
className={`input${this.props.center ? ' input--center' : ''} ${this.props.className}`}
1212
/>
1313
);
1414
}

0 commit comments

Comments
 (0)