-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-utils.js
177 lines (155 loc) · 4.56 KB
/
fetch-utils.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const SUPABASE_URL = 'https://eahhmjbkevtqvjyrciae.supabase.co';
const SUPABASE_KEY =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImVhaGhtamJrZXZ0cXZqeXJjaWFlIiwicm9sZSI6ImFub24iLCJpYXQiOjE2NTM2OTIyMDgsImV4cCI6MTk2OTI2ODIwOH0.D0GZv1xy0QcdmaM9dJr3FCobWoGH6RY4NQSZGvYM7Ek';
const client = supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
//yes
export function getUser() {
return client.auth.session() && client.auth.session().user;
}
export function checkAuth() {
const user = getUser();
if (!user) location.replace('../');
}
// export function redirectIfLoggedIn() {
// if (getUser()) {
// location.replace('./other-page');
// }
// }
export async function signupUser(email, password) {
const response = await client.auth.signUp({ email, password });
return response.user;
}
export async function signInUser(email, password) {
const response = await client.auth.signIn({ email, password });
return response.user;
}
export async function logout() {
await client.auth.signOut();
return (window.location.href = '../');
}
export async function home() {
return (window.location.href = '../');
}
export async function communityBoard() {
return (window.location.href = '../community-board');
}
// Region-page Functions
export async function getRegions() {
const response = await client.from('Regions')
.select('*');
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
export async function getRegionName(id) {
const response = await client.from('Regions')
.select('*')
.match({ id: id });
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
export async function getTacoShops(id) {
const response = await client.from('Shop_info')
.select('*')
.match({ region_id: id });
if (response.error) {
alert('Please sign in to view taco shops :)');
console.error(response.error.message);
} else {
return response.data;
}
}
// Shop-info Functions
export async function getShopInfo(id) {
const response = await client.from('Shop_info')
.select('*')
.match({ id })
.single();
return response.data;
}
// Displays reviews on the page
export async function getReviews(id) {
const response = await client.from('user_review')
.select('*')
.match({ shop_id: id })
.order('created_at');
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
// Adds reviews to the page
export async function addReview(review) {
const response = await client.from('user_review')
.insert(review);
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
// Deletes reviews from the page
export async function deleteReview(id) {
const response = await client.from('user_review')
.delete()
.eq('id', id);
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
// Community Board functions
// Post/Meetup/Community Functions
export async function fetchPosts() {
const response = await client.from('Community_board')
.select('*')
.order('created_at');
return response.data;
}
export async function createPost(post) {
const response = await client.from('Community_board')
.insert(post);
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
export async function deletePost(id) {
const response = await client.from('Community_board')
.delete()
.eq('id', id);
if (response.error) {
console.error(response.error.message);
} else {
return response.data;
}
}
// // Reply to Post functions
// export async function createReply(participant) {
// const response = await client.from('Participants')
// .insert(participant);
// if (response.error) {
// console.error(response.error.message);
// } else {
// return response.data;
// }
// }
// // Delete reply to post
// export async function deleteReply(id) {
// const response = await client.from('Participants')
// .delete()
// .eq(id);
// if (response.error) {
// console.error(response.error.message);
// } else {
// return response.data;
// }
// }