-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
124 lines (95 loc) · 3.15 KB
/
Copy pathapp.js
File metadata and controls
124 lines (95 loc) · 3.15 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
// 1 Set Item
// 2 Get Item
// 3 Remove Item
// 4 Clear
// console.log(localStorage);
// Store String , Object , Array
// Always String Is Save In Local Storage
// Save Data In Local Storage
// localStorage.setItem('email','maaz@gmail.com')
// Store Array Or Object
// let todos = ['Hp','Dell','Samsung','Lenovo']
// Javascript Object Notation :JSON
// Convert String To Object Or Array
// let stringTodo = JSON.stringify(todos)
// localStorage.setItem('todos', stringTodo )
// Get Data From Local Storage
// let email = localStorage.getItem('email')
// let todos1 = localStorage.getItem('todos')
// Convert Array Or Object To String
// todos1 = JSON.parse(todos1)
// console.log(todos1);
var login_container = document.getElementById('login_container')
var home_container = document.getElementById('home_container')
var email = document.getElementById('email')
var password = document.getElementById('password')
var user_email = document.getElementById('user_email')
var todo_input = document.getElementById('todo_input')
var list = document.getElementById('list')
function loginUser(){
if(!email.value || !password.value) return alert('Enter Your Email Or Password')
localStorage.setItem('email',email.value)
checkIsUserLogin()
}
function checkIsUserLogin(){
var email = localStorage.getItem('email')
if(email){
login_container.style.display = 'none'
home_container.style.display = 'block'
user_email.innerText = email
}else{
login_container.style.display = 'block'
home_container.style.display = 'none'
}
}
checkIsUserLogin()
function logOut(){
localStorage.removeItem('email')
checkIsUserLogin()
}
function addTodo(){
var email = localStorage.getItem('email')
var obj = {
email : email,
todo : todo_input.value
}
console.log(obj);
saveValueToLocalStorage(obj)
todo_input.value = ''
}
function saveValueToLocalStorage(obj){
var todos = localStorage.getItem('todos')
console.log('Todos From Local Storage',todos);
if(todos){
todos = JSON.parse(todos)
todos.push(obj)
console.log(todos);
localStorage.setItem('todos',JSON.stringify(todos))
}else{
todos = [obj]
console.log(todos);
localStorage.setItem('todos',JSON.stringify(todos))
}
showTodo()
}
function showTodo(){
var todos = localStorage.getItem('todos')
if(todos){
list.innerHTML = ''
todos = JSON.parse(todos)
console.log(todos);
// get current logged-in email
var currentEmail = localStorage.getItem('email')
todos.forEach(function(data, ind){
if(data.email === currentEmail){ // only show todos of logged-in user
var liElement = `
<li class="class="flex justify-between items-center bg-gray-50 border border-gray-200 rounded-lg px-4 py-2 shadow-sm">
<p class="font-medium text-gray-800">${data.todo}</p>
<span class="text-sm text-gray-500">${data.email}</span>
</li>`;
list.innerHTML += liElement
}
})
}
}
showTodo()