-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dietary.js
141 lines (126 loc) · 3.5 KB
/
Dietary.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
import React, {Component} from 'react';
import {View, Text, StyleSheet, FlatList, Animated, TouchableOpacity, Button} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
const AnimatedButton = Animated.createAnimatedComponent(TouchableOpacity)
export default class Dietary extends Component{
constructor(props) {
super(props);
this.restr = ['Lactose Intolerance', 'Vegetarian', 'Peanut Allergies', 'Diabetic', 'Gluten Free'] // kosher halal
this.restr = this.restr.map((item,i) => {
return {
id: i.toString(),
title: item,
checked: false,
animated_value: new Animated.Value(0),
}
})
this.state = {};
this.onBubbleClick = this.onBubbleClick.bind(this);
this.saveDietary = this.saveDietary.bind(this);
}
onBubbleClick = item_id => {
element = this.restr.find(v => v.id == item_id)
val = element.animated_value
Animated.timing(val, {
toValue: (val._value == 150 ? 0 : 150),
duration: 800
}).start();
element.checked = !element.checked
};
saveDietary = async () => {
// store in AsyncStorage
try {
await AsyncStorage.setItem('@dietary', JSON.stringify(this.restr.map(item => {
return {
title: item.title,
checked: item.checked,
}
})))
} catch (e) {
// saving error
}
console.log('saving diet', this.restr.map(item => {
return {
title: item.title,
checked: item.checked,
}
}));
//navigate to dietary restrictions page
this.props.navigation.navigate('Preferences');
}
render() {
const interpolateColor = val => {
return val.interpolate({
inputRange: [0, 150],
outputRange: ['rgb(255,255,0)', 'rgb(51, 250, 170)']
})
};
const animatedStyle = val => {
return {
backgroundColor: interpolateColor(val),
}
};
return (
<View style = {styles.container}>
<Text style={styles.title}>
Preferences
</Text>
<FlatList
data = {this.restr}
contentContainerStyle={{flexGrow: 1, justifyContent: 'space-evenly'}}
renderItem = {({ item }) => <AnimatedButton style = {[styles.bubble, animatedStyle(item.animated_value)]} onPress = {() => this.onBubbleClick(item.id)}><Text>{ item.title }</Text></AnimatedButton>}
keyExtractor={item => item.id}
/>
<TouchableOpacity
style={styles.customBtnBG}
onPress={this.saveDietary} >
<Text style={styles.customBtnText}>Continue</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
customBtnText: {
fontSize: 30,
color: '#FFFF00',
fontWeight: 'bold',
alignItems:'center',
justifyContent: 'center',
},
customBtnBG: {
backgroundColor: '#33FAAA',
paddingHorizontal: 15,
paddingVertical: 2,
borderRadius: 30,
marginBottom: 40,
alignItems:'center',
justifyContent: 'center',
},
title: {
marginTop: 70,
color: '#33FAAA',
fontWeight: 'bold',
fontSize: 30,
width:250,
height:40,
textAlign: 'center',
alignItems:'center',
justifyContent: 'center',
marginBottom: 30,
},
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#00b5ec',
},
bubble: {
backgroundColor: '#FFFFFF',
borderRadius:50,
width:250,
height:80,
alignItems:'center',
justifyContent: 'center',
},
})