Skip to content

Commit fb0d028

Browse files
committed
add demos
1 parent 862d363 commit fb0d028

File tree

3 files changed

+161
-15
lines changed

3 files changed

+161
-15
lines changed

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/scripts/main.js

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,128 @@
11
// Write chatr code here!
2+
3+
if (false) {
4+
// Promise Review
5+
// Promises are a object that will return a value in the future.
6+
7+
// setTimeout is a async function.
8+
// we "promsified" setTimeout in a promise called saySomethingLater
9+
function saySomethingLater(delay, phrase) {
10+
return new Promise((resolve, reject) => {
11+
setTimeout(() => {
12+
console.log(phrase)
13+
resolve(phrase)
14+
}, delay)
15+
})
16+
}
17+
18+
// to consume our promise `saySomethingLater` we chain a `.then()` method to it.
19+
// `.then()` accepts a callback function. This callback function gets invoked when the promise resolves (completes)
20+
// the argument provided in the .then()'s callback is the value the promise resolves
21+
saySomethingLater(3000, 'monday')
22+
.then((phrase) => {
23+
console.log(`logging from .then the phrase: ${phrase}`);
24+
return 'apples'
25+
})
26+
.then((args) => {
27+
console.log(args)
28+
})
29+
30+
}
31+
32+
33+
// Creating Fetch Requests
34+
35+
// Routes
36+
37+
// GET "/messages" -> Returns all messages (Message Index)
38+
// POST "/messages" -> Create a message (Message Create)
39+
// PATCH "/messages/:id" -> Update a message (Message Update)
40+
// DELETE "/messages/:id" -> Delete a message (Message Delete)
41+
42+
const BASE_URL = 'http://localhost:3434';
43+
44+
const Message = {
45+
all() {
46+
return fetch(`${BASE_URL}/messages`) // GET "/messages"
47+
.then((response) => {
48+
return response.json(); // response.text() is a promise that will will read the response.body and turn it into text
49+
})
50+
},
51+
52+
create(params) {
53+
// POST "/messages"
54+
return fetch(`${BASE_URL}/messages`, {
55+
method: "POST",
56+
headers: {
57+
'Content-Type': 'application/json'
58+
},
59+
body: JSON.stringify(params)
60+
})
61+
},
62+
63+
delete(id) {
64+
return fetch(`${BASE_URL}/messages/${id}`, {
65+
method: "DELETE"
66+
}).then(response => response)
67+
}
68+
}
69+
70+
document.addEventListener('DOMContentLoaded', () => {
71+
const messagesContainer = document.querySelector('#messages');
72+
73+
// grabs all the messages and displays them on the page
74+
function refreshMessages() {
75+
Message.all()
76+
.then((messages) => {
77+
const htmlString = messages.map((m) => {
78+
return (
79+
`<li>
80+
<strong>#${m.id}</strong>
81+
<p>${m.body}</p>
82+
<button data-id="${m.id} "class="delete-msg-btn">
83+
Delete
84+
</button>
85+
</li>
86+
`
87+
)
88+
}).join("");
89+
messagesContainer.innerHTML = htmlString;
90+
})
91+
}
92+
93+
refreshMessages();
94+
95+
// creates a new message and calls "refreshMessages()"
96+
const newMessageForm = document.querySelector('#new-message');
97+
newMessageForm.addEventListener('submit', (e) => {
98+
e.preventDefault();
99+
const form = e.currentTarget;
100+
const formData = new FormData(form);
101+
Message.create({
102+
username: 'brandon',
103+
body: formData.get('body')
104+
})
105+
.then((payload) => refreshMessages());
106+
})
107+
108+
109+
// Delete a message
110+
messagesContainer.addEventListener("click", (event) => {
111+
const target = event.target
112+
console.log(target);
113+
if (target.matches(".delete-msg-btn")) {
114+
const id = target.getAttribute('data-id')
115+
Message.delete(id)
116+
.then(() => refreshMessages())
117+
}
118+
})
119+
})
120+
121+
122+
const PokemonAPI = {
123+
index() {
124+
fetch('https://pokeapi.co/api/v2/pokemon/5')
125+
.then(res => res.json())
126+
.then(payload => console.log(payload));
127+
}
128+
}

routes/messages.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const express = require('express');
22
const router = express.Router();
33
const {index, create, update, destroy} = require('../controllers/messages');
44

5-
router.get('/', index);
6-
router.post('/', create);
7-
router.patch('/:id', update);
8-
router.delete('/:id', destroy);
5+
router.get('/', index); // GET "/messages" -> Returns all messages (Message Index)
6+
router.post('/', create); // POST "/messages" -> Create a message (Message Create)
7+
router.patch('/:id', update); // PATCH "/messages/:id" -> Update a message (Message Update)
8+
router.delete('/:id', destroy); // DELETE "/messages/:id" -> Delete a message (Message Delete)
99

1010
module.exports = router;

0 commit comments

Comments
 (0)