forked from COPS-IITBHU/csoc-2020-task-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
208 lines (192 loc) · 5.39 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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();
console.log(firstName);
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.
*/
const user = document.getElementById("inputUsername").value.trim();
const password = document.getElementById("inputPassword").value;
console.log(user, password);
if (user === " " || password === " ")
displayErrorToast("please fill all fields");
else {
displayInfoToast("please wait.....");
const dataAPI = {
username: user,
password: password,
};
$.ajax({
url: API_BASE_URL + "auth/login/",
data: dataAPI,
method: "POST",
success: function (data, status, xhr) {
//alert(status, xhr);
//alert(xhr);
window.localStorage.token = data.token;
//Console.log(window.location.href);
window.location.href = "/";
},
error: function (data, status, xhr) {
displayErrorToast("Invalid login credentials");
},
});
}
}
function addTask() {
/**
* @todo Complete this function.
* @todo 1. Send the request to add the task to the backend server. Authorization:"Token "+localStorage.getItem('token')
* @todo 2. Add the task in the dom.
*/
//console.log("addTask");
//console.log("Token " + localStorage.getItem("token"));
const task = document.getElementById("addTask").value;
//console.log(task);
if (task === " ") displayErrorToast("Please enter a task");
else {
const taskData = {
title: task,
};
$.ajax({
headers: {
Authorization: "Token " + localStorage.getItem("token"),
},
url: API_BASE_URL + "todo/create/",
method: "POST",
data: taskData,
success: function (data, status, xhr) {
displayInfoToast("Task Added Successfully!");
window.location.reload();
},
error: function (xhr, status, data) {
console.log(xhr);
},
});
}
}
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.
*/
$.ajax({
url: API_BASE_URL + "todo/" + id + "/",
method: "DELETE",
headers: {
Authorization: "Token " + localStorage.getItem("token"),
},
success: function (data, status, xhr) {
displayInfoToast("Task deleted Successfully");
window.location.reload();
},
error: function (xhr, status, data) {
console.log(xhr);
displayErrorToast("Some error happened");
},
});
}
function updateTask(id) {
const update = document.getElementById("input-button-" + id).value;
dataAPI = {
title: update,
};
$.ajax({
url: API_BASE_URL + "todo/" + id + "/",
headers: {
Authorization: "Token " + localStorage.getItem("token"),
},
method: "PATCH",
data: dataAPI,
success: function (data, status, xhr) {
displaySuccessToast("Task updated!");
window.location.reload();
},
error: function (xhr, status, data) {
displayErrorToast("Some Error has occured");
},
});
}