From 102c0933d1a748465240193c33fcb66bedf06375 Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Mon, 17 Apr 2023 19:08:13 +0300 Subject: [PATCH 01/14] loft photo --- projects/loft-photo/friends.json | 32 ++ projects/loft-photo/images/arrow-left.svg | 4 + projects/loft-photo/images/button.svg | 17 ++ projects/loft-photo/images/chat.svg | 3 + projects/loft-photo/images/exit.svg | 5 + projects/loft-photo/images/heart-red.svg | 3 + projects/loft-photo/images/heart.svg | 3 + projects/loft-photo/images/logo.svg | 11 + projects/loft-photo/images/send.svg | 3 + projects/loft-photo/images/vert1.svg | 22 ++ projects/loft-photo/images/vert2.svg | 22 ++ projects/loft-photo/images/vert3.svg | 22 ++ projects/loft-photo/index.js | 0 projects/loft-photo/layout.html | 64 ++++ projects/loft-photo/model.js | 9 + projects/loft-photo/photos.json | 72 +++++ projects/loft-photo/server/index.js | 91 ++++++ projects/loft-photo/settings.json | 7 + projects/loft-photo/styles.css | 348 ++++++++++++++++++++++ 19 files changed, 738 insertions(+) create mode 100644 projects/loft-photo/friends.json create mode 100644 projects/loft-photo/images/arrow-left.svg create mode 100644 projects/loft-photo/images/button.svg create mode 100644 projects/loft-photo/images/chat.svg create mode 100644 projects/loft-photo/images/exit.svg create mode 100644 projects/loft-photo/images/heart-red.svg create mode 100644 projects/loft-photo/images/heart.svg create mode 100644 projects/loft-photo/images/logo.svg create mode 100644 projects/loft-photo/images/send.svg create mode 100644 projects/loft-photo/images/vert1.svg create mode 100644 projects/loft-photo/images/vert2.svg create mode 100644 projects/loft-photo/images/vert3.svg create mode 100644 projects/loft-photo/index.js create mode 100644 projects/loft-photo/layout.html create mode 100644 projects/loft-photo/model.js create mode 100644 projects/loft-photo/photos.json create mode 100644 projects/loft-photo/server/index.js create mode 100644 projects/loft-photo/settings.json create mode 100644 projects/loft-photo/styles.css diff --git a/projects/loft-photo/friends.json b/projects/loft-photo/friends.json new file mode 100644 index 000000000..b861e0c00 --- /dev/null +++ b/projects/loft-photo/friends.json @@ -0,0 +1,32 @@ +[ + { + "id": 0, + "avatar": "https://via.placeholder.com/100?text=avatar", + "firstName": "Adrian", + "lastName": "Norman" + }, + { + "id": 1, + "avatar": "https://via.placeholder.com/100?text=avatar", + "firstName": "Gail", + "lastName": "Norton" + }, + { + "id": 2, + "avatar": "https://via.placeholder.com/100?text=avatar", + "firstName": "Molina", + "lastName": "Rodgers" + }, + { + "id": 3, + "avatar": "https://via.placeholder.com/100?text=avatar", + "firstName": "Adams", + "lastName": "Parrish" + }, + { + "id": 4, + "avatar": "https://via.placeholder.com/100?text=avatar", + "firstName": "Mercer", + "lastName": "Wiggins" + } +] \ No newline at end of file diff --git a/projects/loft-photo/images/arrow-left.svg b/projects/loft-photo/images/arrow-left.svg new file mode 100644 index 000000000..a4e4c339a --- /dev/null +++ b/projects/loft-photo/images/arrow-left.svg @@ -0,0 +1,4 @@ + + + + diff --git a/projects/loft-photo/images/button.svg b/projects/loft-photo/images/button.svg new file mode 100644 index 000000000..6ce85ea9f --- /dev/null +++ b/projects/loft-photo/images/button.svg @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/projects/loft-photo/images/chat.svg b/projects/loft-photo/images/chat.svg new file mode 100644 index 000000000..fc47d01e1 --- /dev/null +++ b/projects/loft-photo/images/chat.svg @@ -0,0 +1,3 @@ + + + diff --git a/projects/loft-photo/images/exit.svg b/projects/loft-photo/images/exit.svg new file mode 100644 index 000000000..d28c122e1 --- /dev/null +++ b/projects/loft-photo/images/exit.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/projects/loft-photo/images/heart-red.svg b/projects/loft-photo/images/heart-red.svg new file mode 100644 index 000000000..e9985dca6 --- /dev/null +++ b/projects/loft-photo/images/heart-red.svg @@ -0,0 +1,3 @@ + + + diff --git a/projects/loft-photo/images/heart.svg b/projects/loft-photo/images/heart.svg new file mode 100644 index 000000000..4bcdacd80 --- /dev/null +++ b/projects/loft-photo/images/heart.svg @@ -0,0 +1,3 @@ + + + diff --git a/projects/loft-photo/images/logo.svg b/projects/loft-photo/images/logo.svg new file mode 100644 index 000000000..12685673d --- /dev/null +++ b/projects/loft-photo/images/logo.svg @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/projects/loft-photo/images/send.svg b/projects/loft-photo/images/send.svg new file mode 100644 index 000000000..5a55b025c --- /dev/null +++ b/projects/loft-photo/images/send.svg @@ -0,0 +1,3 @@ + + + diff --git a/projects/loft-photo/images/vert1.svg b/projects/loft-photo/images/vert1.svg new file mode 100644 index 000000000..d5d86e658 --- /dev/null +++ b/projects/loft-photo/images/vert1.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/loft-photo/images/vert2.svg b/projects/loft-photo/images/vert2.svg new file mode 100644 index 000000000..0f5e75ed2 --- /dev/null +++ b/projects/loft-photo/images/vert2.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/loft-photo/images/vert3.svg b/projects/loft-photo/images/vert3.svg new file mode 100644 index 000000000..7b481af03 --- /dev/null +++ b/projects/loft-photo/images/vert3.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/projects/loft-photo/index.js b/projects/loft-photo/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/projects/loft-photo/layout.html b/projects/loft-photo/layout.html new file mode 100644 index 000000000..89845cfed --- /dev/null +++ b/projects/loft-photo/layout.html @@ -0,0 +1,64 @@ + + + + + + Loft Photo + + + +
+ + + +
+ + diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js new file mode 100644 index 000000000..1e31e33b3 --- /dev/null +++ b/projects/loft-photo/model.js @@ -0,0 +1,9 @@ +// eslint-disable-next-line no-unused-vars +import photosDB from './photos.json'; +// eslint-disable-next-line no-unused-vars +import friendsDB from './friends.json'; + +export default { + getRandomElement(array) {}, + getNextPhoto() {}, +}; diff --git a/projects/loft-photo/photos.json b/projects/loft-photo/photos.json new file mode 100644 index 000000000..a299e6057 --- /dev/null +++ b/projects/loft-photo/photos.json @@ -0,0 +1,72 @@ +{ + "0": [ + { + "id": 10, + "url": "https://via.placeholder.com/360x680?text=photo 1 for Adrian Norman" + }, + { + "id": 11, + "url": "https://via.placeholder.com/360x680?text=photo 2 for Adrian Norman" + }, + { + "id": 12, + "url": "https://via.placeholder.com/360x680?text=photo 3 for Adrian Norman" + } + ], + "1": [ + { + "id": 20, + "url": "https://via.placeholder.com/360x680?text=photo 1 for Gail Norton" + }, + { + "id": 21, + "url": "https://via.placeholder.com/360x680?text=photo 2 for Gail Norton" + }, + { + "id": 22, + "url": "https://via.placeholder.com/360x680?text=photo 3 for Gail Norton" + } + ], + "2": [ + { + "id": 30, + "url": "https://via.placeholder.com/360x680?text=photo 1 for Molina Rodgers" + }, + { + "id": 31, + "url": "https://via.placeholder.com/360x680?text=photo 2 for Molina Rodgers" + }, + { + "id": 32, + "url": "https://via.placeholder.com/360x680?text=photo 3 for Molina Rodgers" + } + ], + "3": [ + { + "id": 40, + "url": "https://via.placeholder.com/360x680?text=photo 1 for Adams Parrish" + }, + { + "id": 41, + "url": "https://via.placeholder.com/360x680?text=photo 2 for Adams Parrish" + }, + { + "id": 42, + "url": "https://via.placeholder.com/360x680?text=photo 3 for Adams Parrish" + } + ], + "4": [ + { + "id": 50, + "url": "https://via.placeholder.com/360x680?text=photo 1 for Mercer Wiggins" + }, + { + "id": 51, + "url": "https://via.placeholder.com/360x680?text=photo 2 for Mercer Wiggins" + }, + { + "id": 52, + "url": "https://via.placeholder.com/360x680?text=photo 3 for Mercer Wiggins" + } + ] +} \ No newline at end of file diff --git a/projects/loft-photo/server/index.js b/projects/loft-photo/server/index.js new file mode 100644 index 000000000..e9057c6c4 --- /dev/null +++ b/projects/loft-photo/server/index.js @@ -0,0 +1,91 @@ +const http = require('node:http'); +const https = require('node:https'); +const url = require('node:url'); + +const DB = { + tokens: new Map(), + likes: new Map(), + comments: new Map(), +}; + +const methods = { + like(req, res, url, vkUser) { + // todo + }, + photoStats(req, res, url, vkUser) { + // todo + }, + postComment(req, res, url, vkUser, body) { + // todo + }, + getComments(req, res, url) { + // todo + }, +}; + +http + .createServer(async (req, res) => { + console.log('➡️ Поступил запрос:', req.method, req.url); + const token = req.headers['vk_token']; + const parsed = new url.URL(req.url, 'http://localhost'); + const vkUser = await getMe(token); + const body = await readBody(req); + const method = parsed.searchParams.get('method'); + const responseData = await methods[method]?.(req, res, parsed, vkUser, body); + + res.end(JSON.stringify(responseData ?? null)); + }) + .listen('8888', () => { + console.log('🚀 Сервер запущен'); + }); + +async function readBody(req) { + if (req.method === 'GET') { + return null; + } + + return new Promise((resolve) => { + let body = ''; + req + .on('data', (chunk) => { + body += chunk; + }) + .on('end', () => resolve(JSON.parse(body))); + }); +} + +async function getVKUser(token) { + const body = await new Promise((resolve, reject) => + https + .get( + `https://api.vk.com/method/users.get?access_token=${token}&fields=photo_50&v=5.120` + ) + .on('response', (res) => { + let body = ''; + + res.setEncoding('utf8'); + res + .on('data', (chunk) => { + body += chunk; + }) + .on('end', () => resolve(JSON.parse(body))); + }) + .on('error', reject) + ); + + return body.response[0]; +} + +async function getMe(token) { + const existing = DB.tokens.get(token); + + if (existing) { + return existing; + } + + const user = getVKUser(token); + + DB.tokens.set(token, user); + + return user; +} diff --git a/projects/loft-photo/settings.json b/projects/loft-photo/settings.json new file mode 100644 index 000000000..3d20b4405 --- /dev/null +++ b/projects/loft-photo/settings.json @@ -0,0 +1,7 @@ +{ + "proxy": { + "/loft-photo/api/": { + "target": "http://localhost:8888" + } + } +} diff --git a/projects/loft-photo/styles.css b/projects/loft-photo/styles.css new file mode 100644 index 000000000..62d5fba21 --- /dev/null +++ b/projects/loft-photo/styles.css @@ -0,0 +1,348 @@ +/* base */ + +body { + font-family: "Roboto Light", Geneva, Arial, Helvetica, sans-serif; +} + +.hidden { + display: none !important; +} + +a { + text-decoration: none; +} + +/* app */ + +#app { + position: absolute; + top: 0; + bottom: 0; + left: 0; + right: 0; + display: flex; + + align-items: center; + justify-content: center; +} + +.page { + height: 100%; + width: 360px; + position: relative; +} + +/* page login */ + +.page-login { + display: flex; + justify-content: center; + background: #1C1B1F; +} + +.page-login-button { + border: none; + background: url('images/button.svg'); + width: 219px; + height: 40px; + position: absolute; + bottom: 60px; + margin: 0 auto; +} + +.page-login-logo { + top: 429px; + position: absolute; + gap: 16px; + display: flex; + flex-direction: column; + align-items: center; +} + +.page-login-image { + width: 147px; + height: 24px; + background: url('images/logo.svg'); +} + +.page-login-text { + font-size: 14px; + line-height: 20px; + text-align: center; + width: 237px; + color: #B0B0B0; +} + +.page-login-vert1, .page-login-vert2, .page-login-vert3 { + width: 71px; + height: 333px; + position: absolute; +} + +.page-login-vert1 { + top: 59px; + left: 49px; + background: linear-gradient(180deg, rgba(28, 27, 31, 0) 80%, #1C1B1F 100%), url('images/vert1.svg'); +} + +.page-login-vert2 { + top: 81px; + left: 144px; + background: linear-gradient(180deg, rgba(28, 27, 31, 0) 80%, #1C1B1F 100%), url('images/vert2.svg'); +} + +.page-login-vert3 { + top: 59px; + left: 239px; + background: linear-gradient(180deg, rgba(28, 27, 31, 0) 80%, #1C1B1F 100%), url('images/vert3.svg'); +} + +/* page main */ + +.page-main .component-header { + position: absolute; + display: flex; + height: 80px; + top: 0; + left: 0; + right: 0; + background: rgba(0 0 0 / 25%); + padding: 0 24px; +} + +.page-main .component-header-profile-link { + display: flex; + align-items: center; +} + +.page-main .component-header-photo { + width: 40px; + height: 40px; + border-radius: 50%; + flex-shrink: 0; +} + +.page-main .component-header-name { + margin-left: 8px; + font-weight: 400; + font-size: 16px; + color: white; +} + +.page-main .component-footer { + position: absolute; + display: flex; + height: 80px; + bottom: 0; + left: 0; + right: 0; + background: rgba(0 0 0 / 25%); + padding: 0 24px; +} + +.page-main .component-footer-container { + display: flex; + align-items: center; + width: 100%; +} + +.page-main .component-footer-container-profile-link { + margin-left: auto; +} + +.page-main .component-footer-photo { + width: 40px; + height: 40px; + border-radius: 50%; +} + +.page-main .component-footer-container-social-comments, +.page-main .component-footer-container-social-likes { + color: white; + display: flex; + align-items: center; +} + +.page-main .component-footer-container-social-comments:before, +.page-main .component-footer-container-social-likes:before { + display: inline-block; + content: ''; + width: 20px; + height: 20px; + margin-right: 6px; +} + +.page-main .component-footer-container-social-comments:before { + background: url("images/chat.svg"); +} + +.page-main .component-footer-container-social-likes:before { + background: url("images/heart.svg"); + margin-left: 18px; +} + +.page-main .component-footer-container-social-likes.liked:before { + background: url("images/heart-red.svg"); + margin-left: 18px; +} + +.page-main .component-photo { + height: 100%; + width: 360px; + position: relative; + + background-size: cover; + background-position: center; +} + +.component-comments { + position: fixed; + bottom: 0; + left: 0; + right: 0; + top: 0; + background: rgba(0, 0, 0, 0.4); +} + +.component-comments-container { + position: absolute; + display: flex; + flex-direction: column; + top: 50vh; + bottom: 0; + left: 0; + right: 0; + padding: 16px; + border-radius: 28px 28px 0 0; + background: white; +} + +.component-comments-container-title { + font-size: 14px; + text-align: center; + width: 100%; +} + +.component-comments-container-list { + margin-top: 24px; + flex-grow: 1; + display: flex; + gap: 12px; + flex-direction: column; + overflow-y: auto; + margin-bottom: 14px +} + +.component-comments-container-form { + display: flex; + align-items: center; + gap: 16px; + height: 48px; +} + +.component-comments-container-form-input { + box-sizing: border-box; + border: 1px solid #E0E0E0; + border-radius: 32px; + flex-grow: 1; + height: 48px; +} + +.component-comments-container-form-input, +.component-comments-container-form-input, +.component-comments-container-form-input, +.component-comments-container-form-input { + padding: 14px 16px; +} + +.component-comments-container-form-send { + background: url('images/send.svg'); + width: 40px; + height: 40px; +} + +.component-comment { + display: flex; + gap: 8px +} + +.component-comment-photo { + width: 24px; + height: 24px; + border-radius: 50%; + background-position: center; + background-size: cover; +} + +.component-comment-content { + flex-direction: column; +} + +.component-comment-name { + font-size: 12px; +} + +.component-comment-text { + font-size: 14px; +} + +/* page profile */ + +.page-profile { + margin-top: 52px; +} + +.page-profile-back { + background: url('images/arrow-left.svg'); + width: 24px; + height: 24px; + + position: absolute; + left: 24px; +} + +.page-profile-exit { + background: url('images/exit.svg'); + width: 24px; + height: 24px; + + position: absolute; + right: 24px; +} + +.component-user-photos { + display: flex; + flex-wrap: wrap; + gap: 8px; + padding: 24px 16px 16px 16px; +} + +.component-user-photo { + width: 104px; + height: 104px; + background-size: cover; + background-repeat: no-repeat; + background-position: center; +} + +.page-profile .component-user-info { + display: flex; + flex-direction: column; + align-items: center; +} + +.page-profile .component-user-info-photo { + height: 72px; + width: 72px; + border-radius: 50%; + + background-size: cover; + background-position: center; +} + +.page-profile .component-user-info-name { + font-weight: 400; + font-size: 18px; + line-height: 26px; + margin-top: 8px; +} \ No newline at end of file From 5fad2ff00a1bd4dad3c4b68cd0cd069696fa307a Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Sat, 2 Mar 2024 11:44:55 +0300 Subject: [PATCH 02/14] relax ts strictness --- tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index c3de4b67b..e8a24b4ce 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,6 @@ "esModuleInterop": true, "strict": true, "noImplicitAny": true, - "noImplicitReturns": true, "resolveJsonModule": true, "declaration": true, "incremental": true, From f32d0b4563cecf63f923bb2708dc22a4dec1b9f5 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 28 Mar 2024 22:52:54 +0300 Subject: [PATCH 03/14] Week 2 project is done --- projects/loft-photo/index.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/projects/loft-photo/index.js b/projects/loft-photo/index.js index e69de29bb..4654df1b5 100644 --- a/projects/loft-photo/index.js +++ b/projects/loft-photo/index.js @@ -0,0 +1,23 @@ +const friendsDB = require('./friends.json'); +const photosDB = require('./photos.json'); + +const obj = { + getRandomElement: function (array) { + const numb = parseInt(Math.random() * array.length); + return array[numb]; + }, + getNextPhoto: function () { + const randomFriend = this.getRandomElement(friendsDB); + return { + firstName: randomFriend.firstName, + url: this.getRandomElement(photosDB[randomFriend.id]), + }; + }, +}; + +console.log(obj.getRandomElement(friendsDB)); + +const photo = obj.getNextPhoto(); + +console.log(photo.firstName); +console.log(photo.url); From 2ea039efada3b5451f39a635dc4fdf7d72470d53 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 4 Apr 2024 18:14:42 +0300 Subject: [PATCH 04/14] Week 3 project is done --- projects/loft-photo/index.js | 10 ++++++++++ projects/loft-photo/pages.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 projects/loft-photo/pages.js diff --git a/projects/loft-photo/index.js b/projects/loft-photo/index.js index 4654df1b5..90f0842be 100644 --- a/projects/loft-photo/index.js +++ b/projects/loft-photo/index.js @@ -1,3 +1,13 @@ +import pages from './pages'; +import('./styles.css'); + +const pageNames = ['login', 'main', 'profile']; + +document.addEventListener('click', () => { + const pageName = pageNames[parseInt(Math.random() * pageNames.length)]; + pages.openPage(pageName); +}); + const friendsDB = require('./friends.json'); const photosDB = require('./photos.json'); diff --git a/projects/loft-photo/pages.js b/projects/loft-photo/pages.js new file mode 100644 index 000000000..c7eff0a44 --- /dev/null +++ b/projects/loft-photo/pages.js @@ -0,0 +1,20 @@ +const pagesMap = { + login: '.page-login', + main: '.page-main', + profile: '.page-profile', +}; + +export default { + openPage(name) { + const page = pagesMap[name]; + if (page) { + for (const pageName in pagesMap) { + if (pagesMap[pageName] === name) { + document.querySelector(pagesMap[pageName]).classList.remove('hidden'); + } else { + document.querySelector(pagesMap[pageName]).classList.add('hidden'); + } + } + } + }, +}; From be394556608105b7120e2ec27eca43df0d74f83b Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Sun, 14 Apr 2024 00:26:23 +0300 Subject: [PATCH 05/14] Week 4 project is done --- projects/loft-photo/loginPage.js | 15 +++++++++++++ projects/loft-photo/mainPage.js | 38 ++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 projects/loft-photo/loginPage.js create mode 100644 projects/loft-photo/mainPage.js diff --git a/projects/loft-photo/loginPage.js b/projects/loft-photo/loginPage.js new file mode 100644 index 000000000..87cf95640 --- /dev/null +++ b/projects/loft-photo/loginPage.js @@ -0,0 +1,15 @@ +import model from './model'; +import pages from './pages'; +import mainPage from './mainPage'; + +export default { + handleEvents() { + document.querySelector('.page-login-button').addEventListener('click', async () => { + await model.login(); + await model.init(); + + pages.openPage('main'); + await mainPage.getNextPhoto(); + }); + }, +}; diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js new file mode 100644 index 000000000..7a5efeafb --- /dev/null +++ b/projects/loft-photo/mainPage.js @@ -0,0 +1,38 @@ +// eslint-disable-next-line no-unused-vars +import pages from './pages'; +import model from './model'; + +export default { + async getNextPhoto() { + const { friend, id, url } = await model.getNextPhoto(); + this.setFriendAndPhoto(friend, id, url); + }, + + setFriendAndPhoto(friend, id, url) { + const photoComp = document.querySelector('.component-photo'); + const headerPhotoComp = document.querySelector('.component-header-photo'); + const headerNameComp = document.querySelector('.component-header-name'); + + headerPhotoComp.computedStyleMap.backgroundImage = `url('${friend.photo_50}')`; + headerNameComp.innerText = `${friend.first_nane ?? ''} ${friend.last_nane ?? ''}`; + photoComp.style.backgroundImage = `url(${url})`; + }, + + handleEvents() { + let startFrom; + + document.querySelector('.component-photo').addEventListener('touchStart', (e) => { + e.preventDefault(); + + startFrom = { y: e.changedTouches[0].pageY }; + }); + + document.querySelector('.component-photo').addEventListener('touchEnd', async (e) => { + const direction = e.changedTouches[0].pageY - startFrom.y; + + if (direction < 0) { + await this.getNextPhoto(); + } + }); + }, +}; From b0e2e06b6586119e55fb1c230b728ad3ff43cd2d Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Sun, 14 Apr 2024 19:13:40 +0300 Subject: [PATCH 06/14] week 4 project is done --- projects/loft-photo/model.js | 110 +++++++++++++++++++++++++++++++++-- 1 file changed, 104 insertions(+), 6 deletions(-) diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index 1e31e33b3..dac565cdf 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -1,9 +1,107 @@ -// eslint-disable-next-line no-unused-vars -import photosDB from './photos.json'; -// eslint-disable-next-line no-unused-vars -import friendsDB from './friends.json'; +const PERM_FRIENDS = 2; +const PERM_PHOTOS = 4; +const APP_ID = 51901591; export default { - getRandomElement(array) {}, - getNextPhoto() {}, + getRandomElement(array) { + if (!array.length) { + return null; + } + const index = Math.round(Math.random() * (array.length - 1)); + + return array[index]; + }, + + async getNextPhoto() { + const friend = this.getRandomElement(this.friends.items); + const photos = await this.getFriendPhotos(friend.id); + const photo = this.getRandomElement(photos.items); + const size = this.findSize(photo); + + return { friend, id: photo.id, url: size.url }; + }, + + findSize(photo) { + const size = photo.sizes.find((size) => size.width >= 360); + + if (!size) { + return photo.sizes.reduce((biggest, current) => { + if (current.width > biggest.width) { + return current; + } + + return biggest; + }, photo.sizes[0]); + } + + return size; + }, + + async init() { + this.photoCache = {}; + this.friends = await this.getFriends(); + }, + + login() { + return new Promise((resolve, reject) => { + VK.init({ + apiId: APP_ID, + }); + + VK.Auth.login((response) => { + if (response.session) { + resolve(response); + } else { + console.error(response); + reject(response); + } + }, PERM_FRIENDS | PERM_PHOTOS); + }); + }, + + logout() { }, + + callApi(method, params) { + params.v = params.v || '5.120'; + + return new Promise((resolve, reject) => { + VK.api(method, params, (response) => { + if (response.error) { + reject(new Error(response.error.error_msg)); + } else { + resolve(response.response); + } + }); + }); + }, + + getFriends() { + const params = { + fields: ['photo_50', 'photo_100'], + }; + + return this.callApi('friends.get', params); + }, + + getPhotos(owner) { + const params = { + owner_id: owner, + }; + + return this.callApi('photos.getAll', params); + }, + + async getFriendPhotos(id) { + let photos = this.photoCache[id]; + + if (photos) { + return photos; + } + + photos = await this.getPhotos(id); + + this.photoCache[id] = photos; + + return photos; + }, }; From 97d95cc3e9907a2031da8c568cd1129ecaa3e39a Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Mon, 15 Apr 2024 10:54:06 +0300 Subject: [PATCH 07/14] Week 4 project is done (ignoring global variable "VK") --- .eslintrc.js | 3 +++ projects/loft-photo/model.js | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.eslintrc.js b/.eslintrc.js index e659833b3..15457868f 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -13,6 +13,9 @@ module.exports = { browser: true, node: true, }, + globals: { + VK: true, + }, rules: { 'prettier/prettier': 'error', 'no-undef': 'error', diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index dac565cdf..6f5f9ce86 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -59,7 +59,7 @@ export default { }); }, - logout() { }, + logout() {}, callApi(method, params) { params.v = params.v || '5.120'; From 7d5e77302dfd6f7c90db9031a0a5562740009775 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 18 Apr 2024 19:04:17 +0300 Subject: [PATCH 08/14] Week 5 project is done --- projects/loft-photo/index.js | 3 ++ projects/loft-photo/mainPage.js | 3 +- projects/loft-photo/model.js | 19 ++++++++++- projects/loft-photo/profilePage.js | 53 ++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 projects/loft-photo/profilePage.js diff --git a/projects/loft-photo/index.js b/projects/loft-photo/index.js index 90f0842be..0811b01e0 100644 --- a/projects/loft-photo/index.js +++ b/projects/loft-photo/index.js @@ -1,4 +1,5 @@ import pages from './pages'; +import profilePage from './profilePage'; import('./styles.css'); const pageNames = ['login', 'main', 'profile']; @@ -25,6 +26,8 @@ const obj = { }, }; +profilePage.handleEvents(); + console.log(obj.getRandomElement(friendsDB)); const photo = obj.getNextPhoto(); diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index 7a5efeafb..b9340a6dc 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -1,6 +1,7 @@ -// eslint-disable-next-line no-unused-vars +/* eslint-disable no-unused-vars */ import pages from './pages'; import model from './model'; +import profilePage from './profilePage'; export default { async getNextPhoto() { diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index 6f5f9ce86..adf16af0a 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -40,6 +40,7 @@ export default { async init() { this.photoCache = {}; this.friends = await this.getFriends(); + [this.me] = await this.getUsers(); }, login() { @@ -59,7 +60,11 @@ export default { }); }, - logout() {}, + logout() { + return new Promise((resolve) => { + return VK.Auth.revokeGrants(resolve); + }); + }, callApi(method, params) { params.v = params.v || '5.120'; @@ -83,6 +88,18 @@ export default { return this.callApi('friends.get', params); }, + getUsers(ids) { + const params = { + fields: ['photo_50', 'photo_100'], + }; + + if (ids) { + params.user_ids = ids; + } + + return this.callApi('users.get', params); + }, + getPhotos(owner) { const params = { owner_id: owner, diff --git a/projects/loft-photo/profilePage.js b/projects/loft-photo/profilePage.js new file mode 100644 index 000000000..f5a29d58c --- /dev/null +++ b/projects/loft-photo/profilePage.js @@ -0,0 +1,53 @@ +import model from './model'; +import mainPage from './mainPage'; +import pages from './pages'; + +export default { + async setUser(user) { + const photoComp = document.querySelector('.component-user-info-photo'); + const nameComp = document.querySelector('.component-user-info-name'); + const photosComp = document.querySelector('.component-user-photos'); + const photos = await model.getPhotos(user.id); + + this.user = user; + + photoComp.style.background = `url('${user.photo_100}')`; + nameComp.innerText = `${user.first_name ?? ''} ${user.last_name ?? ''}`; + photosComp.innerHTML = ''; + + for (const photo of photos.items) { + const size = model.findSize(photo); + const element = document.createElement('div'); + + element.classList.add('component-user-photo'); + element.dataset.id = photo.id; + element.style.backgroundImage = `url(${size.url})`; + photoComp.append(element); + } + }, + + handleEvents() { + document + .querySelector('.component-user-photos') + .addEventListener('click', async (e) => { + if (e.target.classList.contains('component-user-photo')) { + const photoId = e.target.dataset.id; + const friendsPhotos = await model.getFriendsPhotos(this.user.id); + const photo = friendsPhotos.items.find((photo) => photo.id === photoId); + const size = model.findSize(photo); + + mainPage.setFriendAndPhoto(this.user, parseInt(photoId), size.url); + pages.openPage('main'); + } + }); + + document.querySelector('.page-profile-back').addEventListener('click', async () => { + pages.openPage('main'); + }); + + document.querySelector('.page-profile-exit').addEventListener('click', async () => { + await model.logout(); + pages.openPage('login'); + }); + }, +}; From e167de856379e61ac6b87d3ef2bb2050eaaccc29 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 18 Apr 2024 19:57:06 +0300 Subject: [PATCH 09/14] "nane" changed to "name" --- projects/loft-photo/mainPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index b9340a6dc..a1ebd32db 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -15,7 +15,7 @@ export default { const headerNameComp = document.querySelector('.component-header-name'); headerPhotoComp.computedStyleMap.backgroundImage = `url('${friend.photo_50}')`; - headerNameComp.innerText = `${friend.first_nane ?? ''} ${friend.last_nane ?? ''}`; + headerNameComp.innerText = `${friend.first_name ?? ''} ${friend.last_name ?? ''}`; photoComp.style.backgroundImage = `url(${url})`; }, From d99b36989e40a32826dbb11073f9048d91614124 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 18 Apr 2024 21:49:30 +0300 Subject: [PATCH 10/14] fixed errors for page display --- projects/loft-photo/index.js | 35 ++++------------------- projects/loft-photo/mainPage.js | 43 ++++++++++++++++++++++------ projects/loft-photo/model.js | 46 +++++++++++++----------------- projects/loft-photo/pages.js | 21 +++++++------- projects/loft-photo/profilePage.js | 13 +++++---- 5 files changed, 78 insertions(+), 80 deletions(-) diff --git a/projects/loft-photo/index.js b/projects/loft-photo/index.js index 0811b01e0..2ab5dd7b1 100644 --- a/projects/loft-photo/index.js +++ b/projects/loft-photo/index.js @@ -2,35 +2,10 @@ import pages from './pages'; import profilePage from './profilePage'; import('./styles.css'); -const pageNames = ['login', 'main', 'profile']; - -document.addEventListener('click', () => { - const pageName = pageNames[parseInt(Math.random() * pageNames.length)]; - pages.openPage(pageName); -}); - -const friendsDB = require('./friends.json'); -const photosDB = require('./photos.json'); - -const obj = { - getRandomElement: function (array) { - const numb = parseInt(Math.random() * array.length); - return array[numb]; - }, - getNextPhoto: function () { - const randomFriend = this.getRandomElement(friendsDB); - return { - firstName: randomFriend.firstName, - url: this.getRandomElement(photosDB[randomFriend.id]), - }; - }, -}; +import mainPage from './mainPage'; +import loginPage from './loginPage'; +pages.openPage('login'); +loginPage.handleEvents(); +mainPage.handleEvents(); profilePage.handleEvents(); - -console.log(obj.getRandomElement(friendsDB)); - -const photo = obj.getNextPhoto(); - -console.log(photo.firstName); -console.log(photo.url); diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index a1ebd32db..8cab54fc6 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -1,4 +1,3 @@ -/* eslint-disable no-unused-vars */ import pages from './pages'; import model from './model'; import profilePage from './profilePage'; @@ -6,34 +5,60 @@ import profilePage from './profilePage'; export default { async getNextPhoto() { const { friend, id, url } = await model.getNextPhoto(); - this.setFriendAndPhoto(friend, id, url); + const photoStats = await model.photoStats(id); + this.setFriendAndPhoto(friend, id, url, photoStats); }, - setFriendAndPhoto(friend, id, url) { + setFriendAndPhoto(friend, id, url, stats) { const photoComp = document.querySelector('.component-photo'); const headerPhotoComp = document.querySelector('.component-header-photo'); const headerNameComp = document.querySelector('.component-header-name'); + const footerPhotoComp = document.querySelector('.component-footer-photo'); - headerPhotoComp.computedStyleMap.backgroundImage = `url('${friend.photo_50}')`; + this.friend = friend; + this.photoId = id; + + headerPhotoComp.style.backgroundImage = `url('${friend.photo_50}')`; headerNameComp.innerText = `${friend.first_name ?? ''} ${friend.last_name ?? ''}`; photoComp.style.backgroundImage = `url(${url})`; + + footerPhotoComp.style.backgroundImage = `url('${model.me.photo_50}')`; + this.setLikes(stats.likes, stats.liked); + this.setComments(stats.comments); }, handleEvents() { let startFrom; - document.querySelector('.component-photo').addEventListener('touchStart', (e) => { + document.querySelector('.component-photo').addEventListener('touchstart', (e) => { e.preventDefault(); - - startFrom = { y: e.changedTouches[0].pageY }; + startFrom = { + y: e.changedTouches[0].pageY, + }; + startFrom = { + y: e.changedTouches[0].pageY, + }; }); - document.querySelector('.component-photo').addEventListener('touchEnd', async (e) => { + document.querySelector('.component-photo').addEventListener('touchend', async (e) => { const direction = e.changedTouches[0].pageY - startFrom.y; - if (direction < 0) { await this.getNextPhoto(); } }); + + document + .querySelector('.component-header-profile-link') + .addEventListener('click', async () => { + await profilePage.setUser(this.friend); + pages.openPage('profile'); + }); + + document + .querySelector('.component-footer-container-profile-link') + .addEventListener('click', async () => { + await profilePage.setUser(model.me); + pages.openPage('profile'); + }); }, }; diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index adf16af0a..e9d5651d3 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -8,8 +8,7 @@ export default { return null; } const index = Math.round(Math.random() * (array.length - 1)); - - return array[index]; + return array(index); }, async getNextPhoto() { @@ -39,6 +38,7 @@ export default { async init() { this.photoCache = {}; + this.friends = await this.getFriends(); [this.me] = await this.getUsers(); }, @@ -51,6 +51,7 @@ export default { VK.Auth.login((response) => { if (response.session) { + this.token = response.session.sid; resolve(response); } else { console.error(response); @@ -60,15 +61,9 @@ export default { }); }, - logout() { - return new Promise((resolve) => { - return VK.Auth.revokeGrants(resolve); - }); - }, - + photoCache: {}, callApi(method, params) { - params.v = params.v || '5.120'; - + params.v = params.v || '5,199'; return new Promise((resolve, reject) => { VK.api(method, params, (response) => { if (response.error) { @@ -84,30 +79,14 @@ export default { const params = { fields: ['photo_50', 'photo_100'], }; - return this.callApi('friends.get', params); }, - - getUsers(ids) { - const params = { - fields: ['photo_50', 'photo_100'], - }; - - if (ids) { - params.user_ids = ids; - } - - return this.callApi('users.get', params); - }, - getPhotos(owner) { const params = { owner_id: owner, }; - return this.callApi('photos.getAll', params); }, - async getFriendPhotos(id) { let photos = this.photoCache[id]; @@ -121,4 +100,19 @@ export default { return photos; }, + logout() { + return new Promise((resolve) => VK.Auth.revokeGrants(resolve)); + }, + + getUsers(ids) { + const params = { + fields: ['photo_50', 'photo_100'], + }; + + if (ids) { + params.user_ids = ids; + } + + return this.callApi('users.get', params); + }, }; diff --git a/projects/loft-photo/pages.js b/projects/loft-photo/pages.js index c7eff0a44..100d009c5 100644 --- a/projects/loft-photo/pages.js +++ b/projects/loft-photo/pages.js @@ -6,15 +6,16 @@ const pagesMap = { export default { openPage(name) { - const page = pagesMap[name]; - if (page) { - for (const pageName in pagesMap) { - if (pagesMap[pageName] === name) { - document.querySelector(pagesMap[pageName]).classList.remove('hidden'); - } else { - document.querySelector(pagesMap[pageName]).classList.add('hidden'); - } - } - } + const selector = pagesMap[name]; + const click_block = document.querySelector(selector); + const logBlock = document.querySelector('.page-login'); + const mainBlock = document.querySelector('.page-main'); + const profileBlock = document.querySelector('.page-profile'); + + logBlock.classList.add('hidden'); + mainBlock.classList.add('hidden'); + profileBlock.classList.add('hidden'); + + click_block.classList.remove('hidden'); }, }; diff --git a/projects/loft-photo/profilePage.js b/projects/loft-photo/profilePage.js index f5a29d58c..7f137d561 100644 --- a/projects/loft-photo/profilePage.js +++ b/projects/loft-photo/profilePage.js @@ -6,23 +6,26 @@ export default { async setUser(user) { const photoComp = document.querySelector('.component-user-info-photo'); const nameComp = document.querySelector('.component-user-info-name'); - const photosComp = document.querySelector('.component-user-photos'); + const photosComp = document.querySelector('.component-user-photo'); + const photos = await model.getPhotos(user.id); this.user = user; - photoComp.style.background = `url('${user.photo_100}')`; + photoComp.style.backgroundImage = `url('${user.photo_100}')`; nameComp.innerText = `${user.first_name ?? ''} ${user.last_name ?? ''}`; + photosComp.innerHTML = ''; for (const photo of photos.items) { - const size = model.findSize(photo); + const size = model.findeSize(photo); const element = document.createElement('div'); element.classList.add('component-user-photo'); + element.dataset.id = photo.id; element.style.backgroundImage = `url(${size.url})`; - photoComp.append(element); + photosComp.append(element); } }, @@ -32,7 +35,7 @@ export default { .addEventListener('click', async (e) => { if (e.target.classList.contains('component-user-photo')) { const photoId = e.target.dataset.id; - const friendsPhotos = await model.getFriendsPhotos(this.user.id); + const friendsPhotos = await model.getPhotos(this.user.id); const photo = friendsPhotos.items.find((photo) => photo.id === photoId); const size = model.findSize(photo); From f721924038567bd4e02561adad84a35a7364bae7 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Sat, 20 Apr 2024 16:33:23 +0300 Subject: [PATCH 11/14] Bug fixes for the application to work --- projects/loft-photo/mainPage.js | 7 +------ projects/loft-photo/model.js | 10 +++++++--- projects/loft-photo/profilePage.js | 7 ++----- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index 8cab54fc6..7281fbed6 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -5,8 +5,7 @@ import profilePage from './profilePage'; export default { async getNextPhoto() { const { friend, id, url } = await model.getNextPhoto(); - const photoStats = await model.photoStats(id); - this.setFriendAndPhoto(friend, id, url, photoStats); + this.setFriendAndPhoto(friend, id, url); }, setFriendAndPhoto(friend, id, url, stats) { @@ -16,15 +15,11 @@ export default { const footerPhotoComp = document.querySelector('.component-footer-photo'); this.friend = friend; - this.photoId = id; headerPhotoComp.style.backgroundImage = `url('${friend.photo_50}')`; headerNameComp.innerText = `${friend.first_name ?? ''} ${friend.last_name ?? ''}`; photoComp.style.backgroundImage = `url(${url})`; - footerPhotoComp.style.backgroundImage = `url('${model.me.photo_50}')`; - this.setLikes(stats.likes, stats.liked); - this.setComments(stats.comments); }, handleEvents() { diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index e9d5651d3..56eb630d7 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -1,6 +1,10 @@ const PERM_FRIENDS = 2; const PERM_PHOTOS = 4; -const APP_ID = 51901591; +//Мой VKApp_ID +// const APP_ID = 51901591; + +//Преподовательский VKApp_ID +const APP_ID = 5350105; export default { getRandomElement(array) { @@ -8,7 +12,7 @@ export default { return null; } const index = Math.round(Math.random() * (array.length - 1)); - return array(index); + return array[index]; }, async getNextPhoto() { @@ -63,7 +67,7 @@ export default { photoCache: {}, callApi(method, params) { - params.v = params.v || '5,199'; + params.v = params.v || '5.199'; return new Promise((resolve, reject) => { VK.api(method, params, (response) => { if (response.error) { diff --git a/projects/loft-photo/profilePage.js b/projects/loft-photo/profilePage.js index 7f137d561..4f2c33e4a 100644 --- a/projects/loft-photo/profilePage.js +++ b/projects/loft-photo/profilePage.js @@ -6,23 +6,20 @@ export default { async setUser(user) { const photoComp = document.querySelector('.component-user-info-photo'); const nameComp = document.querySelector('.component-user-info-name'); - const photosComp = document.querySelector('.component-user-photo'); - + const photosComp = document.querySelector('.component-user-photos'); const photos = await model.getPhotos(user.id); this.user = user; photoComp.style.backgroundImage = `url('${user.photo_100}')`; nameComp.innerText = `${user.first_name ?? ''} ${user.last_name ?? ''}`; - photosComp.innerHTML = ''; for (const photo of photos.items) { - const size = model.findeSize(photo); + const size = model.findSize(photo); const element = document.createElement('div'); element.classList.add('component-user-photo'); - element.dataset.id = photo.id; element.style.backgroundImage = `url(${size.url})`; photosComp.append(element); From f621cc17c7308911e04602e2bcf96bec1cfa3178 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Sat, 27 Apr 2024 21:23:40 +0300 Subject: [PATCH 12/14] Week 6 project is done --- projects/loft-photo/commentsTemplate.html.hbs | 9 +++ projects/loft-photo/mainPage.js | 75 ++++++++++++++++++- projects/loft-photo/model.js | 50 ++++++++++++- projects/loft-photo/server/index.js | 39 +++++++++- 4 files changed, 165 insertions(+), 8 deletions(-) create mode 100644 projects/loft-photo/commentsTemplate.html.hbs diff --git a/projects/loft-photo/commentsTemplate.html.hbs b/projects/loft-photo/commentsTemplate.html.hbs new file mode 100644 index 000000000..982631d14 --- /dev/null +++ b/projects/loft-photo/commentsTemplate.html.hbs @@ -0,0 +1,9 @@ +{{#each list}} +
+
+
+
{{name}}
+
{{text}}
+
+
+{{/each}} \ No newline at end of file diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index 7281fbed6..c14df0b84 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -1,11 +1,13 @@ import pages from './pages'; import model from './model'; import profilePage from './profilePage'; +import commentsTemplate from './commentsTemplate'; export default { async getNextPhoto() { const { friend, id, url } = await model.getNextPhoto(); - this.setFriendAndPhoto(friend, id, url); + const photoStats = await model.photoStats(id); + this.setFriendAndPhoto(friend, id, url, photoStats); }, setFriendAndPhoto(friend, id, url, stats) { @@ -15,11 +17,14 @@ export default { const footerPhotoComp = document.querySelector('.component-footer-photo'); this.friend = friend; + this.photoId = id; headerPhotoComp.style.backgroundImage = `url('${friend.photo_50}')`; headerNameComp.innerText = `${friend.first_name ?? ''} ${friend.last_name ?? ''}`; photoComp.style.backgroundImage = `url(${url})`; footerPhotoComp.style.backgroundImage = `url('${model.me.photo_50}')`; + this.setLikes(stats.likes, stats.liked); + this.setComments(stats.comments); }, handleEvents() { @@ -55,5 +60,73 @@ export default { await profilePage.setUser(model.me); pages.openPage('profile'); }); + + document + .querySelector('.component-footer-container-social-likes') + .addEventListener('click', async () => { + const { likes, liked } = await model.likePhoto(this.photoId); + this.setLikes(likes, liked); + }); + + document + .querySelector('.component-footer-container-social-comments') + .addEventListener('click', async () => { + document.querySelector('.component-comments').classList.remove('hidden'); + await this.loadComments(this.photoId); + }); + + const input = document.querySelector('.component-comments-container-form-input'); + + document.querySelector('.component-comments').addEventListener('click', async (e) => { + if (e.target === e.currentTarget) { + document.querySelector('.component-comments').classList.add('hidden'); + } + }); + + document + .querySelector('.component-comments-container-form-send') + .addEventListener('click', async () => { + if (input.value.trim().length) { + await model.postComment(this.photoId, input.value.trim()); + input.value = ''; + await this.loadComments(this.photoId); + } + }); + }, + + async loadComments(photo) { + const comments = await model.getComments(photo); + const commentsElements = commentsTemplate({ + list: comments.map((comment) => { + return { + name: `${comment.user.first_name ?? ''} ${comment.user.last_name ?? ''}`, + photo: comment.user.photo_50, + text: comment.text, + }; + }), + }); + + document.querySelector('.component-comments-container-list').innerHTML = ''; + document.querySelector('.component-comments-container-list').append(commentsElements); + this.setComments(comments.length); + }, + + setLikes(total, liked) { + const likesElement = document.querySelector( + '.component-footer-container-social-likes' + ); + likesElement.innerText = total; + if (liked) { + likesElement.classList.add('liked'); + } else { + likesElement.classList.remove('liked'); + } + }, + + setComments(total) { + const likesElement = document.querySelector( + '.component-footer-container-social-comments' + ); + likesElement.innerText = total; }, }; diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index 56eb630d7..8c5486632 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -1,10 +1,10 @@ const PERM_FRIENDS = 2; const PERM_PHOTOS = 4; //Мой VKApp_ID -// const APP_ID = 51901591; +const APP_ID = 51901591; //Преподовательский VKApp_ID -const APP_ID = 5350105; +// const APP_ID = 5350105; export default { getRandomElement(array) { @@ -65,7 +65,6 @@ export default { }); }, - photoCache: {}, callApi(method, params) { params.v = params.v || '5.199'; return new Promise((resolve, reject) => { @@ -91,6 +90,8 @@ export default { }; return this.callApi('photos.getAll', params); }, + + photoCache: {}, async getFriendPhotos(id) { let photos = this.photoCache[id]; @@ -119,4 +120,47 @@ export default { return this.callApi('users.get', params); }, + + async callServer(method, queryParams, body) { + queryParams = { + ...queryParams, + method, + }; + const query = Object.entries(queryParams) + .reduce((all, [name, value]) => { + all.push(`${name}=${encodeURIComponent(value)}`); + return all; + }, []) + .join('&'); + const params = { + headers: { + vk_token: this.token, + }, + }; + + if (body) { + params.method = 'POST'; + params.body = JSON.stringify(body); + } + + const response = await fetch(`/loft-photo/api/?${query}`, params); + + return response.json(); + }, + + async like(photo) { + return this.callServer('like', { photo }); + }, + + async photoStats(photo) { + return this.callServer('photoStats', { photo }); + }, + + async getComments(photo) { + return this.callServer('getComments', { photo }); + }, + + async postComment(photo, text) { + return this.callServer('postComment', { photo }, { text }); + }, }; diff --git a/projects/loft-photo/server/index.js b/projects/loft-photo/server/index.js index e9057c6c4..efa769350 100644 --- a/projects/loft-photo/server/index.js +++ b/projects/loft-photo/server/index.js @@ -10,16 +10,47 @@ const DB = { const methods = { like(req, res, url, vkUser) { - // todo + const photoId = url.searchParams.get('photo'); + let photoLikes = DB.likes.get(photoId); + + if (!photoLikes) { + photoLikes = new Map(); + DB.likes.set(photoId, photoLikes); + } + + if (photoLikes.get(vkUser.Id)) { + photoLikes.delete(vkUser.Id); + return { likes: photoLikes.size, liked: false }; + } + + photoLikes.set(vkUser.Id, true); + return { likes: photoLikes.size, liked: true }; }, photoStats(req, res, url, vkUser) { - // todo + const photoId = url.searchParams.get('photo'); + const photoLikes = DB.likes.get(photoId); + const photoComments = DB.comments.get(photoId); + + return { + likes: photoLikes?.size ?? 0, + liked: photoLikes?.has(vkUser.Id) ?? false, + comments: photoComments?.length ?? 0, + }; }, postComment(req, res, url, vkUser, body) { - // todo + const photoId = url.searchParams.get('photo'); + let photoComments = DB.comments.get(photoId); + + if (!photoComments) { + photoComments = []; + DB.comments.set(photoId, photoComments); + } + + photoComments.unshift({ user: vkUser, text: body.text }); }, getComments(req, res, url) { - // todo + const photoId = url.searchParams.get('photo'); + return DB.comments.get(photoId) ?? []; }, }; From 92cc2a8d0a7bd29e0f0cbca4c90baa80ab57b526 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Sat, 27 Apr 2024 21:32:15 +0300 Subject: [PATCH 13/14] Week 6 project is done --- projects/loft-photo/mainPage.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index c14df0b84..a47fa4839 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -1,7 +1,7 @@ import pages from './pages'; import model from './model'; import profilePage from './profilePage'; -import commentsTemplate from './commentsTemplate'; +import commentsTemplate from './commentsTemplate.html.hbs'; export default { async getNextPhoto() { From 69ef47100a5e7843d297b1632b40e20806393dda Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 2 May 2024 23:35:28 +0300 Subject: [PATCH 14/14] Week 7 project is done --- projects/loft-photo/layout.html | 5 ++ projects/loft-photo/mainPage.js | 2 +- projects/loft-photo/model.js | 7 ++- projects/loft-photo/profilePage.js | 85 +++++++++++++++++++++++++++++- 4 files changed, 95 insertions(+), 4 deletions(-) diff --git a/projects/loft-photo/layout.html b/projects/loft-photo/layout.html index 89845cfed..64f412fc2 100644 --- a/projects/loft-photo/layout.html +++ b/projects/loft-photo/layout.html @@ -57,7 +57,12 @@ +
+ 📸 + 👥 +
+
diff --git a/projects/loft-photo/mainPage.js b/projects/loft-photo/mainPage.js index a47fa4839..31c8446cf 100644 --- a/projects/loft-photo/mainPage.js +++ b/projects/loft-photo/mainPage.js @@ -64,7 +64,7 @@ export default { document .querySelector('.component-footer-container-social-likes') .addEventListener('click', async () => { - const { likes, liked } = await model.likePhoto(this.photoId); + const { likes, liked } = await model.like(this.photoId); this.setLikes(likes, liked); }); diff --git a/projects/loft-photo/model.js b/projects/loft-photo/model.js index 8c5486632..6c47e5460 100644 --- a/projects/loft-photo/model.js +++ b/projects/loft-photo/model.js @@ -78,10 +78,15 @@ export default { }); }, - getFriends() { + getFriends(id) { const params = { fields: ['photo_50', 'photo_100'], }; + + if (id != null) { + params.user_ids = id; + } + return this.callApi('friends.get', params); }, getPhotos(owner) { diff --git a/projects/loft-photo/profilePage.js b/projects/loft-photo/profilePage.js index 4f2c33e4a..3c0abd16a 100644 --- a/projects/loft-photo/profilePage.js +++ b/projects/loft-photo/profilePage.js @@ -6,13 +6,27 @@ export default { async setUser(user) { const photoComp = document.querySelector('.component-user-info-photo'); const nameComp = document.querySelector('.component-user-info-name'); - const photosComp = document.querySelector('.component-user-photos'); - const photos = await model.getPhotos(user.id); + const photosTab = document.querySelector('.component-profile-mode-photos'); + const friendsTab = document.querySelector('.component-profile-mode-friends'); this.user = user; photoComp.style.backgroundImage = `url('${user.photo_100}')`; nameComp.innerText = `${user.first_name ?? ''} ${user.last_name ?? ''}`; + + const mode = localStorage.getItem('loft-photo-profile-mode') ?? '1'; + + if (mode === '1') { + photosTab.click(); + } else { + friendsTab.click(); + } + }, + + async showPhotos() { + const photosComp = document.querySelector('.component-user-photos'); + const photos = await model.getPhotos(this.user.id); + photosComp.innerHTML = ''; for (const photo of photos.items) { @@ -26,6 +40,30 @@ export default { } }, + async showFriends() { + const friendsComp = document.querySelector('.component-user-friends'); + const friends = await model.getFriends(this.user.id); + + friendsComp.innerHTML = ''; + + for (const friend of friends.items) { + const element = document.createElement('div'); + const photoEl = document.createElement('div'); + const nameEl = document.createElement('div'); + + element.classList.add('component-user-friend'); + element.dataset.id = friend.id; + photoEl.classList.add('component-user-friend-photo'); + photoEl.style.backgroundImage = `url('${friend.photo_100}')`; + photoEl.dataset.id = friend.id; + nameEl.classList.add('component-user-friend-name'); + nameEl.innerText = `${friend.first_name ?? ''} ${friend.last_name ?? ''}`; + nameEl.dataset.id = friend.id; + element.append(photoEl, nameEl); + friendsComp.append(element); + } + }, + handleEvents() { document .querySelector('.component-user-photos') @@ -41,6 +79,23 @@ export default { } }); + document + .querySelector('.component-user-friends') + .addEventListener('click', async (e) => { + const friendId = e.target.dataset.id; + + if (friendId) { + const [friend] = await model.getUsers([friendId]); + const friendsPhotos = await model.getPhotos(friendId); + const photo = model.getRandomElement(friendsPhotos.items); + const size = model.findSize(photo); + const photoStats = await model.photoStats(photo.id); + + mainPage.setFriendAndPhoto(friend, parseInt(photo.id), size.url, photoStats); + pages.openPage('main'); + } + }); + document.querySelector('.page-profile-back').addEventListener('click', async () => { pages.openPage('main'); }); @@ -49,5 +104,31 @@ export default { await model.logout(); pages.openPage('login'); }); + + document + .querySelector('.component-profile-mode-photos') + .addEventListener('click', () => { + const photosComp = document.querySelector('.component-user-photos'); + const friendsComp = document.querySelector('.component-user-friends'); + + photosComp.classList.remove('hidden'); + friendsComp.classList.add('hidden'); + + localStorage.setItem('loft-photo-profile-mode', '1'); + this.showPhotos(); + }); + + document + .querySelector('.component-profile-mode-friends') + .addEventListener('click', () => { + const photosComp = document.querySelector('.component-user-photos'); + const friendsComp = document.querySelector('.component-user-friends'); + + photosComp.classList.add('hidden'); + friendsComp.classList.remove('hidden'); + + localStorage.setItem('loft-photo-profile-mode', '2'); + this.showFriends(); + }); }, };