-
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.
feat: implement project store with pinia
- Loading branch information
Showing
7 changed files
with
135 additions
and
41 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { collection, doc, getDocs, setDoc } from "firebase/firestore"; | ||
import { defineStore } from "pinia"; | ||
import { ref } from "vue"; | ||
|
||
import { firestore } from "./firebase"; | ||
import { Project } from "./types"; | ||
|
||
export const useProjectStore = defineStore("projects", () => { | ||
const projects = ref<Project[]>([]); | ||
|
||
const fetchProjects = () => { | ||
const projectsCollection = collection(firestore, "projects"); | ||
|
||
return getDocs(projectsCollection) | ||
.then((querySnapshot) => { | ||
return querySnapshot.docs.map( | ||
(doc) => | ||
({ | ||
_id: doc.id, | ||
...doc.data(), | ||
}) as Project, | ||
); | ||
}) | ||
.catch((error) => { | ||
console.log("Error getting documents: ", error); | ||
|
||
return [] as Project[]; | ||
}); | ||
}; | ||
|
||
const refetch = () => { | ||
fetchProjects().then((data) => { | ||
projects.value = data; | ||
}); | ||
}; | ||
refetch(); | ||
|
||
function upvoteProject(_id: string, uid?: string) { | ||
const project = projects.value.find((p) => p._id === _id); | ||
if (!project) return; | ||
const projectData = { | ||
...project, | ||
upvotes: uid | ||
? project.upvotes.includes(uid) | ||
? project.upvotes.filter((projectUid) => projectUid !== uid) | ||
: [...project.upvotes, uid] | ||
: project.upvotes, | ||
}; | ||
setDoc(doc(firestore, `projects/${_id}`), projectData) | ||
.then(() => { | ||
projects.value = projects.value.map((p) => | ||
p._id === _id ? projectData : p, | ||
); | ||
// refetch(); | ||
}) | ||
.catch((error) => { | ||
console.error("Error writing document: ", error); | ||
}); | ||
} | ||
|
||
return { | ||
projects, | ||
refetch, | ||
upvoteProject, | ||
}; | ||
}); |
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