(firebase): implement basic server functions

master
chwan1 4 years ago
parent 359b89b627
commit 59eb4b2169
  1. 1
      .next/package.json
  2. 192
      Firebase-21006-Backend/firebase-debug.log
  3. 17
      Firebase-21006-Backend/package.json
  4. 6
      Firebase-21006-Backend/postcss.config.js
  5. 2
      Firebase-21006-Backend/src/components/App.tsx
  6. 2
      Firebase-21006-Backend/src/components/Header.tsx
  7. 5
      Firebase-21006-Backend/src/next-env.d.ts
  8. 4
      Firebase-21006-Backend/src/next.config.js
  9. 36
      Firebase-21006-Backend/src/pages/_app.tsx
  10. 22
      Firebase-21006-Backend/src/pages/api/addQuestionnaire.ts
  11. 8
      Firebase-21006-Backend/src/pages/api/test.ts
  12. 67
      Firebase-21006-Backend/src/pages/index.jsx
  13. 3
      Firebase-21006-Backend/src/styles/globals.css
  14. 30
      Firebase-21006-Backend/src/tsconfig.json
  15. 14
      Firebase-21006-Backend/src/utils/db/index.js
  16. 12
      Firebase-21006-Backend/src/utils/db/serviceAccountKey.json
  17. 10
      Firebase-21006-Backend/tailwind.config.js
  18. 957
      Firebase-21006-Backend/yarn.lock

@ -0,0 +1 @@
{"type": "commonjs"}

File diff suppressed because one or more lines are too long

@ -11,14 +11,23 @@
"logs": "firebase functions:log" "logs": "firebase functions:log"
}, },
"dependencies": { "dependencies": {
"firebase-admin": "^9.4.2", "daisyui": "^2.6.3",
"firebase": "^9.1.0",
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.13.1", "firebase-functions": "^3.13.1",
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^17.0.2",
"react-dom": "^17.0.2" "react-dom": "^17.0.2",
"reactfire": "^4.2.1"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^17.0.21",
"@types/react": "^17.0.39",
"autoprefixer": "^10.4.2",
"firebase-functions-test": "^0.2.3", "firebase-functions-test": "^0.2.3",
"firebase-tools": "^9.3.0" "firebase-tools": "^9.3.0",
"postcss": "^8.4.8",
"tailwindcss": "^3.0.23",
"typescript": "^4.6.2"
} }
} }

@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}

@ -2,7 +2,7 @@ import React from 'react'
import Header from './Header' import Header from './Header'
const App = ({ children }) => ( const App = ({ children }) => (
<main> <main className="flex items-center flex-col gap-2 pt-4">
<Header /> <Header />
{children} {children}
</main> </main>

@ -1,7 +1,7 @@
import * as React from 'react' import * as React from 'react'
import Link from 'next/link' import Link from 'next/link'
const Header = ({ pathname }) => ( const Header = ({ pathname }: { pathname?: string }) => (
<header> <header>
<Link href="/"> <Link href="/">
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> <a className={pathname === '/' ? 'is-active' : ''}>Home</a>

@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

@ -1,3 +1,7 @@
module.exports = { module.exports = {
distDir: '../.next', distDir: '../.next',
esmExternals: false,
experimental: {
esmExternals: false
},
} }

@ -0,0 +1,36 @@
import '../styles/globals.css'
import { getFirestore } from 'firebase/firestore';
import { FirebaseAppProvider, FirestoreProvider, useFirebaseApp } from 'reactfire';
// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyBdgSlMbnKwFnCxKu3tMAp4tpCQpG5V1uw",
authDomain: "uc-21006-backend.firebaseapp.com",
projectId: "uc-21006-backend",
storageBucket: "uc-21006-backend.appspot.com",
messagingSenderId: "667264331891",
appId: "1:667264331891:web:8ab0331441423cb883fa06"
};
function FirebaseSDKProviders({ children }) {
const app = useFirebaseApp()
const firestore = getFirestore(app)
return (
<FirestoreProvider sdk={firestore}>
{children}
</FirestoreProvider>
)
}
function App({ Component, pageProps }) {
return (
<FirebaseAppProvider firebaseConfig={firebaseConfig}>
<FirebaseSDKProviders>
<Component {...pageProps} />
</FirebaseSDKProviders>
</FirebaseAppProvider>
)
}
export default App

@ -0,0 +1,22 @@
import type { NextApiRequest, NextApiResponse } from 'next'
import db from '../../utils/db';
import { database } from 'firebase-admin';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const { slug } = req.body;
const entries = await db.collection('questionnaire').get();
const entriesData = entries.docs.map(entry => entry.data());
const { id } = await db.collection('questionnaire').add({
...req.body,
created: new Date().toISOString(),
});
res.status(200).json({ id });
} catch (e) {
res.status(400).end();
}
}

@ -0,0 +1,8 @@
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
res.status(200).json({ name: 'John Doe' })
}

@ -1,9 +1,74 @@
import { useRef } from 'react';
import App from '../components/App' import App from '../components/App'
import { collection, query, orderBy } from '@firebase/firestore';
import { useFirestore, useFirestoreCollectionData } from 'reactfire';
export default function Home() { export default function Home() {
const firestore = useFirestore();
const questionnaireCollection = collection(firestore, 'questionnaire');
const questionnaireQuery = query(questionnaireCollection, orderBy('created', 'desc'));
const { status, data: questionnaire } = useFirestoreCollectionData(questionnaireQuery, {
idField: 'id', // this field will be added to the object created from each document
});
const Q1Ref = useRef();
async function handleSubmit(event) {
event.preventDefault();
let body = JSON.stringify({
Q1: Q1Ref.current.value,
});
try {
let r = await fetch("/api/addQuestionnaire", {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': body.length.toString(),
},
body: body,
});
let data = await r.json();
console.log(data);
}
catch (e) {
console.error(e);
}
console.log(Q1Ref.current.value);
}
return ( return (
<App> <App>
<p>Index Page</p> <div className="artboard phone-1 rounded-lg border-0 flex flex-col p-2 gap-4">
<form className="form-control w-full max-w-xs gap-2" onSubmit={handleSubmit}>
<label className="label">
<span className="label-text">你最喜歡的科幻/奇幻作品是哪一個</span>
{/* <span class="label-text-alt">Alt label</span> */}
</label>
{/* <select className="select select-bordered" value="Pick one" onChange={handleChange}> */}
<select className="select select-bordered" ref={Q1Ref}>
<option disabled selected>Pick one</option>
<option>Star Wars</option>
<option>Harry Potter</option>
<option>Lord of the Rings</option>
<option>Planet of the Apes</option>
<option>Star Trek</option>
</select>
<input className="btn" type="submit" value="Submit" />
{/* <label class="label">
<span class="label-text-alt">Alt label</span>
<span class="label-text-alt">Alt label</span>
</label> */}
</form>
{/* <button className="btn" onClick={() => console.log("test")}>Button</button> */}
{
status == 'success' && questionnaire.map(q => (
<div key={q.id} className=' '>{q.Q1}</div>
))
}
</div>
</App> </App>
) )
} }

@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

@ -0,0 +1,30 @@
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}

@ -0,0 +1,14 @@
import admin from 'firebase-admin';
import serviceAccount from './serviceAccountKey.json';
if (!admin.apps.length) {
try {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
// databaseURL: "YOUR_DB_URL"
});
} catch (error) {
console.log('Firebase admin initialization error', error.stack);
}
}
export default admin.firestore();

@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "uc-21006-backend",
"private_key_id": "82cf89939b88a851b1687b7ec910945044889fcc",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCQUKqzUJ4Q4NqS\nakcXGenyPOqj8P4yurfzGW3E0O/a00TsNwdpZQ4RTuKKA17cHJq3/l5qyuoYqo/V\nQUtck9VDTyJBxd4wYJBlywpwv+pmVdTDLZ3qVfsG/I7FLOKqfaml0V5vRan1m3Zw\nnJAcTLHM9kt1Wr+q2Me54Q8EBsyUzKyxYwI3WCsCcuLj9l5EfFHymGLT/Yg2stQ3\nsUNzBN4g7C1JiVXxSfvDgFE1vhYfkv+NEhx32rOis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fHxFYBGuNJsdxIeE5BrIDRr2H83gnZPZXLeY0aYnIOcL5MLIOQLQyJdWOyFO\n56dMZcHrq87DIM8e4uq+zF6uQHe5iEJ3sTi3MtsCgYBYf91+Ff+kU4YBp+mQJX9u\nqVWdjVUxFku+4/ZAWvoOsoDKLSKN7gOLBY0e6nlc4j09jzNgTZ24TPMS4XGocpWm\nLazqyzCKFLViMS0rZfcPZVPh3I9/IFwkZPMxklKpfZLHhw8xiXH+iw486nRl4FT/\nKj9ejmo6M4jOaNFAV0oi5w==\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-6mr45@uc-21006-backend.iam.gserviceaccount.com",
"client_id": "116964042143658711653",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-6mr45%40uc-21006-backend.iam.gserviceaccount.com"
}

@ -0,0 +1,10 @@
module.exports = {
content: [
"./src/pages/**/*.{js,ts,jsx,tsx}",
"./src/components/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [require("daisyui")]
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save