forked from yallacodecom/ramadan-2020-assessments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
104 lines (91 loc) · 3.13 KB
/
client.js
File metadata and controls
104 lines (91 loc) · 3.13 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
import { debounce } from './debounce.js';
import { renderSingleVidReq } from './renderSingleVidReq.js';
import { checkFormValidity } from './checkFormValidity.js';
import dataService from './dataService.js';
const SUPER_USER_ID = '19980726';
export const state = {
sortBy: 'newFirst',
searchTerm: '',
filterBy: 'all',
userId: '',
isSuperUser: false,
};
// Use DOMContentLoaded event to make sure that the DOM is loaded before running the script
document.addEventListener('DOMContentLoaded', function () {
const videoReqForm = document.getElementById('videoReqForm');
const sortBySelectElms = document.querySelectorAll(`[id*=sort_by_]`);
const searchInputElm = document.getElementById('search-box');
const loginFormElm = document.querySelector('.login-form');
const appContentElm = document.querySelector('.app-content');
const filterByInputsElms = document.querySelectorAll(`[id*=filter_by_]`);
if (window.location.search) {
state.userId = new URLSearchParams(window.location.search).get('id');
if (state.userId === SUPER_USER_ID) {
state.isSuperUser = true;
document.getElementById('normal-user-content').classList.add('d-none');
}
// add d-none class to the login form then remove it from the video request form
loginFormElm.classList.add('d-none');
appContentElm.classList.remove('d-none');
}
dataService.loadAllVidReqs(state);
// send the search value to the server for loading the result
searchInputElm.addEventListener(
'input',
debounce((e) => {
state.searchTerm = e.target.value;
dataService.loadAllVidReqs(
state.sortBy,
state.searchTerm,
state.filterBy
);
}, 300)
);
sortBySelectElms.forEach((elm) => {
elm.addEventListener('click', function (e) {
e.preventDefault();
state.sortBy = this.querySelector('input').value;
dataService.loadAllVidReqs(
state.sortBy,
state.searchTerm,
state.filterBy
);
// Active the current Sort btn
sortBySelectElms.forEach((e) => e.classList.remove('active'));
this.classList.add('active');
});
});
filterByInputsElms.forEach((elm) => {
elm.addEventListener('click', function (e) {
e.preventDefault();
state.filterBy = this.querySelector('input').value;
dataService.loadAllVidReqs(
state.sortBy,
state.searchTerm,
state.filterBy
);
// Active the current Sort btn
filterByInputsElms.forEach((e) => e.classList.remove('active'));
this.classList.add('active');
});
});
videoReqForm.addEventListener('submit', (e) => {
e.preventDefault();
// Get the data from the form to one variable
const formData = new FormData(videoReqForm);
// Send User Id with the request
formData.append('author_id', state.userId);
// Check validation of the form
const isValid = checkFormValidity(formData);
if (!isValid) return;
// Add new video request to the server
dataService
.addNewVidReq(formData)
.then((data) => {
renderSingleVidReq(data, state, true);
})
.catch((err) => {
console.log(err);
});
});
});