-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
204 lines (187 loc) · 4.8 KB
/
App.tsx
File metadata and controls
204 lines (187 loc) · 4.8 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import React from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
SafeAreaView,
Button,
NativeModules,
} from 'react-native';
import RadioGroup from 'react-native-radio-buttons-group';
import Snackbar from 'react-native-snackbar';
import 'react-native-url-polyfill/auto';
import {createClient} from '@supabase/supabase-js';
import {SUPABASE_URL, SUPABASE_KEY} from '@env';
function App(): JSX.Element {
const {PaystackModule} = NativeModules;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const [transactionReference, setTransactionReference] = React.useState('');
const [email, setEmail] = React.useState('');
const [quantity, setQuantity] = React.useState('');
const [tickets, setTickets] = React.useState([]);
const [pricelist, setPricelist] = React.useState({});
const [total, setTotal] = React.useState(0);
const [selectedTicket, setSelectedTicket] = React.useState('');
const getTickets = async () => {
const {data, error} = await supabase.functions.invoke('fetch-tickets');
if (data) {
parseResponse(data.data);
}
if (error) {
console.log('Error: ', error);
}
};
React.useEffect(() => {
getTickets();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const parseResponse = (json: any[]) => {
// @ts-ignore
const result = [];
const price = {};
json.forEach(ticket => {
result.push({
id: ticket.id,
label: ticket.name,
value: ticket.id,
});
// @ts-ignore
price[ticket.id] = ticket.price;
});
// @ts-ignore
setTickets(result);
setPricelist(price);
};
const updateTotal = (value: string) => {
// @ts-ignore
setTotal(parseInt(value, 10) * pricelist[selectedTicket]);
setQuantity(value);
};
const updateSelectedTicket = (value: string) => {
if (!quantity && quantity.length < 1) {
setQuantity('0');
}
//@ts-ignore
setTotal(parseInt(quantity, 10) * pricelist[value]);
setSelectedTicket(value);
};
const resetState = () => {
setEmail('');
setQuantity('0');
setSelectedTicket('');
setTotal(0);
};
const submitForm = () => {
// mutiply total by 100
PaystackModule.makePayment(total * 100, (ref: string) => {
console.log('transaction ref: ', ref);
setTransactionReference(transactionReference);
// confirm payment, then do post payment activiteis
completeRegistration();
});
};
const completeRegistration = async () => {
const {data, error} = await supabase.functions.invoke(
'create-registration',
{
body: {
email: email,
quantity: quantity,
ticket_id: selectedTicket,
},
},
);
if (data) {
Snackbar.show({
text: 'Registration successful!',
duration: Snackbar.LENGTH_SHORT,
});
resetState();
}
if (error) {
console.log('Error: ', error);
}
};
return (
<View style={styles.mainContainer}>
<Text style={styles.title}>Registration Form</Text>
<View style={styles.line} />
<SafeAreaView style={styles.sectionContainer}>
<Text>Email</Text>
<TextInput
style={styles.input}
onChangeText={setEmail}
placeholder="stanley@email.com"
inputMode="email"
value={email}
/>
</SafeAreaView>
<SafeAreaView style={styles.sectionContainer}>
<Text>Ticket</Text>
<RadioGroup
radioButtons={tickets}
onPress={updateSelectedTicket}
selectedId={selectedTicket}
layout="row"
/>
</SafeAreaView>
<SafeAreaView style={styles.sectionContainer}>
<Text>Quantity</Text>
<TextInput
style={styles.input}
onChangeText={updateTotal}
placeholder="2"
inputMode="numeric"
value={quantity}
/>
</SafeAreaView>
<SafeAreaView style={styles.sectionContainer}>
<Button
title={`Pay NGN ${new Intl.NumberFormat().format(total)}`}
color={'#000'}
onPress={submitForm}
/>
</SafeAreaView>
</View>
);
}
const styles = StyleSheet.create({
mainContainer: {
marginVertical: 32,
paddingHorizontal: 32,
},
input: {
height: 40,
borderWidth: 1,
borderRadius: 4,
borderColor: '#3d3d3d',
padding: 10,
marginTop: 4,
},
sectionContainer: {
marginTop: 32,
},
title: {
fontSize: 24,
fontWeight: '600',
marginTop: 32,
marginBottom: 12,
},
button: {
backgroundColor: '#000',
},
sectionDescription: {
marginTop: 8,
fontSize: 18,
fontWeight: '400',
},
highlight: {
fontWeight: '700',
},
line: {
borderBottomColor: '#bababa',
borderBottomWidth: StyleSheet.hairlineWidth,
},
});
export default App;