-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Expand file tree
/
Copy pathindex.html
More file actions
193 lines (163 loc) · 6.26 KB
/
index.html
File metadata and controls
193 lines (163 loc) · 6.26 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to Notely</title>
</head>
<body class="section">
<h1>Welcome to Notely</h1>
<div id="userCreationContainer" class="section">
<input id="nameField" type="text" placeholder="Enter your name">
<button id="createUserButton" onclick="createUser()">Create User</button>
</div>
<div id="noteSection" class="section" style="display: none;">
<p id="greetingMessage"></p>
<textarea id="newNoteContent"></textarea>
<button id="createNoteButton" onclick="createNote()">Create Note</button>
<h2>Your Notes</h2>
<div id="notes"></div>
<button onclick="logout()">Logout</button>
</div>
<script>
const API_BASE = '/v1';
let currentUserAPIKey = null;
let currentUser = null;
async function createNote() {
if (!currentUser) {
alert('Please create a user first');
return;
}
const noteContent = document.getElementById('newNoteContent').value;
const response = await fetchWithAlert(`${API_BASE}/notes`,
{
method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `ApiKey ${currentUserAPIKey}` },
body: JSON.stringify({ note: noteContent })
});
const note = await response.json();
displayNote(note);
}
async function getUser() {
const response = await fetchWithAlert(`${API_BASE}/users`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } });
return await response.json();
}
async function loadNotes() {
if (!currentUser) {
return;
}
const response = await fetchWithAlert(`${API_BASE}/notes`, { headers: { 'Authorization': `ApiKey ${currentUserAPIKey}` } });
const notes = await response.json();
const notesContainer = document.getElementById('notes');
notesContainer.innerHTML = '';
notes.forEach(note => displayNote(note));
}
function displayNote(note) {
const noteElement = document.createElement('div');
noteElement.className = 'note';
noteElement.textContent = note.note;
document.getElementById('notes').appendChild(noteElement);
}
async function createUser() {
const nameField = document.getElementById('nameField');
const response = await fetchWithAlert(`${API_BASE}/users`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: nameField.value }) // using the value from nameField
});
const user = await response.json();
localStorage.setItem('currentUserAPIKey', user.api_key);
login()
alert(`User Created: ${user.name}`);
// Once a user is created, hide the user creation section and show the note section
document.getElementById('userCreationContainer').style.display = 'none';
document.getElementById('noteSection').style.display = 'flex';
}
function logout() {
localStorage.removeItem('currentUserAPIKey');
currentUser = null;
// When a user logs out, show the user creation section and hide the note section
document.getElementById('userCreationContainer').style.display = 'block';
document.getElementById('noteSection').style.display = 'none';
}
async function login() {
currentUserAPIKey = localStorage.getItem('currentUserAPIKey')
if (!currentUserAPIKey) {
return;
}
const user = await getUser();
currentUser = user;
currentUserAPIKey = user.api_key;
await loadNotes();
// When a user logs in, hide the user creation section and show the note section
document.getElementById('userCreationContainer').style.display = 'none';
document.getElementById('noteSection').style.display = 'flex';
// Display a greeting message
document.getElementById('greetingMessage').textContent = `Hello ${user.name}!`;
}
async function fetchWithAlert(url, options) {
const response = await fetch(url, options);
if (response.status > 299) {
alert(`Error: ${response.status}`);
return;
}
return response;
}
login();
</script>
<style>
:root {
--primary: hsl(235, 86%, 65%);
--primary-light: hsl(235, 88%, 73%);
--dark: #121212;
--light: #E4E4E4;
--grey: #424242;
}
body {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: var(--dark);
color: var(--light);
margin: 0;
padding: 0;
height: 100vh;
}
.section {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
textarea {
width: 300px;
height: 100px;
margin-bottom: 10px;
}
input {
display: block;
padding: 1rem;
}
button {
background-color: var(--primary);
color: var(--light);
border: none;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
transition-duration: 0.4s;
}
button:hover {
background-color: var(--primary-light);
}
.note {
width: 300px;
background-color: var(--grey);
border: 1px solid var(--primary);
padding: 10px;
margin-bottom: 10px;
}
</style>
<!-- your existing CSS code... -->
</body>
</html>