ZaidB477 commited on
Commit
b6aa67a
·
verified ·
1 Parent(s): 8c42b95

Upload sample-code.js

Browse files
Files changed (1) hide show
  1. sample-code.js +74 -0
sample-code.js ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // User Data Management Module
2
+ // TODO: Add error handling for network failures
3
+
4
+ /**
5
+ * Fetches user data from the API
6
+ * @param {string} userId - The user's unique identifier
7
+ * @returns {Promise<Object>} User data object
8
+ */
9
+ async function getUserData(userId) {
10
+ const response = await fetch(`/api/users/${userId}`);
11
+ return await response.json();
12
+ }
13
+
14
+ /**
15
+ * Updates user profile information
16
+ * @param {string} userId - The user's unique identifier
17
+ * @param {Object} data - Updated user data
18
+ */
19
+ async function updateUserProfile(userId, data) {
20
+ // First get the current user data
21
+ const currentData = await getUserData(userId);
22
+
23
+ // Merge with new data
24
+ const updatedData = { ...currentData, ...data };
25
+
26
+ // Send update request
27
+ await fetch(`/api/users/${userId}`, {
28
+ method: 'PUT',
29
+ headers: { 'Content-Type': 'application/json' },
30
+ body: JSON.stringify(updatedData)
31
+ });
32
+ }
33
+
34
+ // TODO: Implement caching mechanism for getUserData
35
+
36
+ /**
37
+ * Display user information on the page
38
+ * @param {string} userId - The user's unique identifier
39
+ */
40
+ async function displayUserInfo(userId) {
41
+ try {
42
+ const userData = await getUserData(userId);
43
+ document.getElementById('userName').textContent = userData.name;
44
+ document.getElementById('userEmail').textContent = userData.email;
45
+ } catch (error) {
46
+ console.error('Failed to display user info:', error);
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Delete user account
52
+ * @param {string} userId - The user's unique identifier
53
+ */
54
+ async function deleteUserAccount(userId) {
55
+ // TODO: Add confirmation dialog
56
+ const userData = await getUserData(userId);
57
+ console.log(`Deleting account for ${userData.name}`);
58
+
59
+ await fetch(`/api/users/${userId}`, {
60
+ method: 'DELETE'
61
+ });
62
+ }
63
+
64
+ // Initialize the application
65
+ document.addEventListener('DOMContentLoaded', () => {
66
+ const userId = sessionStorage.getItem('currentUserId');
67
+ if (userId) {
68
+ displayUserInfo(userId);
69
+ }
70
+ });
71
+
72
+ // Export functions for use in other modules
73
+ export { getUserData, updateUserProfile, displayUserInfo, deleteUserAccount };
74
+