Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions data/db.json

This file was deleted.

1 change: 0 additions & 1 deletion details.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<div class="details">
<!-- inject blog details here -->
</div>
<button class="delete">delete</button>

<script src="js/details.js"></script>
</body>
Expand Down
6 changes: 0 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@
<title>JSON Server</title>
</head>
<body>



<nav>
<h1>All Blogs</h1>
<a href="/create.html">Add a new blog</a>
</nav>

<form class="search">
<input type="text" placeholder="search term" name="term">
</form>

<div class="blogs">
<!-- inject blogs here from js -->
</div>
Expand Down
23 changes: 1 addition & 22 deletions js/create.js
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
// javascript for create.html
const form = document.querySelector('form');

const createPost = async (e) => {
e.preventDefault();

const doc = {
title: form.title.value,
body: form.body.value,
likes: 0,
}

await fetch('http://localhost:3000/posts', {
method: 'POST',
body: JSON.stringify(doc),
headers: { 'Content-Type': 'application/json' }
})

window.location.replace('/')
}

form.addEventListener('submit', createPost);
// javascript for create.html
29 changes: 1 addition & 28 deletions js/details.js
Original file line number Diff line number Diff line change
@@ -1,28 +1 @@
// javascript for details.html
const id = new URLSearchParams(window.location.search).get('id');
const container = document.querySelector('.details');
const deleteBtn = document.querySelector('.delete');

const renderDetails = async () => {
const res = await fetch('http://localhost:3000/posts/' + id);
if (!res.ok) {
window.location.replace("/");
}
const post = await res.json();

const template = `
<h1>${post.title}</h1>
<p>${post.body}</p>
`

container.innerHTML = template;
}

deleteBtn.addEventListener('click', async () => {
const res = await fetch('http://localhost:3000/posts/' + id, {
method: 'DELETE'
});
window.location.replace("/");
})

window.addEventListener('DOMContentLoaded', renderDetails);
// javascript for details.html
38 changes: 1 addition & 37 deletions js/index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
// javascript for index.html
const container = document.querySelector('.blogs');
const searchForm = document.querySelector('.search');

const renderPosts = async (term) => {
console.log(term);
let uri = 'http://localhost:3000/posts?_sort=likes&_order=desc';
if (term) {
uri += `&q=${term}`
}

const res = await fetch(uri);
const posts = await res.json();
console.log(posts);

let template = '';
posts.forEach(post => {
template += `
<div class="post">
<h2>${post.title}</h2>
<p><small>${post.likes} likes</small></p>
<p>${post.body.slice(0, 200)}...</p>
<a href="/details.html?id=${post.id}">Read more</a>
</div>
`
});

container.innerHTML = template;
}

// search
searchForm.addEventListener('submit', async (e) => {
e.preventDefault();
renderPosts(searchForm.term.value.trim());
})

window.addEventListener('DOMContentLoaded', () => renderPosts());
// javascript for index.html
17 changes: 0 additions & 17 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,3 @@ input, textarea {
textarea {
min-height:200px;
}

/* post list */
.post {
padding: 16px;
background: white;
border-radius: 10px;
margin: 20px 0;
}
.post h2 {
margin: 0;
}
.post p {
margin-top: 0;
}
.post a {
color: #36cca2;
}