Repository navigation
Expand file tree
/
Copy pathedit-posts.js
More file actions
54 lines (41 loc) · 1.6 KB
/
Copy pathedit-posts.js
File metadata and controls
54 lines (41 loc) · 1.6 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
import { createElement, fetchData, getUrlParam, createAuthorOptionElements, createNewPostElement } from "./functions.js";
async function init() {
const postId = getUrlParam('post_id');
if (!postId) {
const errorMessage = createElement('h1', 'Something went wrong :(');
document.body.prepend(errorMessage);
return;
};
const editPostForm = document.querySelector('#edit-post-form');
const authorSelectElement = editPostForm.querySelector('#author');
const editedPostReview = document.querySelector('#edited-post-review');
createAuthorOptionElements(authorSelectElement);
const post = await fetchData(`https://jsonplaceholder.typicode.com/posts/${postId}`);
const { body, title, userId } = post;
editPostForm.elements.body.value = body;
editPostForm.elements.title.value = title;
editPostForm.elements.author.value = userId;
editPostForm.addEventListener('submit', async (event) => {
event.preventDefault();
const title = event.target.elements.title.value;
const body = event.target.elements.body.value;
const userId = event.target.elements.author.value;
const res = await fetch('https://jsonplaceholder.typicode.com/posts/' + postId, {
method: 'PUT',
body: JSON.stringify({
id: postId,
title,
body,
userId
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
const editedPost = await res.json();
const editedPostElement = await createNewPostElement(editedPost);
editedPostReview.innerHTML = '';
editedPostReview.append(editedPostElement);
})
}
init();