Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const allRidesUrl = 'https://frozen-mesa-95948.herokuapp.com/api/v1/rides';
  2.  
  3.  
  4. const logOut = document.querySelector('#logOut');
  5. logOut.addEventListener('click', signOut);
  6.  
  7. function signOut(event) {
  8.     event.preventDefault();
  9.     sessionStorage.clear();
  10.     alert('You have successfully log out. Good Bye!');
  11.     window.location.replace('index.html');
  12. }
  13.  
  14. const fetchAllRides = {
  15.     method: 'GET',
  16.     mode: 'cors',
  17.     headers: {
  18.         'Access-Control-Allow-Origin': '*',
  19.         Accept: 'application/json, text/plain, */*',
  20.         'Content-type': 'application/json; charset=utf-8',
  21.         authorization: sessionStorage.token,  
  22.     }
  23. };
  24.  
  25. fetch(allRidesUrl, fetchAllRides)
  26.     .then((res) => {
  27.         return res.json();})
  28.     .then((rides) => {
  29.        
  30.         let rideOutput = '';
  31.         if(rides.message === 'You do not have permission to this page.'){
  32.             alert('You do not have permission to this page. Please Login or Sign Up');
  33.             window.location.href = 'index.html';
  34.         }
  35.         rides.rides.map(ride => {
  36.             rideOutput += `
  37.             <div class="column">
  38.             <div class="card" >
  39.             <div class="container">
  40.             <h2>rides</h2>
  41.             <p class="title">
  42.             From: Destination Start Point.
  43.             </p>
  44.             <p>${ride.startpoint}</p>
  45.             <p class="title"> To: Destination Stop Point.
  46.             </p>
  47.             <p>${ride.stoppoint}</p>
  48.             <p>Departure Time: ${ride.departuretime}</p>
  49.             <p>Departure Date: ${ride.departuredate}</p>
  50.             <div style="text-align: center; content: '' ; clear: both; display: flex; justify-content: center; ">
  51.             <button class="button" href ='${ride.id}' aria-controls='${ride.id}' style="border: none; display: inline-block; padding: '8px'; background-color: #000; text-align: center; cusor: pointer; width: 100%; margin-bottom: 20%"  onclick="document.getElementById('${ride.id}').style.display='block'"> View Details</button>
  52.             </div>
  53.             </div>
  54.             </div>
  55.             </div>
  56.             </div>
  57.             `;
  58.             let rideId = ride.id;
  59.             console.log(rideOutput);
  60.             const rideUrl = `https://frozen-mesa-95948.herokuapp.com/api/v1/rides/${rideId}`;
  61.  
  62.             const fetchRide = {
  63.                 method: 'GET',
  64.                 mode: 'cors',
  65.                 headers: {
  66.                     'Access-Control-Allow-Origin': '*',
  67.                     Accept: 'application/json, text/plain, /',
  68.                     'Content-type': 'application/json; charset=utf-8',
  69.                     authorization: sessionStorage.token,  
  70.                 }
  71.             };
  72.            
  73.             fetch(rideUrl, fetchRide)
  74.                 .then((res) => res.json())
  75.                 .then(ride => {
  76.                    
  77.                     let viewOutput = '';
  78.                     //console.log(ride.ride.id);
  79.                    
  80.                     // for (const [key] of Object.entries(rideList)){
  81.                        
  82.                     // if(rideList[key].id === rideId){
  83.                     viewOutput += `
  84.                     <span onclick="document.getElementById('${rideId}').style.display='none'" class="close" title="Close Modal">&times;</span>
  85.                     <div style="overflow-x:auto; background-color: white; padding: 20px; width: 60%; margin: 0 auto;">
  86.                     <table>
  87.                     <h1>Ride Details</h1>
  88.                     <thead>
  89.                     <tr>
  90.                           <th> Rides</th>
  91.                           <th> Details</th>
  92.                         </tr>
  93.                     </thread>
  94.                     <tr>
  95.                     <td> Start destintion </td>
  96.                     <td>${ride.ride.startpoint}</td>
  97.                   </tr>
  98.                   <tr>
  99.                     <td> Final destination </td>
  100.                     <td>${ride.ride.stoppoint}</td>
  101.                   </tr>
  102.                   <tr>
  103.                     <td>Departure Time</td>
  104.                     <td>${ride.ride.departuretime}</td>
  105.                   </tr>
  106.                   <tr>
  107.                     <td>Departure Date</td>
  108.                     <td>${ride.ride.departuredate}</td>
  109.                   </tr>
  110.                     </table>
  111.                     <div style="text-align: center; content: '' ; clear: both; display: flex; justify-content: center; ">
  112.                     <button style="background-color: green; color: white; padding: 10px 22px; margin: 9px 0; border: none; cursor: pointer; width: auto">Join</button>
  113.                     </div>
  114.                     </div>
  115.                    
  116.                     `;
  117.                     console.log(viewOutput);
  118.                     // console.log(ride.ride.id)
  119.                    
  120.                     document.getElementById(`${ride.id}`).innerHTML = viewOutput;
  121.                 });
  122.             //console.log(document.getElementById(`${ride.id}`));      
  123.                
  124.                      
  125.         });
  126.              
  127.        
  128.        
  129.         document.getElementById('allRides').innerHTML = rideOutput;
  130.     });
  131.  
  132.  
  133.  
  134.                
  135. //     let rideId = ride.id;
  136. //     console.log(rideId);
  137. //     requestUrl = `https://frozen-mesa-95948.herokuapp.com/api/v1/rides/${rideId}/requests`;
  138.  
  139. //     function saveRequest(e){
  140. //         e.preventDefault();
  141. //         console.log('====> meme');
  142. //         const postData = {
  143. //             method: 'POST',
  144. //             mode: 'cors',
  145. //             headers: {
  146. //                 'Access-Control-Allow-Origin': '*',
  147. //                 Accept: 'application/json, text/plain, */*',
  148. //                 'Content-type': 'application/json; charset=utf-8',
  149. //                 authorization: sessionStorage.token,
  150. //             }
  151. //         };
  152. //         console.log('====> meme');
  153. //         fetch(requestUrl, postData)
  154. //             .then((res) => {
  155. //                 console.log(res);
  156. //                 return res.json();
  157. //             })
  158. //             .then((request) => {
  159. //                 console.log(request);
  160. //                 return request;
  161. //             })
  162. //             .catch((err) => {
  163. //                 console.log(err);
  164. //                 return err;
  165. //             });
  166. //     }
  167.            
  168. //     const request = document.getElementById('request');
  169. //     request.addEventListener('click', saveRequest);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement