-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.js
181 lines (156 loc) · 5.01 KB
/
API.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
178
179
180
181
import { database, auth } from '@/firebaseConfig'
import { ref, set, get, update} from "firebase/database";
import { createUserWithEmailAndPassword, signInWithEmailAndPassword, signOut, updateProfile } from "firebase/auth";
import { SubjectStopWatch } from './components/Subject';
const getRandomColor = () => {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
};
const pieChartColors = [
'#1f77b4',
'#ff7f0e',
'#2ca02c',
'#d62728',
'#9467bd',
'#8c564b',
'#e377c2',
'#7f7f7f',
'#bcbd22',
'#17becf'
];
const get_color = (index)=>{
return pieChartColors[index % pieChartColors.length];
}
/* API used to communicate with Firebase DB */
// 0. Register / Login / Logout
export async function API_firebase_register(nickname, email, password){
//console.log(nickname, email, password);
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
console.log("Register Send SUCCESSFUL");
const user = auth.currentUser;
await updateProfile(user, {
displayName: nickname,
});
const db_ref = ref(database, 'users/' + user.uid); // database의 users/<user_id>로 접근
set(db_ref,
{
username: user.displayName,
email : user.email,
});
console.log("Register SUCCESSFUL");
return { success: true };
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
console.log("Register FAILED");
return { success: false, errorCode, errorMessage };
}
}
export async function API_firebase_login(email, password) {
try {
const userCredential = await signInWithEmailAndPassword(auth, email, password);
const user = userCredential.user;
const db_ref = ref(database, 'users/' + user.uid); // database의 users/<user_id>로 접근
update(db_ref,
{
username: user.displayName,
email : user.email,
});
console.log("Login SUCCESSFUL");
return { success: true, user };
} catch (error) {
const errorCode = error.code;
const errorMessage = error.message;
console.log("Login FAILED");
return { success: false, errorCode, errorMessage };
}
}
export async function API_firebase_logout() {
try {
await signOut(auth);
console.log("Logout SUCCESSFUL");
return true;
} catch (error) {
console.log("Logout FAILED");
return false;
}
}
// 1. User Registration
export function API_register(user_id, name){
const db_ref = ref(database, 'users/' + user_id); // database의 users/<user_id>로 접근
set(db_ref,
{
username: name,
});
}
// Get Ranking Data
export async function API_get_ranking(){
const user = auth.currentUser;
const db_ref = ref(database, 'users/');
}
// Get Stat Data
export async function API_get_lastweek_stats(year, month, day){
const seconds = [];
const labels = [];
const subjects = [];
const today = new Date(year, month-1, day);
for (let i = 0; i < 7; i++) {
const date = new Date(today);
date.setDate(today.getDate() - (6-i));
const year = date.getFullYear();
const month = date.getMonth() + 1; // 월은 0부터 시작하므로 +1 해줌
const day = date.getDate();
const data = await API_get_stats(year, month, day);
if (data) {
seconds.push(data.seconds);
labels.push(`${month}/${day}`);
subjects.push(data.subjects);
//results.push({ x: `${year}-${month}-${day}`, y: data });
} else{
seconds.push(0);
labels.push(`${month}/${day}`);
subjects.push(null);
//results.push({ x: `${year}-${month}-${day}`, y: 0 });
}
}
// Subject Data
const subject = subjects[6];
const sub = subject?
(
subject.map(
(subject, index) => ({
name: subject.subject, // 과목 이름
seconds: subject.seconds,
color: get_color(index)
}))
):
(null);
console.log(sub);
// return results;
return {labels: labels, data: seconds , subjects:sub};
}
export async function API_get_stats(year, month, day){ // date : YYYY-MM-DD form string
console.log(year, month, day);
const uid = auth.currentUser.uid;
const path = 'users/' + uid + '/data/' + year.toString() + '/' + month.toString() + '/' + day.toString() +'/'
const db_ref = ref(database, path);
try {
const snapshot = await get(db_ref);
if (snapshot.exists()) {
const data = snapshot.val();
console.log("DATA", data);
return data;
} else {
//console.log("No data available");
return null;
}
} catch (error) {
console.error("Error getting data:", error);
throw error;
}
}