-
Notifications
You must be signed in to change notification settings - Fork 56
Firebase integration #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: thusara
Are you sure you want to change the base?
Firebase integration #301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| REACT_APP_API_KEY=AIzaSyBoO1cF3m--z1OKJUNaflL5drrTA0aebaE | ||
| REACT_APP_AUTH_DOMIAN=dear-diary-thusara-9535b.firebaseapp.com | ||
| REACT_APP_PROJECT_ID=dear-diary-thusara-9535b | ||
| REACT_APP_STORAGE_BUCKET=dear-diary-thusara-9535b.appspot.com | ||
| REACT_APP_SENDER_ID=458518884342 | ||
| REACT_APP_APP_ID=1:458518884342:web:9b1c0526c179fbf82c21d7 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| .env.development.local | ||
| .env.test.local | ||
| .env.production.local | ||
| .env | ||
|
|
||
| npm-debug.log* | ||
| yarn-debug.log* | ||
|
|
||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,9 @@ import { TextField, Button, Container, Stack, Typography } from '@mui/material'; | |
| import type { RootState } from '../../redux/store/store'; | ||
| import { useSelector, useDispatch } from 'react-redux'; | ||
| import { addCard } from '../../redux/card/cardSlice'; | ||
| import app from '../../firebase/setup'; | ||
| import { collection, getFirestore, addDoc } from 'firebase/firestore'; | ||
| const db = getFirestore(app); | ||
|
|
||
| const DiaryForm: React.FC = () => { | ||
| const [showForm, setShowForm] = useState(false); | ||
|
|
@@ -15,7 +18,7 @@ const DiaryForm: React.FC = () => { | |
| setShowForm(true); | ||
| }; | ||
|
|
||
| const handleSubmit = (event: React.FormEvent) => { | ||
| async function handleSubmit(event: React.FormEvent): Promise<void> { | ||
| event.preventDefault(); | ||
| const formData = new FormData(event.target as HTMLFormElement); | ||
| const title = (formData.get('title') || '').toString(); | ||
|
|
@@ -40,6 +43,10 @@ const DiaryForm: React.FC = () => { | |
| description, | ||
| }; | ||
| dispatch(addCard(newDiaryEntry)); | ||
|
|
||
| const docRef = await addDoc(collection(db, "cards"), newDiaryEntry); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't call APIs directly from the UI file, use saga to handle API calls |
||
| console.log("Document written with ID: ", docRef.id); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove unwanted logs |
||
|
|
||
| (event.target as HTMLFormElement).reset(); | ||
| setShowForm(false); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,52 @@ | ||
| import React from 'react'; | ||
| import React, { useEffect } from 'react'; | ||
| import { Container, Grid, Box } from '@mui/material'; | ||
| import DiaryCard from '../../components/DiaryCard/DiaryCard'; | ||
| import PrimaryAppBar from '../../components/AppBar/AppBar'; | ||
| import DiaryForm from '../../components/DiaryForm/DiaryForm'; | ||
| import type { RootState } from '../../redux/store/store'; | ||
| import { useSelector } from 'react-redux'; | ||
| import { useSelector, useDispatch } from 'react-redux'; | ||
| import app from '../../firebase/setup'; | ||
| import { getFirestore, collection, query, onSnapshot } from 'firebase/firestore'; | ||
| import { addCards } from '../../redux/card/cardSlice'; | ||
|
|
||
| const db = getFirestore(app); | ||
|
|
||
| const DiaryHome: React.FC = () => { | ||
| const cards = useSelector((state: RootState) => state.cards.cards) | ||
| return ( | ||
| <> | ||
| <PrimaryAppBar /> | ||
| <Container sx={{ minHeight: '100vh', minWidth: '100vw', backgroundColor: '#42a5f5' }}> | ||
| <Box sx={{ paddingTop: '15vh' }}> | ||
| <DiaryForm /> | ||
| <Grid container sx={{paddingTop: '5vh'}} columnSpacing={{ xs: 1, sm: 1, md: 1 }}> | ||
| {cards.map((card, index) => ( | ||
| <Grid key={index} item xs={12} sm={6} md={3}> | ||
| <DiaryCard title={card.title} subtitle={card.username} description={card.description} /> | ||
| </Grid> | ||
| ))} | ||
| </Grid> | ||
| </Box> | ||
| </Container> | ||
| </> | ||
| ); | ||
| const dispatch = useDispatch(); | ||
|
|
||
| useEffect(() => { | ||
| const unsubscribe = onSnapshot(query(collection(db, 'cards')), (snapShot) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move to saga |
||
| const cards = snapShot.docs.map((doc) => { | ||
| const { title, username, description } = doc.data(); | ||
| return { title, username, description }; | ||
| }); | ||
| dispatch(addCards(cards)); | ||
| }); | ||
|
|
||
| return () => { | ||
| unsubscribe(); | ||
| }; | ||
| }, [dispatch]); | ||
|
|
||
|
|
||
| const cards = useSelector((state: RootState) => state.cards.cards) | ||
| return ( | ||
| <> | ||
| <PrimaryAppBar /> | ||
| <Container sx={{ minHeight: '100vh', minWidth: '100vw', backgroundColor: '#42a5f5' }}> | ||
| <Box sx={{ paddingTop: '15vh' }}> | ||
| <DiaryForm /> | ||
| <Grid container sx={{paddingTop: '5vh'}} columnSpacing={{ xs: 1, sm: 1, md: 1 }}> | ||
| {cards.map((card, index) => ( | ||
| <Grid key={index} item xs={12} sm={6} md={3}> | ||
| <DiaryCard title={card.title} subtitle={card.username} description={card.description} /> | ||
| </Grid> | ||
| ))} | ||
| </Grid> | ||
| </Box> | ||
| </Container> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default DiaryHome; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Import the functions you need from the SDKs you need | ||
| import { initializeApp } from "firebase/app"; | ||
| // https://firebase.google.com/docs/web/setup#available-libraries | ||
|
|
||
| // Your web app's Firebase configuration | ||
| console.log(process.env.REACT_APP_API_KEY) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove logs |
||
| const firebaseConfig = { | ||
| apiKey: process.env.REACT_APP_API_KEY, | ||
| authDomain: process.env.REACT_APP_AUTH_DOMAIN, | ||
| projectId: process.env.REACT_APP_PROJECT_ID, | ||
| storageBucket: process.env.REACT_APP_STORAGE_BUCKET, | ||
| messagingSenderId: process.env.REACT_APP_SENDER_ID, | ||
| appId: process.env.REACT_APP_APP_ID, | ||
| }; | ||
|
|
||
| // Initialize Firebase | ||
| const app = initializeApp(firebaseConfig); | ||
| export default app; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Compare objects for equality | ||
| function isEqual(objA: any, objB: any): boolean { | ||
| return JSON.stringify(objA) === JSON.stringify(objB); | ||
| } | ||
|
|
||
| export {isEqual}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't commit env values