@@ -53,25 +53,34 @@ class book extends Component<
5353 }
5454 } ;
5555 if ( req ) {
56- console . log ( 'Server only call.' ) ;
56+ /* Just a note using Promise.all() would probably be faster */
5757 if ( id ) {
58+ console . log ( 'Server Fetching Book' , id ) ;
5859 const res = await fetch (
59- // Local example http://localhost:5001/ajonp-ajs-books/us-central1/api/
60- `https://us-central1-ajonp-ajs-books.cloudfunctions.net/api/book?id=${ id } `
60+ /*
61+ API can be found in next.config.js
62+ Local: http://localhost:5001/ajonp-ajs-books/us-central1/api/
63+ Remote: https://us-central1-ajonp-ajs-books.cloudfunctions.net/api/
64+ */
65+ `${ process . env . API_ENDPOINT } book?id=${ id } `
6166 ) ;
6267 const json = await res . json ( ) ;
6368 retObj . book = json ;
6469 }
6570 if ( chapterId ) {
71+ console . log ( 'Server Fetching Chapter' , chapterId ) ;
6672 const res = await fetch (
67- `https://us-central1-ajonp-ajs-books.cloudfunctions.net/api/ chapter?id=${ id } &chapterId=${ chapterId } `
73+ `${ process . env . API_ENDPOINT } chapter?id=${ id } &chapterId=${ chapterId } `
6874 ) ;
6975 const json = await res . json ( ) ;
7076 retObj . chapter = json ;
7177 }
7278 if ( pageId ) {
79+ console . log ( 'Server Fetching Page' , pageId ) ;
7380 const res = await fetch (
74- `https://us-central1-ajonp-ajs-books.cloudfunctions.net/api/page?id=${ id } &chapterId=${ chapterId } &pageId=${ pageId } `
81+ `${
82+ process . env . API_ENDPOINT
83+ } page?id=${ id } &chapterId=${ chapterId } &pageId=${ pageId } `
7584 ) ;
7685 const json = await res . json ( ) ;
7786 retObj . page = json ;
@@ -89,7 +98,11 @@ class book extends Component<
8998 } ) ;
9099
91100 /* After client loads */
92- this . updateFirebaseRefs ( ) ;
101+ this . updateFirebaseRefs (
102+ this . props . book . id as string ,
103+ this . props . chapter . id as string ,
104+ this . props . page . id as string
105+ ) ;
93106 }
94107 componentWillUnmount ( ) {
95108 /* Stop observing */
@@ -102,53 +115,60 @@ class book extends Component<
102115 if ( prevProps . router . query !== this . props . router . query ) {
103116 const q = this . props . router . query ;
104117 if ( q ) {
105- await this . setState ( {
106- book : { id : q . id } ,
107- chapter : { id : q . chapterId } ,
108- page : { id : q . pageId }
109- } ) ;
110- this . updateFirebaseRefs ( ) ;
118+ this . updateFirebaseRefs (
119+ q . id as string ,
120+ q . chapterId as string ,
121+ q . pageId as string
122+ ) ;
111123 }
112124 }
113125 }
114- updateFirebaseRefs ( ) {
115- const booksRef = this . state . firebase
116- . firestore ( )
117- . collection ( 'books' )
118- . doc ( this . state . book . id ) ;
119- // Book Detail
120- docData ( booksRef , 'id' )
121- . pipe ( takeUntil ( this . state . stopSubs ) )
122- . subscribe ( book => {
123- this . setState ( { book } ) ;
124- } ) ;
126+ updateFirebaseRefs (
127+ bookId : string ,
128+ chapterId : string | undefined ,
129+ pageId : string | undefined
130+ ) {
131+ if ( bookId ) {
132+ const booksRef = this . state . firebase
133+ . firestore ( )
134+ . collection ( 'books' )
135+ . doc ( bookId ) ;
136+ // Book Detail
137+ docData ( booksRef , 'id' )
138+ . pipe ( takeUntil ( this . state . stopSubs ) )
139+ . subscribe ( book => {
140+ this . setState ( { book } ) ;
141+ } ) ;
142+ }
125143 // Chapter Detail
126- if ( this . state . chapter . id ) {
144+ if ( chapterId ) {
127145 const chapterRef = this . state . firebase
128146 . firestore ( )
129- . collection ( `books/${ this . state . book . id } /chapters` )
130- . doc ( this . state . chapter . id ) ;
147+ . collection ( `books/${ bookId } /chapters` )
148+ . doc ( chapterId ) ;
131149 docData ( chapterRef , 'id' )
132150 . pipe ( takeUntil ( this . state . stopSubs ) )
133151 . subscribe ( chapter => {
134152 this . setState ( { chapter } ) ;
135153 } ) ;
136154
137- if ( this . state . page . id ) {
155+ if ( pageId ) {
138156 const pageRef = this . state . firebase
139157 . firestore ( )
140- . collection (
141- `books/${ this . state . book . id } /chapters/${
142- this . state . chapter . id
143- } /pages`
144- )
145- . doc ( this . state . page . id ) ;
158+ . collection ( `books/${ bookId } /chapters/${ chapterId } /pages` )
159+ . doc ( pageId ) ;
146160 docData ( pageRef , 'id' )
147161 . pipe ( takeUntil ( this . state . stopSubs ) )
148162 . subscribe ( page => {
149163 this . setState ( { page } ) ;
150164 } ) ;
165+ } else {
166+ /* Remove page state if not added */
167+ this . state . page = new PageModel ( ) ;
151168 }
169+ } else {
170+ /* Remove chapter state if not added */
171+ this . state . chapter = new ChapterModel ( ) ;
152172 }
153173 }
154174 render ( ) {
0 commit comments