Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. app.js
  2. import { get, post, put, del } from './requester.js'
  3.  
  4. const html = {
  5. 'getAllBooks': () => document.getElementById('all-books')
  6. }
  7.  
  8. const actions = {
  9. 'load-books': async function() {
  10. try{
  11. const books = await get('appdata','books');
  12. const booksContainer = html.getAllBooks();
  13. const fragment = document.createDocumentFragment();
  14. books.forEach(b =>{
  15.  
  16. })
  17. } catch(err){
  18. alert(err);
  19. }
  20. },
  21. 'create-book': async function() {}
  22. }
  23.  
  24. function handleEvent(e){
  25. if(typeof actions[e.target.id] === 'function'){
  26. actions[e.target.id]();
  27. }
  28. }
  29.  
  30. (function attachEvenets(){
  31. document.addEventListener('click',handleEvent);
  32. }())
  33. ==========================================================
  34. HTML
  35. <!DOCTYPE html>
  36. <html lang="en">
  37. <head>
  38. <meta charset="UTF-8">
  39. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  40. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  41. <title>Books</title>
  42. <link rel="stylesheet" href="./styles.css">
  43. <script type="module" src="./requester.js"></script>
  44. <script type="module" src="./app.js"></script>
  45.  
  46. </head>
  47. <body>
  48. <button id="load-books">LOAD ALL BOOKS</button>
  49. <table>
  50. <thead>
  51. <tr>
  52. <th>Title</th>
  53. <th>Author</th>
  54. <th>Isbn</th>
  55. <th>Action</th>
  56. </tr>
  57. </thead>
  58. <tbody id="all-books">
  59. <tr>
  60. <td>Harry Poter</td>
  61. <td>J. K. Rowling</td>
  62. <td>0-7475-3269-9</td>
  63. <td>
  64. <button>Edit</button>
  65. <button>Delete</button>
  66. </td>
  67. </tr>
  68. <tr>
  69. <td>Game of Thrones</td>
  70. <td>George R. R. Martin</td>
  71. <td>9780553386790</td>
  72. <td>
  73. <button>Edit</button>
  74. <button>Delete</button>
  75. </td>
  76. </tr>
  77. </tbody>
  78. </table>
  79.  
  80. <form>
  81. <h3>CREATE FORM</h3>
  82. <label>TITLE</label>
  83. <input type="text" id="title" placeholder="Title...">
  84. <label>AUTHOR</label>
  85. <input type="text" id="author" placeholder="Author...">
  86. <label>ISBN</label>
  87. <input type="text" id="isbn" placeholder="Isnb...">
  88. <button id="create-book">Submit</button>
  89. </form>
  90. <form>
  91. <h3>EDIT FORM</h3>
  92. <input type="hidden" value="" />
  93. <label>TITLE</label>
  94. <input type="text" id="edit-title" placeholder="Title...">
  95. <label>AUTHOR</label>
  96. <input type="text" id="edit-author" placeholder="Author...">
  97. <label>ISBN</label>
  98. <input type="text" id="edit-isbn" placeholder="Isnb...">
  99. <button id="edit-book">Submit</button>
  100. </form>
  101. </body>
  102. </html>
  103.  
  104. <!--Feel free to change/update/modify the HTML Structure-->
  105. ===================================================================
  106. request
  107. const username = 'goran1';
  108. const password = 'goran1';
  109.  
  110. const baseURL = 'https://baas.kinvey.com'
  111. const appKey = 'kid_S124GhM3r';
  112. const appSecret = '4e8beb4cafa045d9afef4bcabfb23797';
  113.  
  114.  
  115.  
  116. function makeHeaders(httpMethod, data){
  117. const headers = {
  118. method: httpMethod,
  119. headers: {
  120. 'Authorization': `Basic ${btoa(`${username}:${password}`)}`,
  121. 'Content-Type': 'application/json'
  122. }
  123. }
  124.  
  125. if(httpMethod === 'POST' || httpMethod === 'PUT'){
  126. headers.body = JSON.stringify(data);
  127. }
  128.  
  129. return headers;
  130. }
  131.  
  132. function handleError(e){
  133. if(!e.ok){
  134. throw new Error(e.statusText);
  135. }
  136.  
  137. return e;
  138. }
  139.  
  140. function fetchData(kinveyModule,endPoint,headers){
  141. const url = `${baseURL}/${kinveyModule}/${appKey}/${endPoint}`
  142.  
  143. return fetch(url, headers)
  144. .then(handleError)
  145. .then(serializeData);
  146. }
  147.  
  148. function serializeData(x){
  149. return x.json();
  150. }
  151.  
  152. export function get(kinveyModule, endPoint){
  153. const headers = makeHeaders('GET');
  154.  
  155. return fetchData(kinveyModule,endPoint,headers);
  156. }
  157.  
  158. export function post(kinveyModule, endPoint, data){
  159. const headers = makeHeaders('POST',data);
  160.  
  161. return fetchData(kinveyModule,endPoint,headers);
  162. }
  163.  
  164. export function put(kinveyModule, endPoint, data){
  165. const headers = makeHeaders('PUT',data);
  166.  
  167. return fetchData(kinveyModule,endPoint,headers);
  168. }
  169.  
  170. export function del(kinveyModule,endPoint){
  171. const headers = makeHeaders('DELETE');
  172.  
  173. return fetchData(kinveyModule,endPoint,headers)
  174. }
  175.  
  176. ==============================================================
  177. css
  178. *{
  179. font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
  180. font-size: 20px;
  181. }
  182.  
  183. table{
  184. border:1px solid #234465;
  185. padding: 1%;
  186. margin: 0 auto;
  187. width: 70%;
  188. border-radius: 4px;
  189. }
  190. table th{
  191. text-decoration: underline;
  192. text-align: center;
  193. }
  194. table tbody td{
  195. text-align: center;
  196. border-bottom:1px solid black;
  197. padding: 0.5%;
  198. }
  199.  
  200. table tbody tr:nth-child(even){
  201. background-color: #234465;
  202. color: white
  203. }
  204.  
  205. table tbody tr:nth-child(odd){
  206. color: #234465;
  207. border-bottom: 1px solid black;
  208. }
  209.  
  210. table tbody tr:nth-child(odd) button{
  211. background: #234465;
  212. color:white;
  213. }
  214.  
  215. table tbody tr:nth-child(even) button{
  216. background: white;
  217. color:#234465;
  218. }
  219.  
  220. table tr td:not(:last-child):hover{
  221. text-decoration: underline;
  222. }
  223.  
  224. table button{
  225. border: none;
  226. padding: 5px;
  227. border-radius: 5px;
  228. margin-left: 8%;
  229. }
  230.  
  231. table button:hover{
  232. text-decoration: underline;
  233. }
  234.  
  235. button#load-books{
  236. margin: 0 auto;
  237. display:block;
  238. padding:1%;
  239. margin-top: 1%;
  240. margin-bottom: 1%;
  241. border-radius: 4px;
  242. }
  243.  
  244.  
  245. label, input{
  246. display:block;
  247. margin: 0 auto;
  248. }
  249.  
  250. form input{
  251. margin-bottom: 1%;
  252. width: 25%;
  253. }
  254.  
  255. form{
  256. display: block;
  257. margin: 0 auto;
  258. text-align: center;
  259. }
  260.  
  261. form button{
  262. margin: 0 auto;
  263. display:block;
  264. margin-top: 1%;
  265. margin-bottom: 1%;
  266. border-radius: 4px;
  267. background-color: #234465;
  268. color:white;
  269. border: none;
  270. padding: 0.5%;
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement