text: (this.webpackJsonpclient=this.webpackJsonpclient||[]).push([[0],{113:function(e,t,n){},123:function(e,t,n){"use strict";n.r(t);var a,r,o,s,c,i=n(0),l=n.n(i),d=n(41),u=n.n(d),j=(n(112),n(113),n(56)),b=n(10),h=n(103),O=n(33),m=n(14),p=n(25),x=n.n(p),g=n(127),v=n(128),k=n(135),f=n(95),w=n(129),S=n(130),y=n(139),I=n(126),C=n(48),B=n(34),F=Object(B.a)(a||(a=Object(C.a)(["\n  mutation addUser($username: String!, $email: String!, $password: String!) {\n    addUser(username: $username, email: $email, password: $password) {\n      token\n      user {\n        _id\n        username\n      }\n    }\n  }\n"]))),T=Object(B.a)(r||(r=Object(C.a)(["\n  mutation login($email: String!, $password: String!) {\n    login(email: $email, password: $password) {\n      token\n      user {\n        _id\n        username\n      }\n    }\n  }\n"]))),L=Object(B.a)(o||(o=Object(C.a)(["\n  mutation saveBook($input: SavedBook!) {\n    saveBook(input: $input) {\n      _id\n      username\n      email\n      bookCount\n      savedBooks {\n        bookId\n        title\n        authors\n        image\n        description\n        link\n      }\n    }\n  }\n"]))),_=Object(B.a)(s||(s=Object(C.a)(["\n  mutation deleteBook($bookId: ID!) {\n  deleteBook(bookId: $bookId) {\n      _id\n      username\n      email\n      bookCount\n      savedBooks {\n        bookId\n        title\n        authors\n        image\n        description\n        link\n      }\n    }\n  }\n"]))),N=n(92),$=n(93),q=n(81),E=n.n(q),D=new(function(){function e(){Object(N.a)(this,e)}return Object($.a)(e,[{key:"getProfile",value:function(){return E()(this.getToken())}},{key:"loggedIn",value:function(){var e=this.getToken();return!!e&&!this.isTokenExpired(e)}},{key:"isTokenExpired",value:function(e){try{return E()(e).exp<Date.now()/1e3}catch(t){return!1}}},{key:"getToken",value:function(){return localStorage.getItem("id_token")}},{key:"login",value:function(e){localStorage.setItem("id_token",e),window.location.assign("/")}},{key:"logout",value:function(){localStorage.removeItem("id_token"),window.location.assign("/")}}]),e}()),P=function(e){var t=localStorage.getItem("saved_books")?JSON.parse(localStorage.getItem("saved_books")):null;if(!t)return!1;var n=null===t||void 0===t?void 0:t.filter((function(t){return t!==e}));return localStorage.setItem("saved_books",JSON.stringify(n)),!0},G=n(2),U=function(){var e=Object(i.useState)([]),t=Object(m.a)(e,2),n=t[0],a=t[1],r=Object(i.useState)(""),o=Object(m.a)(r,2),s=o[0],c=o[1],l=Object(i.useState)(localStorage.getItem("saved_books")?JSON.parse(localStorage.getItem("saved_books")):[]),d=Object(m.a)(l,2),u=d[0],j=d[1],b=Object(I.a)(L),p=Object(m.a)(b,2),C=p[0];p[1].error;Object(i.useEffect)((function(){return function(){var e;(e=u).length?localStorage.setItem("saved_books",JSON.stringify(e)):localStorage.removeItem("saved_books")}}));var B=function(){var e=Object(O.a)(x.a.mark((function e(t){var n,r,o,i;return x.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(t.preventDefault(),s){e.next=3;break}return e.abrupt("return",!1);case 3:return e.prev=3,e.next=6,fetch("https://www.googleapis.com/books/v1/volumes?q=".concat(s));case 6:if((n=e.sent).ok){e.next=9;break}throw new Error("something went wrong!");case 9:return e.next=11,n.json();case 11:r=e.sent,o=r.items,i=o.map((function(e){var t;return{bookId:e.id,authors:e.volumeInfo.authors||["No author to display"],title:e.volumeInfo.title,description:e.volumeInfo.description,image:(null===(t=e.volumeInfo.imageLinks)||void 0===t?void 0:t.thumbnail)||""}})),a(i),c(""),e.next=21;break;case 18:e.prev=18,e.t0=e.catch(3),console.error(e.t0);case 21:case"end":return e.stop()}}),e,null,[[3,18]])})));return function(t){return e.apply(this,arguments)}}(),F=function(){var e=Object(O.a)(x.a.mark((function e(t){var a,r;return x.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a=n.find((function(e){return e.bookId===t})),D.loggedIn()?D.getToken():null){e.next=4;break}return e.abrupt("return",!1);case 4:return e.prev=4,e.next=7,C({variables:{input:a}});case 7:r=e.sent,r.data,j([].concat(Object(h.a)(u),[a.bookId])),e.next=15;break;case 12:e.prev=12,e.t0=e.catch(4),console.error(e.t0);case 15:case"end":return e.stop()}}),e,null,[[4,12]])})));return function(t){return e.apply(this,arguments)}}();return Object(G.jsxs)(G.Fragment,{children:[Object(G.jsx)(g.a,{fluid:!0,className:"text-light bg-dark",children:Object(G.jsxs)(v.a,{children:[Object(G.jsx)("h1",{children:"Search for Books!"}),Object(G.jsx)(k.a,{onSubmit:B,children:Object(G.jsxs)(k.a.Row,{children:[Object(G.jsx)(f.a,{xs:12,md:8,children:Object(G.jsx)(k.a.Control,{name:"searchInput",value:s,onChange:function(e){return c(e.target.value)},type:"text",size:"lg",placeholder:"Search for a book"})}),Object(G.jsx)(f.a,{xs:12,md:4,children:Object(G.jsx)(w.a,{type:"submit",variant:"success",size:"lg",children:"Submit Search"})})]})})]})}),Object(G.jsxs)(v.a,{children:[Object(G.jsx)("h2",{children:n.length?"Viewing ".concat(n.length," results:"):"Search for a book to begin"}),Object(G.jsx)(S.a,{children:n.map((function(e){return Object(G.jsxs)(y.a,{border:"dark",children:[e.image?Object(G.jsx)(y.a.Img,{src:e.image,alt:"The cover for ".concat(e.title),variant:"top"}):null,Object(G.jsxs)(y.a.Body,{children:[Object(G.jsx)(y.a.Title,{children:e.title}),Object(G.jsxs)("p",{className:"small",children:["Authors: ",e.authors]}),Object(G.jsx)(y.a.Text,{children:e.description}),D.loggedIn()&&Object(G.jsx)(w.a,{disabled:null===u||void 0===u?void 0:u.some((function(t){return t===e.bookId})),className:"btn-block btn-info",onClick:function(){return F(e.bookId)},children:null!==u&&void 0!==u&&u.some((function(t){return t===e.bookId}))?"This book has already been saved!":"Save this Book!"})]})]},e.bookId)}))})]})]})},V=n(131),Y=Object(B.a)(c||(c=Object(C.a)(["\n  {\n    me {\n      _id\n      username\n      email\n      bookCount\n      savedBooks {\n        bookId\n        title\n        authors\n        image\n        description\n        link\n      }\n    }\n  }\n"]))),J=function(){var e=Object(I.a)(_),t=Object(m.a)(e,2),n=t[0],a=t[1].error,r=Object(V.a)(Y),o=r.loading,s=r.data,c=(null===s||void 0===s?void 0:s.me)||{},i=function(){var e=Object(O.a)(x.a.mark((function e(t){var r,o;return x.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(D.loggedIn()?D.getToken():null){e.next=3;break}return e.abrupt("return",!1);case 3:return e.prev=3,e.next=6,n({variables:{bookId:t}});case 6:if(r=e.sent,o=r.data,console.log(o),!a){e.next=11;break}throw new Error("something went wrong!");case 11:P(t),e.next=17;break;case 14:e.prev=14,e.t0=e.catch(3),console.error(e.t0);case 17:case"end":return e.stop()}}),e,null,[[3,14]])})));return function(t){return e.apply(this,arguments)}}();return o?Object(G.jsx)("h2",{children:"LOADING..."}):Object(G.jsxs)(G.Fragment,{children:[Object(G.jsx)(g.a,{fluid:!0,className:"text-light bg-dark",children:Object(G.jsx)(v.a,{children:Object(G.jsx)("h1",{children:"Viewing saved books!"})})}),Object(G.jsxs)(v.a,{children:[Object(G.jsx)("h2",{children:c.savedBooks.length?"Viewing ".concat(c.savedBooks.length," saved ").concat(1===c.savedBooks.length?"book":"books",":"):"You have no saved books!"}),Object(G.jsx)(S.a,{children:c.savedBooks.map((function(e){return Object(G.jsxs)(y.a,{border:"dark",children:[e.image?Object(G.jsx)(y.a.Img,{src:e.image,alt:"The cover for ".concat(e.title),variant:"top"}):null,Object(G.jsxs)(y.a.Body,{children:[Object(G.jsx)(y.a.Title,{children:e.title}),Object(G.jsxs)("p",{className:"small",children:["Authors: ",e.authors]}),Object(G.jsx)(y.a.Text,{children:e.description}),Object(G.jsx)(w.a,{className:"btn-block btn-danger",onClick:function(){return i(e.bookId)},children:"Delete this Book!"})]})]},e.bookId)}))})]})]})},K=n(137),z=n(136),A=n(134),M=n(138),H=n(53),R=n(44),W=n(132),Q=function(){var e=Object(i.useState)({username:"",email:"",password:""}),t=Object(m.a)(e,2),n=t[0],a=t[1],r=Object(i.useState)(!1),o=Object(m.a)(r,1)[0],s=Object(i.useState)(!1),c=Object(m.a)(s,2),l=c[0],d=c[1],u=Object(I.a)(F),j=Object(m.a)(u,2),b=j[0],h=(j[1].error,function(e){var t=e.target,r=t.name,o=t.value;a(Object(R.a)(Object(R.a)({},n),{},Object(H.a)({},r,o)))}),p=function(){var e=Object(O.a)(x.a.mark((function e(t){var r,o;return x.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t.preventDefault(),!1===t.currentTarget.checkValidity()&&(t.preventDefault(),t.stopPropagation()),e.prev=3,e.next=6,b({variables:Object(R.a)({},n)});case 6:r=e.sent,o=r.data,D.login(o.addUser.token),e.next=15;break;case 11:e.prev=11,e.t0=e.catch(3),console.error(e.t0),d(!0);case 15:a({username:"",email:"",password:""});case 16:case"end":return e.stop()}}),e,null,[[3,11]])})));return function(t){return e.apply(this,arguments)}}();return Object(G.jsx)(G.Fragment,{children:Object(G.jsxs)(k.a,{noValidate:!0,validated:o,onSubmit:p,children:[Object(G.jsx)(W.a,{dismissible:!0,onClose:function(){return d(!1)},show:l,variant:"danger",children:"Something went wrong with your signup!"}),Object(G.jsxs)(k.a.Group,{children:[Object(G.jsx)(k.a.Label,{htmlFor:"username",children:"Username"}),Object(G.jsx)(k.a.Control,{type:"text",placeholder:"Your username",name:"username",onChange:h,value:n.username,required:!0}),Object(G.jsx)(k.a.Control.Feedback,{type:"invalid",children:"Username is required!"})]}),Object(G.jsxs)(k.a.Group,{children:[Object(G.jsx)(k.a.Label,{htmlFor:"email",children:"Email"}),Object(G.jsx)(k.a.Control,{type:"email",placeholder:"Your email address",name:"email",onChange:h,value:n.email,required:!0}),Object(G.jsx)(k.a.Control.Feedback,{type:"invalid",children:"Email is required!"})]}),Object(G.jsxs)(k.a.Group,{children:[Object(G.jsx)(k.a.Label,{htmlFor:"password",children:"Password"}),Object(G.jsx)(k.a.Control,{type:"password",placeholder:"Your password",name:"password",onChange:h,value:n.password,required:!0}),Object(G.jsx)(k.a.Control.Feedback,{type:"invalid",children:"Password is required!"})]}),Object(G.jsx)(w.a,{disabled:!(n.username&&n.email&&n.password),type:"submit",variant:"success",children:"Submit"})]})})},X=function(){var e=Object(i.useState)({email:"",password:""}),t=Object(m.a)(e,2),n=t[0],a=t[1],r=Object(i.useState)(!1),o=Object(m.a)(r,1)[0],s=Object(i.useState)(!1),c=Object(m.a)(s,2),l=c[0],d=c[1],u=Object(I.a)(T),j=Object(m.a)(u,2),b=j[0],h=(j[1].error,function(e){var t=e.target,r=t.name,o=t.value;a(Object(R.a)(Object(R.a)({},n),{},Object(H.a)({},r,o)))}),p=function(){var e=Object(O.a)(x.a.mark((function e(t){var r,o;return x.a.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t.preventDefault(),!1===t.currentTarget.checkValidity()&&(t.preventDefault(),t.stopPropagation()),e.prev=3,e.next=6,b({variables:Object(R.a)({},n)});case 6:r=e.sent,o=r.data,D.login(o.login.token),e.next=15;break;case 11:e.prev=11,e.t0=e.catch(3),console.error(e.t0),d(!0);case 15:a({username:"",email:"",password:""});case 16:case"end":return e.stop()}}),e,null,[[3,11]])})));return function(t){return e.apply(this,arguments)}}();return Object(G.jsx)(G.Fragment,{children:Object(G.jsxs)(k.a,{noValidate:!0,validated:o,onSubmit:p,children:[Object(G.jsx)(W.a,{dismissible:!0,onClose:function(){return d(!1)},show:l,variant:"danger",children:"Something went wrong with your login credentials!"}),Object(G.jsxs)(k.a.Group,{children:[Object(G.jsx)(k.a.Label,{htmlFor:"email",children:"Email"}),Object(G.jsx)(k.a.Control,{type:"text",placeholder:"Your email",name:"email",onChange:h,value:n.email,required:!0}),Object(G.jsx)(k.a.Control.Feedback,{type:"invalid",children:"Email is required!"})]}),Object(G.jsxs)(k.a.Group,{children:[Object(G.jsx)(k.a.Label,{htmlFor:"password",children:"Password"}),Object(G.jsx)(k.a.Control,{type:"password",placeholder:"Your password",name:"password",onChange:h,value:n.password,required:!0}),Object(G.jsx)(k.a.Control.Feedback,{type:"invalid",children:"Password is required!"})]}),Object(G.jsx)(w.a,{disabled:!(n.email&&n.password),type:"submit",variant:"success",children:"Submit"})]})})},Z=function(){var e=Object(i.useState)(!1),t=Object(m.a)(e,2),n=t[0],a=t[1];return Object(G.jsxs)(G.Fragment,{children:[Object(G.jsx)(K.a,{bg:"dark",variant:"dark",expand:"lg",children:Object(G.jsxs)(v.a,{fluid:!0,children:[Object(G.jsx)(K.a.Brand,{as:j.b,to:"/",children:"Google Books Search"}),Object(G.jsx)(K.a.Toggle,{"aria-controls":"navbar"}),Object(G.jsx)(K.a.Collapse,{id:"navbar",children:Object(G.jsxs)(z.a,{className:"ml-auto",children:[Object(G.jsx)(z.a.Link,{as:j.b,to:"/",children:"Search For Books"}),D.loggedIn()?Object(G.jsxs)(G.Fragment,{children:[Object(G.jsx)(z.a.Link,{as:j.b,to:"/saved",children:"See Your Books"}),Object(G.jsx)(z.a.Link,{onClick:D.logout,children:"Logout"})]}):Object(G.jsx)(z.a.Link,{onClick:function(){return a(!0)},children:"Login/Sign Up"})]})})]})}),Object(G.jsx)(A.a,{size:"lg",show:n,onHide:function(){return a(!1)},"aria-labelledby":"signup-modal",children:Object(G.jsxs)(M.a.Container,{defaultActiveKey:"login",children:[Object(G.jsx)(A.a.Header,{closeButton:!0,children:Object(G.jsx)(A.a.Title,{id:"signup-modal",children:Object(G.jsxs)(z.a,{variant:"pills",children:[Object(G.jsx)(z.a.Item,{children:Object(G.jsx)(z.a.Link,{eventKey:"login",children:"Login"})}),Object(G.jsx)(z.a.Item,{children:Object(G.jsx)(z.a.Link,{eventKey:"signup",children:"Sign Up"})})]})})}),Object(G.jsx)(A.a.Body,{children:Object(G.jsxs)(M.a.Content,{children:[Object(G.jsx)(M.a.Pane,{eventKey:"login",children:Object(G.jsx)(X,{handleModalClose:function(){return a(!1)}})}),Object(G.jsx)(M.a.Pane,{eventKey:"signup",children:Object(G.jsx)(Q,{handleModalClose:function(){return a(!1)}})})]})})]})})]})},ee=n(133),te=new(n(102).a)({request:function(e){var t=localStorage.getItem("id_token");e.setContext({headers:{authorization:t?"Bearer ".concat(t):""}})},uri:"/graphql"});var ne=function(){return Object(G.jsx)(ee.a,{client:te,children:Object(G.jsx)(j.a,{children:Object(G.jsxs)(G.Fragment,{children:[Object(G.jsx)(Z,{}),Object(G.jsxs)(b.c,{children:[Object(G.jsx)(b.a,{exact:!0,path:"/",element:Object(G.jsx)(U,{})}),Object(G.jsx)(b.a,{exact:!0,path:"/saved",element:Object(G.jsx)(J,{})}),Object(G.jsx)(b.a,{render:function(){return Object(G.jsx)("h1",{className:"display-2",children:"Wrong page!"})}})]})]})})})};u.a.render(Object(G.jsx)(l.a.StrictMode,{children:Object(G.jsx)(ne,{})}),document.getElementById("root"))}},[[123,1,2]]]);
  //# sourceMappingURL=main.d508f77b.chunk.js.map