Skip to content

Commit

Permalink
receive hackatons directly from firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
voronin-ivan committed Sep 28, 2020
1 parent f885765 commit 8dbca30
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
},
"rules": {
"react/react-in-jsx-scope": "off",
"jsx-a11y/anchor-is-valid": "off"
"jsx-a11y/anchor-is-valid": "off",
"react/prop-types": "off"
}
}
11 changes: 4 additions & 7 deletions pages/api/hackathons.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { NextApiRequest, NextApiResponse } from 'next';
import db from '../../utils/server/database';
import { Hackathon } from '../../types/hackathon';
import getHackathons from '../../utils/firebase/getHackathons';

const getHackathons = async (_: NextApiRequest, res: NextApiResponse) => {
const hackathonsHandler = async (_: NextApiRequest, res: NextApiResponse) => {
try {
const hackathonsRef = db.ref('hackathons');
const snapshot = await hackathonsRef.once('value');
const hackathons: Hackathon[] = snapshot.val();
const hackathons = await getHackathons();

res.status(200).json({ hackathons });
} catch (e) {
res.status(500).json({ error: e.message });
}
};

export default getHackathons;
export default hackathonsHandler;
15 changes: 8 additions & 7 deletions pages/hackathons.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { useEffect, useState } from 'react';
import { NextPageContext } from 'next';
import { NextPage } from 'next';
import axios from 'axios';
import { Hackathon } from '../types/hackathon';
import { HackathonsApi } from '../types/api';
import getOriginFromRequest from '../utils/server/getOriginFromRequest';
import endpoints from '../constants/endpoints';

type HackathonsProps = {
hackathons?: Hackathon[];
error?: string;
};

const Hackathons = ({ hackathons: initialHackathons, error: initialError }: HackathonsProps) => {
const Hackathons: NextPage<HackathonsProps> = ({
hackathons: initialHackathons,
error: initialError,
}) => {
const [hackathons, updateHackathons] = useState(initialHackathons);
const [error, updateError] = useState(initialError);

Expand Down Expand Up @@ -46,13 +48,12 @@ const Hackathons = ({ hackathons: initialHackathons, error: initialError }: Hack
);
};

Hackathons.getInitialProps = async ({ req }: NextPageContext): Promise<HackathonsProps> => {
Hackathons.getInitialProps = async ({ req }): Promise<HackathonsProps> => {
if (!req) return {};

try {
const url = getOriginFromRequest(req) + endpoints.api.hackathons;
const res = await axios.get<HackathonsApi>(url);
const { hackathons } = res.data;
const { default: getHackathons } = await import('../utils/firebase/getHackathons');
const hackathons = await getHackathons();

return { hackathons };
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const initialConfig = {
appId: process.env.APP_ID,
};

if (!firebase.apps.length) {
firebase.initializeApp(initialConfig);
}
const getFirebaseInstance = () => firebase.apps[0] || firebase.initializeApp(initialConfig);

export default firebase.database();
export default getFirebaseInstance;
19 changes: 19 additions & 0 deletions utils/firebase/getHackathons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import getFirebaseInstance from './getFirebaseInstance';
import { Hackathon } from '../../types/hackathon';

const getHackathons = async () => {
const firebaseInstance = getFirebaseInstance();
const hackathonsRef = firebaseInstance.database().ref('hackathons');
const snapshot = await hackathonsRef.once('value');
const hackathons: Hackathon[] = snapshot.val();

try {
// required for netlify https://5f723a99796e900007e9382a--growth-points.netlify.app/
await firebaseInstance.delete();
} finally {
// eslint-disable-next-line no-unsafe-finally
return hackathons;
}
};

export default getHackathons;
10 changes: 0 additions & 10 deletions utils/server/getOriginFromRequest.ts

This file was deleted.

0 comments on commit 8dbca30

Please sign in to comment.