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+ }
0 commit comments