-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Saba-Var/feat/firebase_integration
feat: firebase integration
- Loading branch information
Showing
16 changed files
with
1,771 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
VITE_FIREBASE_API_KEY=firebase_api_key | ||
VITE_FIREBASE_AUTH_DOMAIN=firebase_auth_domain | ||
VITE_FIREBASE_PROJECT_ID=firebase_project_id | ||
VITE_FIREBASE_STORAGE_BUCKET=firebase_storage_bucket | ||
VITE_FIREBASE_MESSAGING_SENDER=firebase_messaging_sender | ||
VITE_FIREBASE_APP_ID=firebase_app_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ dist-ssr | |
coverage | ||
*.local | ||
|
||
.env | ||
|
||
/cypress/videos/ | ||
/cypress/screenshots/ | ||
|
||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getFirestore } from 'firebase/firestore' | ||
import { initializeApp } from 'firebase/app' | ||
|
||
const firebaseConfig = { | ||
apiKey: import.meta.env.VITE_VITE_FIREBASE_API_KEY, | ||
authDomain: import.meta.env.VITE_FIREBASE_AUTH_DOMAIN, | ||
projectId: import.meta.env.VITE_FIREBASE_PROJECT_ID, | ||
storageBucket: import.meta.env.VITE_FIREBASE_STORAGE_BUCKET, | ||
messagingSenderId: import.meta.env.VITE_FIREBASE_MESSAGING_SENDER, | ||
appId: import.meta.env.VITE_FIREBASE_APP_ID | ||
} | ||
|
||
const app = initializeApp(firebaseConfig) | ||
|
||
export const firebaseDb = getFirestore(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './firebase' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './notes' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { useToast } from 'vue-toastification' | ||
import { firebaseDb } from '@/services' | ||
import { defineStore } from 'pinia' | ||
import { ref, computed } from 'vue' | ||
import { | ||
collection, | ||
onSnapshot, | ||
deleteDoc, | ||
updateDoc, | ||
orderBy, | ||
addDoc, | ||
query, | ||
doc | ||
} from 'firebase/firestore' | ||
|
||
export const useNotesStore = defineStore('notes', () => { | ||
const notesCollection = collection(firebaseDb, 'notes') | ||
const toast = useToast() | ||
|
||
const notesLoading = ref(false) | ||
const notes = ref([]) | ||
|
||
const getNoteContent = computed(() => { | ||
return (id) => { | ||
const note = notes.value?.find((note) => note.id === id) | ||
return note ? note.content : null | ||
} | ||
}) | ||
|
||
const totalNotesCount = computed(() => notes.value.length) | ||
|
||
const totalCharactersCount = computed(() => { | ||
return notes.value.reduce((acc, note) => acc + note.content.length, 0) | ||
}) | ||
|
||
const getAllNotes = async () => { | ||
try { | ||
notesLoading.value = true | ||
|
||
const q = query(notesCollection, orderBy('createdAt', 'desc')) | ||
onSnapshot(q, (querySnapshot) => { | ||
const allNotes = [] | ||
|
||
querySnapshot.forEach((doc) => { | ||
allNotes.push({ | ||
id: doc.id, | ||
...doc.data() | ||
}) | ||
}) | ||
|
||
notes.value = allNotes | ||
notesLoading.value = false | ||
}) | ||
} catch (error) { | ||
toast.error('Notes could not be fetched!') | ||
} | ||
} | ||
|
||
const noteAddHandler = async (newNote, cb = () => {}) => { | ||
try { | ||
await addDoc(notesCollection, { | ||
createdAt: new Date().toISOString(), | ||
content: newNote.trim(), | ||
updatedAt: null | ||
}) | ||
|
||
toast.success('Note added successfully!') | ||
cb() | ||
} catch (error) { | ||
toast.error('Note could not be added!') | ||
} | ||
} | ||
|
||
const editNoteHandler = async (id, newContent) => { | ||
try { | ||
await updateDoc(doc(notesCollection, id), { | ||
content: newContent, | ||
updatedAt: new Date().toISOString() | ||
}) | ||
|
||
toast.success('Note updated successfully!') | ||
} catch (error) { | ||
console.log(error) | ||
toast.error('Note could not be updated!') | ||
} | ||
} | ||
|
||
const deleteNoteHandler = async (id) => { | ||
try { | ||
await deleteDoc(doc(notesCollection, id)) | ||
toast.success('Note deleted successfully!') | ||
} catch (error) { | ||
toast.error('Note could not be deleted!') | ||
} | ||
} | ||
|
||
return { | ||
totalCharactersCount, | ||
deleteNoteHandler, | ||
totalNotesCount, | ||
editNoteHandler, | ||
getNoteContent, | ||
noteAddHandler, | ||
notesLoading, | ||
getAllNotes, | ||
notes | ||
} | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.