Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.

Commit 776a6b6

Browse files
author
Axel
committed
Add no-modal views
1 parent f44fc8c commit 776a6b6

17 files changed

+164
-80
lines changed

controllers/imageControllers.js

Lines changed: 13 additions & 6 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -51,7 +51,7 @@ exports.recentImages = async (req, res) => {
51
}
51
}
52

52

53
exports.imageForm = (req, res) => {
53
exports.imageForm = (req, res) => {
54-
res.json(req.user.slug)
54+
res.render('share-image', {title: 'Share an Image'})
55
}
55
}
56

56

57
exports.upload = multer({
57
exports.upload = multer({
@@ -151,19 +151,26 @@ exports.addLike = async (req, res, next) => {
151
}
151
}
152

152

153
exports.showLikes = async (req, res) => {
153
exports.showLikes = async (req, res) => {
154-
const img = await Image.findOne( { _id: req.params.id } ).populate('likes');
154+
const likes = []
155

155

156-
const like = img.likes.map(like => {
156+
const img = await Image.findOne( { url: req.params.image } ).populate('likes');
157-
return {
157+
158+
img.likes.map(like => {
159+
return likes.push({
158
name: like.name,
160
name: like.name,
159
username: like.username,
161
username: like.username,
160
slug: like.slug,
162
slug: like.slug,
161
gravatar: like.gravatar,
163
gravatar: like.gravatar,
162
avatar: like.avatar
164
avatar: like.avatar
163-
}
165+
})
164
})
166
})
165

167

166-
res.json(like)
168+
if (req.path.includes('api')) {
169+
res.json(likes)
170+
return;
171+
}
172+
173+
res.render('list', { title: "Likes", list: likes })
167
}
174
}
168

175

169
exports.removeQuestion = async (req, res) => {
176
exports.removeQuestion = async (req, res) => {

controllers/notificationControllers.js

Lines changed: 8 additions & 3 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -40,15 +40,20 @@ exports.addNotification = async (req, res) => {
40
}
40
}
41

41

42
exports.showNotifications = async (req, res) => {
42
exports.showNotifications = async (req, res) => {
43-
const user = await User.findOne({ slug: req.params.user })
43+
const user = await User.findOne({ _id: req.user._id })
44
const notify = user.notifications.reverse();
44
const notify = user.notifications.reverse();
45

45

46-
res.json({notify, user: req.params.user})
46+
if (req.path.includes('api')) {
47+
res.json(notify)
48+
return;
49+
}
50+
51+
res.render('list', { title: "Notifications", list: notify })
47
}
52
}
48

53

49
exports.clearNotifications = async (req, res) => {
54
exports.clearNotifications = async (req, res) => {
50
const user = await User.findOneAndUpdate(
55
const user = await User.findOneAndUpdate(
51-
{ slug: req.params.user },
56+
{ _id: req.user._id },
52
{ notifications: [] }
57
{ notifications: [] }
53
)
58
)
54
res.redirect('back')
59
res.redirect('back')

controllers/userControllers.js

Lines changed: 23 additions & 9 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -45,7 +45,7 @@ exports.updateAccount = async (req, res) => {
45
}
45
}
46

46

47
exports.showLikedImages = async (req, res) => {
47
exports.showLikedImages = async (req, res) => {
48-
const user = await User.findOne({ slug: req.params.user }).populate('likes')
48+
const user = await User.findOne({ _id: req.user._id }).populate('likes')
49
const images = await Image.find({ _id: { $in: user.likes } }).populate('comments')
49
const images = await Image.find({ _id: { $in: user.likes } }).populate('comments')
50

50

51
if (!images.length) {
51
if (!images.length) {
@@ -93,35 +93,49 @@ exports.follow = async (req, res, next) => {
93
}
93
}
94

94

95
exports.showFollowers = async (req, res) => {
95
exports.showFollowers = async (req, res) => {
96+
const followers = []
97+
96
const user = await User.findOne({ slug: req.params.user }).populate('followers')
98
const user = await User.findOne({ slug: req.params.user }).populate('followers')
97

99

98-
const follower = user.followers.map(profile => {
100+
user.followers.map(profile => {
99-
return {
101+
return followers.push({
100
name: profile.name,
102
name: profile.name,
101
username: profile.username,
103
username: profile.username,
102
slug: profile.slug,
104
slug: profile.slug,
103
gravatar: profile.gravatar,
105
gravatar: profile.gravatar,
104
avatar: profile.avatar
106
avatar: profile.avatar
105-
}
107+
})
106
})
108
})
107

109

108-
res.json(follower)
110+
if (req.path.includes('api')) {
111+
res.json(followers);
112+
return;
113+
}
114+
115+
res.render('list', { title: "Followers", list: followers })
109
}
116
}
110

117

111
exports.showFollowing = async (req, res) => {
118
exports.showFollowing = async (req, res) => {
119+
const following = []
120+
112
const user = await User.findOne({ slug: req.params.user }).populate('following')
121
const user = await User.findOne({ slug: req.params.user }).populate('following')
113

122

114-
const following = user.following.map(profile => {
123+
user.following.map(profile => {
115-
return {
124+
return following.push({
116
name: profile.name,
125
name: profile.name,
117
username: profile.username,
126
username: profile.username,
118
slug: profile.slug,
127
slug: profile.slug,
119
gravatar: profile.gravatar,
128
gravatar: profile.gravatar,
120
avatar: profile.avatar
129
avatar: profile.avatar
121-
}
130+
})
122
})
131
})
123

132

124-
res.json(following)
133+
if (req.path.includes('api')) {
134+
res.json(following);
135+
return;
136+
}
137+
138+
res.render('list', { title: "Following", list: following })
125
}
139
}
126

140

127
exports.saveAvatar = async (req, res) => {
141
exports.saveAvatar = async (req, res) => {

public/javascript/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -73,6 +73,6 @@ if (notification) add(notification, 'click', showNotifications)
73
/*
73
/*
74
==== Upload images ====
74
==== Upload images ====
75
*/
75
*/
76-
const upload = getAll('a.upload__image')
76+
const upload = get('a.upload__image')
77
import uploadImage from './modules/uploadImage';
77
import uploadImage from './modules/uploadImage';
78-
if (upload) addEach(upload, "click", uploadImage)
78+
if (upload) add(upload, "click", uploadImage)

public/javascript/modules/handleFollow.js

Lines changed: 2 additions & 2 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -9,7 +9,7 @@ function showFollowers(e) {
9
const ul = get(".contact-list", modal);
9
const ul = get(".contact-list", modal);
10

10

11
e.preventDefault()
11
e.preventDefault()
12-
axios.get(this.href)
12+
axios.get(`/api${this.pathname}`)
13
.then(res => {
13
.then(res => {
14
header.innerHTML = "Followers";
14
header.innerHTML = "Followers";
15
ul.innerHTML = renderModal(res.data).join(" ")
15
ul.innerHTML = renderModal(res.data).join(" ")
@@ -24,7 +24,7 @@ function showFollowing(e) {
24
const ul = get(".contact-list", modal);
24
const ul = get(".contact-list", modal);
25

25

26
e.preventDefault()
26
e.preventDefault()
27-
axios.get(this.href)
27+
axios.get(`/api${this.pathname}`)
28
.then(res => {
28
.then(res => {
29
header.innerHTML = "Following";
29
header.innerHTML = "Following";
30
ul.innerHTML = renderModal(res.data).join(" ")
30
ul.innerHTML = renderModal(res.data).join(" ")

public/javascript/modules/handleLikes.js

Lines changed: 3 additions & 3 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -23,14 +23,14 @@ function showLikes(e) {
23
const modal = get(".modal");
23
const modal = get(".modal");
24
const header = get(".header", modal);
24
const header = get(".header", modal);
25
const ul = get(".contact-list", modal);
25
const ul = get(".contact-list", modal);
26-
26+
27-
axios.get(this.href)
27+
axios.get(`/api${this.pathname}`)
28
.then(res => {
28
.then(res => {
29
header.innerHTML = "Likes";
29
header.innerHTML = "Likes";
30
ul.innerHTML = renderModal(res.data).join(" ")
30
ul.innerHTML = renderModal(res.data).join(" ")
31
showModal()
31
showModal()
32
})
32
})
33-
.catch(error => console.log(error));
33+
.catch(err => console.log(err));
34
}
34
}
35

35

36
export { addLike, showLikes }
36
export { addLike, showLikes }

public/javascript/modules/handleNotifications.js

Lines changed: 15 additions & 13 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -7,29 +7,31 @@ import { showModal } from './modal';
7
const renderModal = (res) => {
7
const renderModal = (res) => {
8
return res.map(data => {
8
return res.map(data => {
9
// Check if the avatar is an original image, a facebook cover or a gravatar
9
// Check if the avatar is an original image, a facebook cover or a gravatar
10-
const checkAvatar = !data.author.avatar ? `${data.author.gravatar}&s=30` : (data.author.avatar.includes("http") ? data.author.avatar : `/uploads/avatar/${data.author.avatar}`);
10+
const { username, slug, avatar } = data.author
11+
const { url, name } = data.image
12+
const checkAvatar = avatar.includes('http') ? avatar : `/uploads/avatar/${avatar}`
11

13

12
const time = moment(data.created).fromNow()
14
const time = moment(data.created).fromNow()
13

15

14
const img = `
16
const img = `
15
<div>
17
<div>
16-
<a href="/p/${data.image.url}" class="profile-image">
18+
<a href="/p/${url}" class="profile-image">
17-
<img src="/uploads/gallery/${data.image.name}" alt="${data.image.name}" class="thumb-img">
19+
<img src="/uploads/gallery/${name}" alt="${name}" class="thumb-img">
18
</a>
20
</a>
19
</div>
21
</div>
20
`
22
`
21

23

22
return `
24
return `
23-
<li class="row" data-user=${data.author.username}>
25+
<li class="row" data-user=${username}>
24-
<a href="/${data.author.slug}" class="img">
26+
<a href="/${slug}" class="img">
25-
<img src="${checkAvatar}" alt="${data.author.username}'s avatar">
27+
<img src="${checkAvatar}" alt="${username}'s avatar">
26
</a>
28
</a>
27
<div class="user-name">
29
<div class="user-name">
28-
<a href="/${data.author.slug}">${data.author.username}</a>
30+
<a href="/${slug}">${username}</a>
29
<span style="color: #262626"> ${data.notify} </span>
31
<span style="color: #262626"> ${data.notify} </span>
30
<span style="display: block; font-size: 12px"> <time datetime="${data.created}"> ${time} </time> </span>
32
<span style="display: block; font-size: 12px"> <time datetime="${data.created}"> ${time} </time> </span>
31
</div>
33
</div>
32-
${data.image.name ? img : ''}
34+
${name ? img : ''}
33
</li>
35
</li>
34
`;
36
`;
35
})
37
})
@@ -42,21 +44,21 @@ function showNotifications(e) {
42
const header = get(".header", modal);
44
const header = get(".header", modal);
43
const ul = get(".contact-list", modal);
45
const ul = get(".contact-list", modal);
44

46

45-
axios.get(this.href)
47+
axios.get(`/api${this.pathname}`)
46
.then(res => {
48
.then(res => {
47-
const { notify, user } = res.data
49+
const notify = res.data
48

50

49
const n = notify.length
51
const n = notify.length
50
const notification = n === 1 ? `${n} new notification` : `${n} new notifications`
52
const notification = n === 1 ? `${n} new notification` : `${n} new notifications`
51

53

52
const headerContent = `
54
const headerContent = `
53
${notification}
55
${notification}
54-
<a href="/api/${user}/notifications/clear"> clear all </a>
56+
<a href="/api/notifications/clear"> clear all </a>
55
`
57
`
56-
header.innerHTML = !notify.length ? 'No new notifications' : headerContent;
58+
header.innerHTML = !n ? 'No new notifications' : headerContent;
57
ul.innerHTML = renderModal(notify).join(" ")
59
ul.innerHTML = renderModal(notify).join(" ")
58
showModal()
60
showModal()
59-
})
61+
}).catch(err => console.log(err))
60
}
62
}
61

63

62
export default showNotifications;
64
export default showNotifications;

public/javascript/modules/modalRemove.js

Lines changed: 1 addition & 3 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -27,7 +27,7 @@ function removeImage(e){
27
e.preventDefault()
27
e.preventDefault()
28
const parent = this.parentNode;
28
const parent = this.parentNode;
29

29

30-
axios.get(this.href)
30+
axios.post(`/api${this.pathname}`)
31
.then(res => renderModal.call(this, parent, `/p/${res.data}/remove-confirm`))
31
.then(res => renderModal.call(this, parent, `/p/${res.data}/remove-confirm`))
32
}
32
}
33

33

@@ -36,8 +36,6 @@ function removeAvatar(e) {
36
e.preventDefault()
36
e.preventDefault()
37
const parent = this.parentNode;
37
const parent = this.parentNode;
38

38

39-
console.log(parent.parentNode);
40-
41
renderModal.call(this, parent, `/api/remove-avatar-confirm`)
39
renderModal.call(this, parent, `/api/remove-avatar-confirm`)
42
}
40
}
43

41

public/javascript/modules/uploadImage.js

Lines changed: 17 additions & 21 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -15,33 +15,29 @@ function preUpload(event) {
15
reader.readAsDataURL(image);
15
reader.readAsDataURL(image);
16
}
16
}
17

17

18-
19
function uploadImage(e) {
18
function uploadImage(e) {
20
e.preventDefault()
19
e.preventDefault()
21

20

22
const modal = get(".modal");
21
const modal = get(".modal");
23
const content = get(".modal__content", modal);
22
const content = get(".modal__content", modal);
24

23

25-
axios.get(this.href)
24+
content.innerHTML = `
26-
.then(res => {
25+
<div class="upload form">
27-
content.innerHTML = `
26+
<p class="upload__message"> Share a new image! </p>
28-
<div class="upload form">
27+
<form class="form__image col" action="/upload" method="POST" enctype="multipart/form-data">
29-
<p class="upload__message"> Share a new image! </p>
28+
<label for="photo" class="upload__label col">
30-
<form class="form__image col" action="/api/${res.data}/upload" method="POST" enctype="multipart/form-data">
29+
<img class="thumb">
31-
<label for="photo" class="upload__label col">
30+
<div class="icon-modal__upload"> ⬆︎ </div>
32-
<img class="thumb">
31+
<input type="file" name="photo" id="photo" accept="image/png, image/jpeg" required />
33-
<div class="icon-modal__upload"> ⬆︎ </div>
32+
</label>
34-
<input type="file" name="photo" id="photo" accept="image/png, image/jpeg" required />
33+
<textarea name="caption" placeholder="Write a caption..." maxlength="140"></textarea>
35-
</label>
34+
<input class="button" type="submit" value="Share"/>
36-
<textarea name="caption" placeholder="Write a caption..." maxlength="140"></textarea>
35+
</form>
37-
<input class="button" type="submit" value="Share"/>
36+
</div>
38-
</form>
37+
`
39-
</div>
38+
showModal()
40-
`
39+
const input = get('input[type="file"]', modal)
41-
showModal()
40+
input.onchange = () => preUpload(event);
42-
const input = get('input[type="file"]', modal)
43-
input.onchange = () => preUpload(event);
44-
})
45

41

46
}
42
}
47

43

routes.js

Lines changed: 15 additions & 12 deletions
Original file line numberOriginal file lineDiff line numberDiff line change
@@ -41,54 +41,57 @@ router.get('/edit', auth.isLoggedIn, user.showUserData);
41
router.post('/edit', catchErrors(user.updateAccount));
41
router.post('/edit', catchErrors(user.updateAccount));
42

42

43
router.get('/p/:image', catchErrors(img.showImage));
43
router.get('/p/:image', catchErrors(img.showImage));
44-
router.get('/p/:image/remove', catchErrors(img.removeQuestion));
44+
router.get('/p/:image/likes', catchErrors(img.showLikes))
45+
router.post('/p/:image/remove', catchErrors(img.removeQuestion));
45
router.get('/p/:image/remove-confirm', auth.isLoggedIn, catchErrors(img.removeImage));
46
router.get('/p/:image/remove-confirm', auth.isLoggedIn, catchErrors(img.removeImage));
46

47

47-
router.get('/:user/likes', auth.isLoggedIn, catchErrors(user.showLikedImages))
48+
router.get('/likes', auth.isLoggedIn, catchErrors(user.showLikedImages))
48

49

50+
router.get('/:user/followers', catchErrors(user.showFollowers))
51+
router.get('/:user/following', catchErrors(user.showFollowing))
49

52

50-
/* API */
53+
router.get('/notifications/', auth.isLoggedIn, catchErrors(notify.showNotifications));
51-
router.get('/api/:user/upload', auth.isLoggedIn, img.imageForm);
54+
52-
router.post('/api/:user/upload',
55+
router.get('/upload', auth.isLoggedIn, img.imageForm);
56+
router.post('/upload',
57+
auth.isLoggedIn,
53
img.upload,
58
img.upload,
54
catchErrors(img.resize),
59
catchErrors(img.resize),
55
catchErrors(img.saveImage)
60
catchErrors(img.saveImage)
56
);
61
);
57

62

63+
/* API */
58
router.post('/api/comment/:id',
64
router.post('/api/comment/:id',
59
catchErrors(comment.addComment),
65
catchErrors(comment.addComment),
60
catchErrors(notify.addNotification)
66
catchErrors(notify.addNotification)
61
);
67
);
62-
63
router.get('/api/comment/:id/remove', auth.isLoggedIn, catchErrors(comment.removeComment));
68
router.get('/api/comment/:id/remove', auth.isLoggedIn, catchErrors(comment.removeComment));
64-
router.get('/api/remove-avatar-confirm', auth.isLoggedIn, catchErrors(user.removeAvatar))
65

69

66
router.get('/api/:user/followers', catchErrors(user.showFollowers))
70
router.get('/api/:user/followers', catchErrors(user.showFollowers))
67
router.get('/api/:user/following', catchErrors(user.showFollowing))
71
router.get('/api/:user/following', catchErrors(user.showFollowing))
68-
69
router.post('/api/:user/follow',
72
router.post('/api/:user/follow',
70
catchErrors(user.findProfile),
73
catchErrors(user.findProfile),
71
catchErrors(user.follow),
74
catchErrors(user.follow),
72
catchErrors(notify.addNotification)
75
catchErrors(notify.addNotification)
73
);
76
);
74

77

75-
router.get('/api/:user/notifications/', catchErrors(notify.showNotifications));
78+
router.get('/api/notifications/', auth.isLoggedIn, catchErrors(notify.showNotifications));
76-
router.get('/api/:user/notifications/clear', catchErrors(notify.clearNotifications));
79+
router.get('/api/notifications/clear', auth.isLoggedIn, catchErrors(notify.clearNotifications));
77

80

78
router.post('/api/like/:id',
81
router.post('/api/like/:id',
79
catchErrors(img.findImg),
82
catchErrors(img.findImg),
80
catchErrors(img.addLike),
83
catchErrors(img.addLike),
81
catchErrors(notify.addNotification)
84
catchErrors(notify.addNotification)
82
);
85
);
83

86

84-
router.get('/api/like/:id/show', catchErrors(img.showLikes))
87+
router.get('/api/p/:image/likes', catchErrors(img.showLikes))
85

88

86
// change avatar
89
// change avatar
87
router.post('/:user',
90
router.post('/:user',
88
img.upload,
91
img.upload,
89
catchErrors(img.makeAvatar),
92
catchErrors(img.makeAvatar),
90
catchErrors(user.saveAvatar)
93
catchErrors(user.saveAvatar)
91
);
94
);
92-
95+
router.get('/api/remove-avatar-confirm', auth.isLoggedIn, catchErrors(user.removeAvatar))
93

96

94
module.exports = router;
97
module.exports = router;

0 commit comments

Comments
 (0)