This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.js
110 lines (96 loc) · 3.2 KB
/
main.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
function displaySuccessToast(message) {
iziToast.success({
title: 'Success',
message: message
});
}
function displayErrorToast(message) {
iziToast.error({
title: 'Error',
message: message
});
}
function displayInfoToast(message) {
iziToast.info({
title: 'Info',
message: message
});
}
const API_BASE_URL = 'https://todo-app-csoc.herokuapp.com/';
function logout() {
localStorage.removeItem('token');
window.location.href = '/login';
}
function registerFieldsAreValid(firstName, lastName, email, username, password) {
if (firstName === '' || lastName === '' || email === '' || username === '' || password === '') {
displayErrorToast("Please fill all the fields correctly.");
return false;
}
if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))) {
displayErrorToast("Please enter a valid email address.")
return false;
}
return true;
}
function register() {
const firstName = document.getElementById('inputFirstName').value.trim();
const lastName = document.getElementById('inputLastName').value.trim();
const email = document.getElementById('inputEmail').value.trim();
const username = document.getElementById('inputUsername').value.trim();
const password = document.getElementById('inputPassword').value;
if (registerFieldsAreValid(firstName, lastName, email, username, password)) {
displayInfoToast("Please wait...");
const dataForApiRequest = {
name: firstName + " " + lastName,
email: email,
username: username,
password: password
}
$.ajax({
url: API_BASE_URL + 'auth/register/',
method: 'POST',
data: dataForApiRequest,
success: function(data, status, xhr) {
localStorage.setItem('token', data.token);
window.location.href = '/';
},
error: function(xhr, status, err) {
displayErrorToast('An account using same email or username is already created');
}
})
}
}
function login() {
/***
* @todo Complete this function.
* @todo 1. Write code for form validation.
* @todo 2. Fetch the auth token from backend and login the user.
*/
}
function addTask() {
/**
* @todo Complete this function.
* @todo 1. Send the request to add the task to the backend server.
* @todo 2. Add the task in the dom.
*/
}
function editTask(id) {
document.getElementById('task-' + id).classList.add('hideme');
document.getElementById('task-actions-' + id).classList.add('hideme');
document.getElementById('input-button-' + id).classList.remove('hideme');
document.getElementById('done-button-' + id).classList.remove('hideme');
}
function deleteTask(id) {
/**
* @todo Complete this function.
* @todo 1. Send the request to delete the task to the backend server.
* @todo 2. Remove the task from the dom.
*/
}
function updateTask(id) {
/**
* @todo Complete this function.
* @todo 1. Send the request to update the task to the backend server.
* @todo 2. Update the task in the dom.
*/
}