-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
319 lines (262 loc) · 9.32 KB
/
index.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { getS3JSON, putS3JSON } from './modules/s3_interaction.js'
import { logIn, logOut, UserObject } from './modules/login.js'
import './components/upsert-value-modal.js'
import './components/upsert-habit-modal.js'
import './components/value-card.js'
import './components/habit-info-modal.js'
import { downloadObjectAsJson } from './modules/file_utils.js'
import { AwsInteraction } from './modules/aws_config.js'
const s3BucketName = 'selfactualizationtest'
const awsConnection = new AwsInteraction()
const s3 = awsConnection.setupS3()
const poolData = awsConnection.getPoolData()
/*
* TODO: Add store module that allows registering callbacks when its data changes, so we do not have
* to call putS3JSON and updateHabitDisplay every time we modify the data.
*/
let data = {
values: [],
habits: []
}
function jsonPath () {
if (cognitoUserObj.user) {
return cognitoUserObj.getUsername()
} else {
return 'default.json'
}
}
async function loadFromDatabase (s3BucketName) {
return getS3JSON(s3, s3BucketName, jsonPath())
}
async function loadFromDatabaseAndFill () {
try {
data = await loadFromDatabase(s3BucketName)
} catch (ignored) { }
if (data.values === undefined || data.habits === undefined) {
data = {
values: [],
habits: []
}
}
updateHabitsDisplay(data.values, data.habits)
}
const cognitoUserObj = new UserObject(
'show_when_logged_in',
'show_when_logged_out',
'login_status'
)
cognitoUserObj.registerLoginCompletedFunction(loadFromDatabaseAndFill)
// https://stackoverflow.com/questions/43376270/how-to-dynamically-populate-a-list-on-an-html-page
function updateHabitsDisplay (values, habits) {
const container = document.getElementById('value-container')
container.innerHTML = ''
values.forEach(entry => {
const habitsForValue = data.habits.filter(habit => habit.values.includes(entry.name))
container.appendChild(createValueCard(entry, habitsForValue))
})
}
function createValueCard (value, habits) {
const card = document.createElement('saw-value-card')
card.value = value
card.habits = habits
card.addEventListener('saw.valuecard-delete-value-click', e => {
onDeleteValueClicked(value)
})
card.addEventListener('saw.valuecard-edit-value-click', e => {
onEditValueClicked(value, e)
})
card.addEventListener('saw.valuecard-habit-click', e => {
console.log('habit clicked')
onHabitClicked(value, e)
})
card.addEventListener('valuecard-move-up-click', e => {
console.log('move up value clicked')
onMoveValueClicked(value, 'up')
})
card.addEventListener('valuecard-move-down-click', e => {
console.log('move down value clicked')
onMoveValueClicked(value, 'down')
})
return card
}
function openModal (modal) {
modal.addEventListener('saw.modal-close', () => document.body.removeChild(modal))
document.body.appendChild(modal)
}
function openAddValueModal () {
const modal = document.createElement('saw-upsert-value-modal')
modal.addEventListener('saw.modal-submit', onAddValue)
openModal(modal)
}
function openEditValueModal (valueToEdit) {
const modal = document.createElement('saw-upsert-value-modal')
modal.conflictingValueNames =
data.values.filter(val => val !== valueToEdit).map(val => val.name)
modal.setAttribute('prefill-name', valueToEdit.name)
modal.setAttribute('prefill-description', valueToEdit.description)
modal.addEventListener('saw.modal-submit', e =>
onEditValue(valueToEdit, e.detail.input.name, e.detail.input.description))
openModal(modal)
}
function openEditHabitModal (habitToEdit) {
const modal = document.createElement('saw-upsert-habit-modal')
modal.conflictingHabitNames =
data.habits.filter(habit => habit !== habitToEdit).map(habit => habit.name)
modal.setAttribute('prefill-name', habitToEdit.name)
if (habitToEdit.description) {
modal.setAttribute('prefill-description', habitToEdit.description)
}
modal.values = data.values
modal.preselectValues = habitToEdit.values
modal.addEventListener('saw.modal-submit', e => onEditHabit(
habitToEdit, e.detail.input.name, e.detail.input.description, e.detail.input.values
))
openModal(modal)
}
function openAddHabitModal () {
const modal = document.createElement('saw-upsert-habit-modal')
modal.values = data.values
modal.addEventListener('saw.modal-submit', onAddHabit)
openModal(modal)
}
function openHabitInfoModal (value, habit) {
const modal = document.createElement('saw-habit-info-modal')
modal.habit = habit
modal.addEventListener('saw.habitinfomodal-edit-click', e => {
onEditHabitClicked(habit)
})
modal.addEventListener('saw.habitinfomodal-delete-click', e => {
onDeleteHabitClicked(value, habit)
modal.close()
})
openModal(modal)
}
function onAddValue (event) {
data.values.push({
name: event.detail.input.name,
description: event.detail.input.description
})
updateHabitsDisplay(data.values, data.habits)
putS3JSON(s3, s3BucketName, jsonPath(), data)
}
function onEditValue (valueToUpdate, newName, newDescription) {
const affectedHabits = data.habits.filter(habit =>
habit.values.includes(valueToUpdate.name) && valueToUpdate.name !== newName
)
const oldName = valueToUpdate.name
valueToUpdate.name = newName
valueToUpdate.description = newDescription
affectedHabits.forEach(habit => {
const index = habit.values.indexOf(oldName)
if (index !== -1) {
habit.values[index] = newName
}
})
updateHabitsDisplay(data.values, data.habits)
putS3JSON(s3, s3BucketName, jsonPath(), data)
}
function onEditHabit (habitToEdit, newName, newDescription, newValues) {
habitToEdit.name = newName
habitToEdit.description = newDescription
habitToEdit.values = newValues
updateHabitsDisplay(data.values, data.habits)
putS3JSON(s3, s3BucketName, jsonPath(), data)
}
function onAddHabit (event) {
data.habits.push({
name: event.detail.input.name,
description: event.detail.input.description,
values: event.detail.input.values
})
updateHabitsDisplay(data.values, data.habits)
putS3JSON(s3, s3BucketName, jsonPath(), data)
}
function onEditValueClicked (value) {
openEditValueModal(value)
}
function onMoveValueClicked(value, direction) {
const valueName = value.name
const valueIndex = data.values.map(value => value.name).indexOf(value.name)
let targetIndex = -1;
if (direction === 'up') {
if (valueIndex > 0) {
targetIndex = valueIndex - 1
}
}
else if (direction === 'down') {
if (valueIndex < data.values.length - 1) {
targetIndex = valueIndex + 1
}
}
else {
throw new Error(`Unknown direction encountered: ${direction}, expected "up" or "down"`)
}
if (targetIndex !== -1) {
// Swap places
[data.values[valueIndex], data.values[targetIndex]] = [data.values[targetIndex], data.values[valueIndex]]
putS3JSON(s3, s3BucketName, jsonPath(), data)
updateHabitsDisplay(data.values, data.habits)
}
}
function onDeleteValueClicked (value) {
removeIfPresent(data.values, value)
putS3JSON(s3, s3BucketName, jsonPath(), data)
updateHabitsDisplay(data.values, data.habits)
}
function onHabitClicked (value, event) {
openHabitInfoModal(value, event.detail.habit)
}
function onDeleteHabitClicked (value, habit) {
// Unlink the habit from the value
removeIfPresent(habit.values, value.name)
// Remove the habit if it has no more values linking to it
if (habit.values.length === 0) {
removeIfPresent(data.habits, habit)
}
putS3JSON(s3, s3BucketName, jsonPath(), data)
updateHabitsDisplay(data.values, data.habits)
}
function onEditHabitClicked (habit) {
openEditHabitModal(habit)
}
function removeIfPresent (arr, element) {
const index = arr.indexOf(element)
if (index !== -1) {
arr.splice(index, 1)
}
}
function addListeners () {
document.getElementById('login_button').addEventListener('click', async () => {
await logIn(
poolData,
cognitoUserObj,
document.getElementById('email').value,
document.getElementById('password').value
)
})
function fireLogin (event) {
event.preventDefault()
if (event.key === 'Enter') {
document.getElementById('login_button').click()
}
}
document.getElementById('email')
.addEventListener('keyup', fireLogin)
document.getElementById('password')
.addEventListener('keyup', fireLogin)
document.getElementById('logout_button').addEventListener('click', async () => {
await logOut(cognitoUserObj)
loadFromDatabaseAndFill()
})
document.getElementById('open_add_value_modal_button').addEventListener('click', () =>
openAddValueModal()
)
document.getElementById('open_add_habit_modal_button').addEventListener('click', () => {
openAddHabitModal()
})
document.getElementById('download_json').addEventListener('click', () => {
downloadObjectAsJson(data, 'backup')
})
}
addListeners()
window.onload = () => loadFromDatabaseAndFill()