-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
130 lines (118 loc) · 3.93 KB
/
App.js
File metadata and controls
130 lines (118 loc) · 3.93 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
import React, { useState, useEffect } from 'react';
import { signInWithEmailAndPassword, createUserWithEmailAndPassword, signOut, onAuthStateChanged } from "firebase/auth";
import { doc, setDoc, getDoc } from "firebase/firestore";
import { auth, db } from './firebase';
import HabitTracker from './HabitTracker';
import ExerciseTracker from './ExerciseTracker';
const App = () => {
const [user, setUser] = useState(null);
const [error, setError] = useState('');
useEffect(() => {
onAuthStateChanged(auth, (currentUser) => {
setUser(currentUser);
});
// Network status detection
window.addEventListener('online', () => {
console.log('You are now online!');
if (user) {
loadHabitData();
}
});
window.addEventListener('offline', () => {
console.log('You are now offline.');
alert('You are offline. Habit data is unavailable.');
});
return () => {
window.removeEventListener('online', () => {});
window.removeEventListener('offline', () => {});
};
}, [user]);
const handleLogin = async (email, password) => {
try {
await signInWithEmailAndPassword(auth, email, password);
console.log('Logged in successfully');
} catch (error) {
console.error('Login error:', error);
setError('Login failed. Please check your credentials and try again.');
}
};
const handleSignup = async (email, password) => {
try {
await createUserWithEmailAndPassword(auth, email, password);
console.log('Signed up successfully');
} catch (error) {
console.error('Signup error:', error);
if (error.code === 'auth/email-already-in-use') {
setError('This email is already in use. Please use a different email.');
} else {
setError('Signup failed. Please try again.');
}
}
};
const handleLogout = async () => {
try {
await signOut(auth);
console.log('Logged out successfully');
} catch (error) {
console.error('Logout error:', error);
}
};
const saveHabitData = async (data) => {
if (user) {
const userDoc = doc(db, "users", user.uid);
try {
await setDoc(userDoc, { habitData: data }, { merge: true });
console.log('Data saved successfully');
} catch (error) {
console.error('Error saving data:', error);
}
}
};
const loadHabitData = async () => {
if (user) {
const userDoc = doc(db, "users", user.uid);
try {
const docSnap = await getDoc(userDoc);
if (docSnap.exists()) {
console.log('Data loaded successfully:', docSnap.data().habitData);
return docSnap.data().habitData;
} else {
console.log("No such document!");
}
} catch (error) {
if (error.code === 'unavailable') {
console.error('You are offline. Data cannot be loaded.');
alert('You are offline. Habit data is unavailable.');
} else {
console.error('Error loading data:', error);
}
}
}
return null;
};
return (
<div className="App">
{user ? (
<div>
<button onClick={handleLogout}>Logout</button>
<HabitTracker
saveHabitData={saveHabitData}
loadHabitData={loadHabitData}
user={user}
/>
<ExerciseTracker user={user} />
</div>
) : (
<div>
<h1>Login</h1>
{error && <p style={{ color: 'red' }}>{error}</p>}
<input type="email" placeholder="Email" id="email" />
<input type="password" placeholder="Password" id="password" />
<button onClick={() => handleLogin(document.getElementById('email').value, document.getElementById('password').value)}>Login</button>
<button onClick={() => handleSignup(document.getElementById('email').value, document.getElementById('password').value)}>Signup</button>
</div>
)}
</div>
);
};
export default App;