Skip to content

Commit 3263c23

Browse files
authored
Exam - 08 April 2020 JS Apps
1 parent 5693bbf commit 3263c23

35 files changed

+1280
-0
lines changed

Exam/config/firebase.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var firebaseConfig = {
2+
apiKey: "AIzaSyA-e6knwqKg8e6r4_0z_PN1YB2YG7Ao9Qg",
3+
authDomain: "exam-30768.firebaseapp.com",
4+
databaseURL: "https://exam-30768.firebaseio.com",
5+
projectId: "exam-30768",
6+
storageBucket: "exam-30768.appspot.com",
7+
messagingSenderId: "534722425550",
8+
appId: "1:534722425550:web:6d733daf66fa664732969f"
9+
};
10+
// Initialize Firebase
11+
firebase.initializeApp(firebaseConfig);

Exam/controllers/home.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import extend from '../utilz/singleUser.js'
2+
export default {
3+
get: {
4+
home(context) {
5+
extend(context).then(function () {
6+
this.partial('../templates/home/homePage.hbs')
7+
context.redirect('#/user/login')
8+
});
9+
}
10+
}
11+
}

Exam/controllers/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import user from './user.js';
2+
import item from './item.js';
3+
import home from './home.js';
4+
5+
export default {
6+
user,
7+
item,
8+
home
9+
}

Exam/controllers/item.js

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
import extend from '../utilz/singleUser.js'
2+
import models from '../models/Index.js'
3+
import docModifier from '../utilz/doc-modifier.js'
4+
5+
// import {
6+
// noteError,
7+
// noteLoading,
8+
// noteSuccess
9+
// } from './notifications.js'
10+
11+
12+
export default {
13+
get: {
14+
//GET ALL ITEMS!
15+
dashboard(context) {
16+
17+
models.Item.getAll()
18+
.then((res) => {
19+
let articles = res.docs.map(docModifier);
20+
21+
let JavaScriptArticles = [];
22+
let CSharpArticles = [];
23+
let JavaArticles = [];
24+
let PytonArticles = [];
25+
26+
for (let i = 0; i < articles.length; i++) {
27+
let categoryArticle = articles[i].category;
28+
29+
if (categoryArticle === 'JavaScript') {
30+
JavaScriptArticles.push(articles[i])
31+
JavaScriptArticles = JavaScriptArticles.sort((a, b) => a.title.localeCompare(b.title))
32+
} else if (categoryArticle === 'C#') {
33+
CSharpArticles.push(articles[i])
34+
CSharpArticles = CSharpArticles.sort((a, b) => a.title.localeCompare(b.title))
35+
} else if (categoryArticle === 'Java') {
36+
JavaArticles.push(articles[i])
37+
JavaArticles = JavaArticles.sort((a, b) => a.title.localeCompare(b.title))
38+
} else if (categoryArticle === 'Pyton') {
39+
PytonArticles.push(articles[i])
40+
PytonArticles = PytonArticles.sort((a, b) => a.title.localeCompare(b.title))
41+
}
42+
}
43+
44+
context.JavaScriptArticles = JavaScriptArticles;
45+
context.JavaArticles = JavaArticles
46+
context.CSharpArticles = CSharpArticles
47+
context.PytonArticles = PytonArticles
48+
49+
extend(context).then(function () {
50+
this.partial('../templates/item/dashboard.hbs')
51+
})
52+
})
53+
.catch((err) => console.error(err))
54+
},
55+
//CREATE FORM FOR NEW ITEM!
56+
create(context) {
57+
extend(context).then(function () {
58+
this.partial('../templates/item/create.hbs')
59+
})
60+
},
61+
//GET A SINGLE ITEM VIEW!
62+
details(context) {
63+
let {
64+
id
65+
} = context.params
66+
67+
models.Item.getSingle(id)
68+
.then((res) => {
69+
let currentArticle = docModifier(res)
70+
context.currentArticle = currentArticle
71+
72+
//Check for author of item!
73+
if (currentArticle.uid !== localStorage.getItem('userId')) {
74+
context.isAuthor = false;
75+
} else {
76+
context.isAuthor = true;
77+
}
78+
79+
extend(context).then(function () {
80+
this.partial('../templates/item/details.hbs')
81+
})
82+
})
83+
.catch((err) => console.error(err))
84+
},
85+
//LOAD EDIT FORM
86+
edit(context) {
87+
88+
let {
89+
id
90+
} = context.params
91+
context.id = id
92+
93+
extend(context).then(function () {
94+
this.partial('../templates/item/edit.hbs')
95+
96+
//LOADING DATE FOR EDITED ITEM!
97+
models.Item.getSingle(id)
98+
.then((res) => {
99+
100+
let article = docModifier(res)
101+
let $title = document.querySelector("#title")
102+
$title.value = article.title
103+
let $category = document.querySelector("#category")
104+
$category.value = article.category
105+
let $content = document.querySelector("#content")
106+
$content.value = article.content
107+
})
108+
})
109+
},
110+
},
111+
post: {
112+
//CREATE NEW ITEM!
113+
create(context) {
114+
115+
let possibleCategories = ['JavaScript', 'C#', 'Java', 'Pyton']
116+
let data = {
117+
...context.params,
118+
uid: localStorage.getItem('userId'),
119+
creator: localStorage.getItem('userEmail')
120+
}
121+
122+
if (possibleCategories.includes(data.category)) {
123+
models.Item.create(data)
124+
.then(() => {
125+
context.redirect('#/article/dashboard')
126+
})
127+
.catch((err) => console.error(err))
128+
} else {
129+
alert('Please choose valid category!')
130+
context.redirect('#/article/create')
131+
}
132+
}
133+
134+
},
135+
// DELETE ITEM
136+
del: {
137+
close(context) {
138+
let {
139+
id
140+
} = context.params
141+
if (confirm('Do you want to delete this article?')) {
142+
models.Item.close(id)
143+
.then((res) => {
144+
145+
context.redirect('#/article/dashboard')
146+
//noteSuccess('You closed the article successfully.')
147+
})
148+
.catch((err) => console.error(err))
149+
}
150+
}
151+
152+
},
153+
//UPDATE ITEM!
154+
put: {
155+
edit(context) {
156+
let {
157+
id,
158+
title,
159+
category,
160+
content
161+
} = context.params
162+
163+
models.Item.getSingle(id)
164+
.then((res) => {
165+
166+
let article = docModifier(res)
167+
article.title = title;
168+
article.category = category;
169+
article.content = content
170+
171+
return models.Item.edit(id, article)
172+
})
173+
.then((res) => {
174+
175+
context.redirect('#/article/dashboard')
176+
//noteSuccess('Trek edited successfully.')
177+
})
178+
.catch((err) => console.error(err))
179+
},
180+
// like(context) {
181+
// let {
182+
// id
183+
// } = context.params
184+
185+
// noteLoading('block')
186+
// models.Item.getSingle(id)
187+
// .then((res) => {
188+
// let trek = docModifier(res)
189+
// trek.likes += 1;
190+
191+
// // if (!cause.donors.includes(localStorage.getItem('userEmail'))) {
192+
// // cause.donors.push(localStorage.getItem('userEmail'))
193+
// // }
194+
// return models.Item.edit(id, trek)
195+
// })
196+
// .then((res) => {
197+
198+
// noteLoading('none')
199+
// context.redirect('#/treks/dashboard')
200+
// noteSuccess('You liked the trek successfully.')
201+
// })
202+
// .catch((err) => console.error(err))
203+
204+
// }
205+
}
206+
}

Exam/controllers/notifications.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
let loadingNot = document.querySelector("#loadingBox"); //notificationBox
2+
let successNot = document.querySelector("#successBox"); //notificationBox
3+
let errorNot = document.querySelector('#errorBox'); // check for id - notifications boxes!
4+
5+
function noteError(message) {
6+
errorNot.textContent = message;
7+
errorNot.style.display = 'block';
8+
errorNot.addEventListener('click', () => {
9+
errorNot.style.display = 'none';
10+
errorNot.textContent = '';
11+
});
12+
13+
setTimeout(() => {
14+
errorNot.textContent = '';
15+
errorNot.style.display = 'none';
16+
return;
17+
18+
}, 5000)
19+
20+
21+
}
22+
23+
function noteLoading(message) {
24+
loadingNot.textContent = 'Loading...';
25+
26+
if (message === 'block') {
27+
loadingNot.style.display = 'block';
28+
} else if (message === 'none') {
29+
loadingNot.style.display = 'none';
30+
}
31+
}
32+
33+
function noteSuccess(message) {
34+
35+
successNot.textContent = message;
36+
successNot.style.display = 'block';
37+
successNot.addEventListener('click', () => {
38+
successNot.style.display = 'none';
39+
successNot.textContent = '';
40+
});
41+
42+
setTimeout(() => {
43+
successNot.textContent = '';
44+
successNot.style.display = 'none';
45+
return;
46+
47+
}, 5000)
48+
}
49+
50+
export {
51+
noteError,
52+
noteLoading,
53+
noteSuccess
54+
}

0 commit comments

Comments
 (0)