Skip to content

Commit 64f9eea

Browse files
author
Alex Patterson
committed
added books lambda
1 parent 040d4b7 commit 64f9eea

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

firebase.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
"target": "ajsbooks-nextjs",
1313
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
1414
"rewrites": [
15+
{
16+
"source": "/books",
17+
"function": "books"
18+
},
1519
{
1620
"source": "/book{,/**}",
1721
"function": "book"

functions/src/api/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as cors from 'cors';
22
import * as express from 'express';
33
import * as admin from 'firebase-admin';
44
import * as functions from 'firebase-functions';
5-
5+
import BookModel from '../../../models/BookModel';
66
// Initialize Firebase Admin
77
admin.initializeApp();
88

@@ -32,6 +32,22 @@ app.get('/book', async (req, res) => {
3232
return;
3333
}
3434
});
35+
app.get('/books', async (req, res) => {
36+
try {
37+
const booksSnapshot = await admin
38+
.firestore()
39+
.collection('books')
40+
.get();
41+
const books: BookModel[] = [];
42+
booksSnapshot.forEach(doc => {
43+
books.push(doc.data());
44+
});
45+
res.json(books);
46+
} catch (e) {
47+
console.log(e);
48+
return;
49+
}
50+
});
3551
app.get('/chapter', async (req, res) => {
3652
const id = req.query.id;
3753
const chapterId = req.query.chapterId;

functions/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const onRequest = functions.https.onRequest;
66

77
// These relative paths will exist after compiling everything
88
// const index = require('./_next/serverless/pages/index'); //Removed as Next 9 only pushes static
9-
// const books = require('./_next/serverless/pages/books'); //Removed as Next 9 only pushes static
9+
const books = require('./_next/serverless/pages/books');
1010
const book = require('./_next/serverless/pages/book');
1111

1212
// These named exports will map to Firebase Function names
1313
// exports.index = onRequest((req, res) => index.render(req, res)); //Removed as Next 9 only pushes static
14-
// exports.books = onRequest((req, res) => books.render(req, res)); //Removed as Next 9 only pushes static
14+
exports.books = onRequest((req, res) => books.render(req, res));
1515
exports.book = onRequest((req, res) => book.render(req, res));
1616

1717
// API

pages/books.tsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,40 @@ import Grid from '@material-ui/core/Grid';
33
import React, { Component } from 'react';
44
import { collectionData } from 'rxfire/firestore';
55
import { Subject } from 'rxjs';
6+
import fetch from 'isomorphic-unfetch';
67
import { takeUntil } from 'rxjs/operators';
78

89
import BookCard from '../components/BookCard';
910
import loadFirebase from '../lib/firebase';
10-
import Book from '../models/BookModel';
11+
import BookModel from '../models/BookModel';
1112

12-
export default class books extends Component {
13-
state: { books: Book[]; stopSubs: Subject<boolean> } = {
13+
export default class books extends Component<
14+
{
15+
books: BookModel[];
16+
},
17+
any
18+
> {
19+
state: {
20+
books: BookModel[];
21+
stopSubs: Subject<boolean>;
22+
firebase?: any;
23+
} = {
1424
books: [],
1525
stopSubs: new Subject<boolean>()
1626
};
27+
static async getInitialProps() {
28+
const res = await fetch(`${process.env.API_ENDPOINT}books`);
29+
const books = await res.json();
30+
return { books: books };
31+
}
1732
async componentDidMount() {
33+
/* Coming from SSR Initial Props */
34+
await this.setState(() => {
35+
return {
36+
books: this.props.books
37+
};
38+
});
39+
/* After client loads */
1840
const firebase = await loadFirebase();
1941
const booksRef = firebase.firestore().collection('books');
2042
collectionData(booksRef, 'bookId')
@@ -35,7 +57,7 @@ export default class books extends Component {
3557
return (
3658
<Grid container direction="row" justify="center">
3759
{loading}
38-
{this.state.books.map((book: Book) => {
60+
{this.state.books.map((book: BookModel) => {
3961
return <BookCard book={book} key={book.id} />;
4062
})}
4163
</Grid>

0 commit comments

Comments
 (0)