Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
voronin-ivan committed Sep 28, 2020
1 parent 72d1692 commit 70ef2d9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pages/api/hackathons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { NextApiRequest, NextApiResponse } from 'next';
import getFirebaseInstance from '../../utils/server/getFirebaseInstance';
import { Hackathon } from '../../types/hackathon';

const getHackathons = async (_: NextApiRequest, res: NextApiResponse) => {
console.log(process.env);
const getHackathons = async (req: NextApiRequest, res: NextApiResponse) => {
console.log(req);
try {
const firebase = getFirebaseInstance();
const hackathonsRef = firebase.database().ref('hackathons');
Expand Down
25 changes: 14 additions & 11 deletions pages/hackathons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextPageContext } from 'next';
import axios from 'axios';
import { Hackathon } from '../types/hackathon';
import { HackathonsApi } from '../types/api';
import getOriginFromRequest from '../utils/server/getOriginFromRequest';
import getFirebaseInstance from '../utils/server/getFirebaseInstance';
import endpoints from '../constants/endpoints';

type HackathonsProps = {
Expand Down Expand Up @@ -46,18 +46,21 @@ const Hackathons = ({ hackathons: initialHackathons, error: initialError }: Hack
);
};

Hackathons.getInitialProps = async ({ req }: NextPageContext): Promise<HackathonsProps> => {
if (!req) return {};
export const getServerSideProps = async ({ req }: NextPageContext) => {
if (req?.headers.referer) {
return { props: {} };
}

try {
const url = getOriginFromRequest(req) + endpoints.api.hackathons;
const res = await axios.get<HackathonsApi>(url);
const { hackathons } = res.data;
const firebase = getFirebaseInstance();
const hackathonsRef = firebase.database().ref('hackathons');
const snapshot = await hackathonsRef.once('value');
const hackathons: Hackathon[] = snapshot.val();

return { hackathons };
} catch (e) {
return { error: e.message };
}
await firebase.delete();

return {
props: { hackathons },
};
};

export default Hackathons;
9 changes: 2 additions & 7 deletions utils/server/getFirebaseInstance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import firebase from 'firebase-admin';
import firebase from 'firebase';
import 'firebase/database';

const initialConfig = {
Expand All @@ -7,13 +7,8 @@ const initialConfig = {
databaseURL: process.env.DATABASE_URL,
projectId: process.env.PROJECT_ID,
appId: process.env.APP_ID,
credential: firebase.credential.cert({
clientEmail: process.env.CLIENT_EMAIL,
projectId: process.env.PROJECT_ID,
privateKey: process.env.PRIVATE_KEY?.replace(/\\n/g, '\n'),
}),
};

const getFirebaseInstance = () => firebase.apps[0] || firebase.initializeApp(initialConfig);
const getFirebaseInstance = () => firebase.initializeApp(initialConfig);

export default getFirebaseInstance;

0 comments on commit 70ef2d9

Please sign in to comment.