-
Notifications
You must be signed in to change notification settings - Fork 9
/
RewardsComponent.js
152 lines (144 loc) · 3.57 KB
/
RewardsComponent.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
import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import { Text, View, Animated } from 'react-native';
import posed from 'react-native-pose';
import { generateConfettiItems, generateConfettiInitialTranslations, generateConfettiAnimations } from './confetti';
import { generateEmojiItems } from './emoji';
const confecttiCount = 40;
const SpringAnim = posed.View({
clicked: {
y: 5,
transition: {
type: 'spring',
stiffness: 200,
damping: 2,
},
},
punished: {
x: 5,
transition: {
type: 'spring',
stiffness: 200,
damping: 2,
},
},
rest: {
x: 0,
y: 0,
transition: {
type: 'spring',
stiffness: 200,
damping: 2,
},
},
});
class RewardsComponent extends Component {
state={
translations: generateConfettiInitialTranslations(confecttiCount, 0),
state: null,
}
constructor(props) {
super(props);
this.containerRef = createRef();
}
get animationParams() {
const { initialSpeed, spread, deacceleration, rotationXSpeed, rotationZSpeed } = this.props;
const params = { initialSpeed, spread, deacceleration, rotationXSpeed, rotationZSpeed };
return params;
}
async rest() {
setTimeout(() => {
this.setState({ state: 'rest' });
this.props.onRest();
}, 100);
}
async punishMe() {
this.setState({ state: 'punished' });
this.rest();
}
UNSAFE_componentWillReceiveProps(props) {
const newState = props.state;
const { state } = this.props;
if (state === newState) {
return;
}
switch (newState) {
case 'reward':
this.rewardMe();
break;
case 'punish':
this.rewardMe();
break;
default:
}
}
async rewardMe() {
this.setState({ state: 'clicked' });
const translations = generateConfettiInitialTranslations(confecttiCount);
this.setState({ translations }, () => generateConfettiAnimations(translations, this.animationParams));
this.rest();
}
render() {
const { children, animationType, colors, emojis } = this.props;
const { translations, state } = this.state;
let items;
switch (animationType) {
case 'confetti':
items = generateConfettiItems(translations, confecttiCount, colors);
break;
case 'emoji':
items = generateEmojiItems(translations, confecttiCount, emojis);
break;
default:
items = generateConfettiItems(translations, confecttiCount, colors);
}
return (
<View>
<View style={{ alignItems: 'center', justifyContent: 'center' }}>
{items}
</View>
<SpringAnim pose={state}>
{children}
</SpringAnim>
</View>
);
}
}
RewardsComponent.propTypes = {
children: PropTypes.element.isRequired,
initialSpeed: PropTypes.number,
spread: PropTypes.number,
deacceleration: PropTypes.number,
rotationXSpeed: PropTypes.number,
rotationZSpeed: PropTypes.number,
particiesCount: PropTypes.number,
colors: PropTypes.array,
emojis: PropTypes.array,
animationType: PropTypes.oneOf(['confetti', 'emoji']),
state: PropTypes.oneOf(['rest', 'reward', 'punish']),
onRest: PropTypes.func,
};
RewardsComponent.defaultProps = {
initialSpeed: 1,
spread: 1,
deacceleration: 1,
rotationXSpeed: 5,
rotationZSpeed: 5,
particiesCount: 20,
colors: [
'#A45BF1',
'#25C6F6',
'#72F753',
'#F76C88',
'#F5F770',
],
emojis: [
'👍',
'😊',
'🎉',
],
animationType: 'confetti',
state: 'rest',
onRest: () => {},
};
export default RewardsComponent;