-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodolistscript.js
More file actions
281 lines (229 loc) · 8.4 KB
/
todolistscript.js
File metadata and controls
281 lines (229 loc) · 8.4 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const listsContainer = document.querySelector('[data-lists]')
const newListForm = document.querySelector('[data-new-list-form]')
const newListInput = document.querySelector('[data-new-list-input]')
const deleteListButton = document.querySelector('[data-delete-list-button]')
const deleteTaskButton = document.querySelector('[data-delete-task-button]')
const listDisplayContainer = document.querySelector('[data-list-display-container]')
const listTitleElement = document.querySelector('[data-list-title]')
const listCountElement = document.querySelector('[data-list-count]')
const tasksContainer = document.querySelector('[data-tasks]')
const taskTemplate = document.getElementById('task-template')
const newTaskForm = document.querySelector('[data-new-task-form]')
const newTaskInput = document.querySelector('[data-new-task-input]')
const clearCompleteTaskButton = document.querySelector('[data-delete-task-button]')
const readToServerButton = document.querySelector('[read-task-button]')
const writeToServerButton = document.querySelector('[write-task-button]')
const emailAddressInputForm = document.querySelector('[storing-address-input]')
let awsConfig = {
"region": "us-east-1",
"endpoint": "http://dynamodb.us-east-1.amazonaws.com",
"accessKeyId": "AKIARD6ATKRQDOIDNRP4",
"secretAccessKey": "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"
};
AWS.config.update(awsConfig);
const LOCAL_STOARGE_LIST_KEY = 'task.lists'
const LOCAL_STOARGE_SELECTED_LIST_ID_KEY = 'task.selectedListID'
let lists = JSON.parse(localStorage.getItem(LOCAL_STOARGE_LIST_KEY)) || []
let selectedListID = localStorage.getItem(LOCAL_STOARGE_SELECTED_LIST_ID_KEY)
listsContainer.addEventListener('click', e=> {
if(e.target.tagName.toLowerCase() === 'li') {
selectedListID = e.target.dataset.listId;
saveAndRender();
}
})
deleteListButton.addEventListener('click', e=>{
lists = lists.filter(list => list.id !== selectedListID)
selectedListID = null
saveAndRender();
})
readToServerButton.addEventListener('click', e=>{
e.preventDefault()
if(emailAddressInputForm.value != null && emailAddressInputForm.value != ""){
readFromServer(emailAddressInputForm.value);
console.log("===================================================================")
}
else{
console.log("no input")// print a message that this can't be done
}
})
writeToServerButton.addEventListener('click', e=>{
e.preventDefault()
if(emailAddressInputForm.value != null && emailAddressInputForm.value != ""){
writeToServer(emailAddressInputForm.value);
console.log("write complete")
}
else{
console.log("no input")// print a message that this can't be done
}
})
emailAddressInputForm.addEventListener('submit', e=>{
e.preventDefault()
console.log(emailAddressInputForm.value)
})
//save the current list into the local storage
function save(){
localStorage.setItem(LOCAL_STOARGE_LIST_KEY, JSON.stringify(lists))
localStorage.setItem(LOCAL_STOARGE_SELECTED_LIST_ID_KEY, selectedListID)
}
function saveAndRender(){
save()
render()
}
//add this fucntion later that allow users to save their information to the cloud
function saveToCloud() {
}
newListForm.addEventListener('submit', e => {
e.preventDefault()
const listName = newListInput.value
if (listName == null || listName === "") return
const list = createList(listName)
newListInput.value = null
lists.push(list)
saveAndRender()
})
newTaskForm.addEventListener('submit', e => {
e.preventDefault()
const taskName = newTaskInput.value
if (taskName == null || taskName === "") return
const task = createTask(taskName)
newTaskInput.value = null
const selectedList = lists.find(list => list.id === selectedListID)
selectedList.tasks.push(task)
saveAndRender()
})
tasksContainer.addEventListener('click', e => {
if (e.target.tagName.toLowerCase() === 'input') {
const selectedList = lists.find(list => list.id === selectedListID)
const selectedTask = selectedList.tasks.find(task => task.id === e.target.id)
selectedTask.complete = e.target.checked
save()
renderTaskCount(selectedList)
}
})
clearCompleteTaskButton.addEventListener('click', e => {
const selectedList = lists.find(list => list.id === selectedListID)
//change the task list into a list that, through filter, only
//have the incomplete tasks left
selectedList.tasks = selectedList.tasks.filter(task => !task.complete)
saveAndRender()
})
function render() {
clearElement(listsContainer)
renderLists()
const selectedList = lists.find(list => list.id === selectedListID)
// No list is selected
if (selectedListID == null){
listDisplayContainer.style.display = "none"
}
// One list is selected
else {
listDisplayContainer.style.display = ""
listTitleElement.innerText = selectedList.name
renderTaskCount(selectedList)
clearElement(tasksContainer)
renderTasks(selectedList)
}
}
function renderLists(){
lists.forEach(list => {
const listElement = document.createElement('li')
listElement.dataset.listId = list.id
listElement.classList.add("list-name")
listElement.innerText = list.name
if(list.id === selectedListID) {
listElement.classList.add('active-list')
}
listsContainer.appendChild(listElement)
})
}
function renderTasks(selectedList) {
selectedList.tasks.forEach(task => {
const taskElement = document.importNode(taskTemplate.content, true)
const checkbox = taskElement.querySelector('input')
checkbox.id = task.id
checkbox.checked = task.complete
const label = taskElement.querySelector('label')
label.htmlFor = task.id
label.append(task.name)
tasksContainer.appendChild(taskElement)
})
}
function renderTaskCount(selectedList) {
const incompleteTasksCount = selectedList.tasks.filter(task => !task.complete).length
const taskString = incompleteTasksCount === 1 ? "task": "tasks"
listCountElement.innerText = `${incompleteTasksCount} ${taskString} remaining`
}
function clearElement(element) {
while(element.firstChild) {
element.removeChild(element.firstChild)
}
}
function createList(name) {
return {id: Date.now().toString(),
name: name,
tasks: []}
}
function createTask(name) {
return {id: Date.now().toString(),
name: name,
complete: false}
}
function readFromServer(email){
//create new DynamoDB
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
//create the parameters and keys to pass into the getItem functions later
const params = {
TableName: "StudyBuddy",
Key: {
"email_id": {S: email}
},
};
function getProperty(){
// Call DynamoDB to get the todolist from table under the given email
return item = ddb.getItem(params).promise();
}
console.log("Got here")
getProperty().then(
function(data) {
console.log('Success', data.Item);
lists = convertToList(data.Item).listOfTasks;
console.log(lists);
saveAndRender();
}).catch(function(err) {
console.log(err);
}
);
}
function writeToServer(email){
//create new DynamoDB
var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
//create the parameters and keys to pass into the getItem functions later
var params = {
TableName: "StudyBuddy",
Item: {
"email_id": {S: email},
"listOfTasks": {L: convertToStructure(lists)}
//AWS.DynamoDB.Converter.marshall(lists)
},
};
// Call DynamoDB to add the todo list to the table under the given email
ddb.putItem(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
console.log(convertToStructure(lists))
}
function convertToStructure(todolist){
let marshalledlist = [];
for(const i in todolist){
marshalledlist.push({M: AWS.DynamoDB.Converter.marshall(todolist[i])});
}
return marshalledlist;
}
function convertToList(dbStructure){
return AWS.DynamoDB.Converter.unmarshall(dbStructure);
}
render()