diff --git a/api.js b/api.js index 123a9e3b6..3cd596f58 100644 --- a/api.js +++ b/api.js @@ -1,7 +1,7 @@ // Замени на свой, чтобы получить независимый от других набор данных. // "боевая" версия инстапро лежит в ключе prod const personalKey = "prod"; -const baseHost = "https://webdev-hw-api.vercel.app"; +const baseHost = "https://wedev-api.sky.pro"; const postsHost = `${baseHost}/api/v1/${personalKey}/instapro`; export function getPosts({ token }) { @@ -54,6 +54,25 @@ export function loginUser({ login, password }) { return response.json(); }); } +export function getUserPosts({ id, token }) { + return fetch(postsHost + "/user-posts/" + id, { + method: "GET", + headers: { + Authorization: token, + }, + }) + .then((response) => { + if (response.status === 401) { + throw new Error("Нет авторизации"); + } + + return response.json(); + }) + .then((data) => { + return data.posts; + }); + +} // Загружает картинку в облако, возвращает url загруженной картинки export function uploadImage({ file }) { @@ -67,3 +86,47 @@ export function uploadImage({ file }) { return response.json(); }); } +export function addPost({token, data }) { + return fetch(postsHost, { + method: "POST", + headers: { + Authorization: token, + }, + body: JSON.stringify(data), + }).then((response) => { + if (response.status === 400) { + throw new Error("Некорректные данные"); + } + return response.json(); + }); +} + +export function likePost(id, token) { + return fetch(postsHost+'/'+id+'/like', { + method: "POST", + headers: { + Authorization: token, + }, + + }).then((response) => { + if (response.status === 401) { + throw new Error("Вы не авторизованы"); + } + return response.json(); + }); +} + +export function dislikePost(id, token) { + return fetch(postsHost+'/'+id+'/dislike', { + method: "POST", + headers: { + Authorization: token, + }, + + }).then((response) => { + if (response.status === 401) { + throw new Error("Вы не авторизованы"); + } + return response.json(); + }); +} \ No newline at end of file diff --git a/components/add-post-page-component.js b/components/add-post-page-component.js index 8277f093b..cf2d675f4 100644 --- a/components/add-post-page-component.js +++ b/components/add-post-page-component.js @@ -1,20 +1,55 @@ +import { renderHeaderComponent } from "./header-component.js"; +import { renderUploadImageComponent } from "./upload-image-component.js"; export function renderAddPostPageComponent({ appEl, onAddPostClick }) { + let imageUrl = ""; const render = () => { // @TODO: Реализовать страницу добавления поста const appHtml = `
- Иван Иваныч
-
- - Нравится: 2 -
-- Иван Иваныч - Ромашка, ромашка... -
-- 19 минут назад -
-
- Варварва Н.
+ const postsHtml = posts.map( + (p) => `${p.user.name}
+ - Варварва Н. - Нарисовала картину, посмотрите какая красивая + ${p.user.name} + ${p.description}
- 3 часа назад + ${new Date(p.createdAt).toLocaleDateString()}
-
- Варварва Н.
-- Нравится: 0 -
-- Варварва Н. - Голова -
-- 8 дней назад -
-${p.user.name}
++ Нравится: ${p.likes.length} +
++ ${p.user.name} + ${p.description} +
++ ${new Date(p.createdAt).toLocaleDateString()} +
+