-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRibbon.js
154 lines (116 loc) · 4.77 KB
/
Ribbon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { Component, } from 'react';
import { createApi } from 'unsplash-js';
import './stylecomponents.scss'
import Button from './Button.js'
import Toggle from './Toggle.js'
import { BiBeer, BiCloudLightRain, BiGhost, BiUndo } from 'react-icons/bi';
//Class + functions component
//function to hide changing style
function offsetToggle(isToggled){
if (isToggled===false)
return {bottom:"10px",};
else
return {bottom:"-140px",};
}
//API to get images from Unsplash with a state from parent to update
const pathUnsplash = (scenario, setButton) =>{
//exit in case the scenario is the one
if (scenario===0)
return;
const arrayScenarios = {
1: "ghost",
2: "storm",
3: "beer"
}
//TODO hide on server
const accessKey ='YSKE_r_OqHTWaoZXGYGHnChVg34Zq0cWTU5OJrJEoOQ';
//create session on unsplash
const unsplash = createApi({
accessKey: accessKey,
// `fetch` options to be sent with every request
headers: { 'X-Custom-Header': 'foo' },
});
//get a random photo
return unsplash.photos.getRandom({
query: arrayScenarios[scenario],
orientation: 'landscape',
count: 1,
}).then(result => {
switch (result.type) {
default:
console.log('error occurred: ', result.errors[0]);
break;
case 'success':
console.log(result.response[0].urls.small);
//update state to re-render on async promise
setButton({
buttonPressed: scenario,
urlPhoto: result.response[0].urls.small,
});
break;
}
});
}
class Ribbon extends Component {
constructor(props) {
super(props);
this.state = {
isToggled : false,
}
this.wrapperRef = React.createRef();
this.handleToggle= this.handleToggle.bind(this)
}
handleClick(scenario){
//check if only have to update button state (houses case)
if (scenario===0){
this.props.setId(0);
this.props.setScenario({
buttonPressed:0,
})
}
else
//set promise to get URL and buttonpressed
pathUnsplash(scenario, this.props.setScenario);
}
handleToggle(){
//animate ribbon with css isnavopen
//const wrapper = this.wrapperRef.current;
//wrapper.classList.toggle('is-nav-open');
//change state
this.setState({isToggled:!this.state.isToggled,})
//alert(this.state.isToggled)
}
render() {
return (
<div ref={this.wrapperRef} className='ribbondiv' style={offsetToggle(this.state.isToggled)} >
<Toggle onToggle={()=>this.handleToggle()}/>
<div className="bottom">
{(() => {
switch (this.props.show) {
case 1: return ( <div className="array ">
<Button icon={<BiUndo />} handleClick={()=>this.handleClick(0)} />
<Button icon={<BiGhost/>} handleClick={()=>this.handleClick(1)} />
</div>)
case 2: return ( <div className="array ">
<Button icon={<BiUndo /> } handleClick={()=>this.handleClick(0)}/>
<Button icon={<BiGhost />} handleClick={()=>this.handleClick(1)}/>
<Button icon={<BiCloudLightRain />} handleClick={()=>this.handleClick(2)}/>
</div>)
case 3: return ( <div className="array ">
<Button icon={<BiUndo />} handleClick={()=>this.handleClick(0)}/>
<Button icon={<BiGhost />} handleClick={()=>this.handleClick(1)}/>
<Button icon={<BiCloudLightRain />} handleClick={()=>this.handleClick(2)}/>
<Button icon={<BiBeer />} handleClick={()=>this.handleClick(3)}/>
</div>)
default: return ( <div className="arraybg ">
<Button icon={<BiBeer />} />
</div>)
}
})()}
<img alt='' src='./resources/panel-vacio.png' />
</div>
</div>
);
}
}
export default Ribbon;