-
Notifications
You must be signed in to change notification settings - Fork 0
Added surveys and survey making functionality #55
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
Open
mafernandag
wants to merge
7
commits into
main
Choose a base branch
from
surveys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4db87cf
Added survey tab to admin dashboard and survey edit page
mafernandag 3fed321
Added survey form page + functionality
mafernandag 0ec5a98
Merge branch 'surveys'
mafernandag 4380822
Added answer table
mafernandag c99ccdf
Added active surveys section, complete survey page and minor fixes
mafernandag 3b6ba0b
Check fixes and layout modifications
mafernandag e66bb1a
Package.json fix
mafernandag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -30,3 +30,4 @@ yarn-error.log* | |
|
|
||
| server/firebase-service-account-key.json | ||
| functions/node_modules/ | ||
|
|
||
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,217 @@ | ||
| import { | ||
| doc, | ||
| setDoc, | ||
| collection, | ||
| deleteDoc, | ||
| getDoc, | ||
| getDocs, | ||
| addDoc, | ||
| serverTimestamp, | ||
| onSnapshot, | ||
| query, | ||
| orderBy, | ||
| updateDoc, | ||
| increment | ||
| } from 'firebase/firestore' | ||
| import { db } from './firebaseConfig' | ||
| import { getUserById } from './users' | ||
|
|
||
| export const createEmptySurvey = async (userId) => { | ||
| try { | ||
| const surveyRef = doc(collection(db, 'surveys')) | ||
| const user = await getUserById(userId) | ||
| if (!user) { | ||
| throw new Error('User not found') | ||
| } | ||
| const author = { id: userId, name: user.displayName, email: user.email } | ||
| await setDoc(surveyRef, { | ||
| author, | ||
| title: 'Untitled Survey', | ||
| description: '', | ||
| createdAt: serverTimestamp(), | ||
| publishedAt: null, | ||
| responses: 0, | ||
| questionsCount: 0, | ||
| enabledFor: { mentors: false, mentees: false }, | ||
| status: 'draft' | ||
| }) | ||
|
|
||
| return surveyRef.id | ||
| } catch (error) { | ||
| console.error('Error creating survey:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export const deleteSurvey = async (surveyId) => { | ||
| try { | ||
| const surveyRef = doc(db, 'surveys', surveyId) | ||
| await deleteDoc(surveyRef) | ||
| return { ok: true } | ||
| } catch (error) { | ||
| console.error('Error deleting survey:', error) | ||
| return { ok: false, error: error.message } | ||
| } | ||
| } | ||
|
|
||
| export const getSurveyById = async (id) => { | ||
| if (!id) { | ||
| throw new Error('getSurveyById: missing survey id') | ||
| } | ||
| try { | ||
| // Ensure we never pass undefined into doc() | ||
| const ref = doc(collection(db, 'surveys'), String(id)) | ||
| const snap = await getDoc(ref) | ||
| if (!snap.exists()) { | ||
| throw new Error('Survey not found') | ||
| } | ||
|
|
||
| // Fetch questions ordered by "order" | ||
| const questionsRef = collection(db, 'surveys', id, 'questions') | ||
| const questionsSnap = await getDocs(query(questionsRef, orderBy('order', 'asc'))) | ||
| const questions = questionsSnap.docs.map((d) => ({ id: d.id, ...d.data() })) | ||
|
|
||
| return { id: snap.id, ...snap.data(), questions } | ||
| } catch (error) { | ||
| console.error('Error fetching survey by ID:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export const getAllSurveys = async () => { | ||
| try { | ||
| const surveysCol = collection(db, 'surveys') | ||
| const surveySnapshot = await getDocs(surveysCol) | ||
| const surveys = surveySnapshot.docs.map(doc => ({ id: doc.id, ...doc.data() })) | ||
| // console.log('Fetched surveys:', surveys) | ||
| return surveys | ||
| } catch (error) { | ||
| console.error('Error fetching surveys:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| export const getSurveysByStatusAndUserRole = async (status, role) => { | ||
| const surveys = await getAllSurveys() | ||
|
|
||
| // normalize role to 'mentors' | 'mentees' | ||
| const normalizeRole = (r) => { | ||
| if (!r) return null | ||
| const v = String(r).toLowerCase() | ||
| if (v === 'mentor') return 'mentors' | ||
| if (v === 'mentee') return 'mentees' | ||
| return null | ||
| } | ||
| const roleKey = normalizeRole(role) | ||
|
|
||
| const filteredSurveys = surveys.filter((survey) => { | ||
| const matchesStatus = survey.status === status | ||
| const matchesRole = roleKey ? !!(survey.enabledFor && survey.enabledFor[roleKey]) : true | ||
| return matchesStatus && matchesRole | ||
| }) | ||
|
|
||
| return filteredSurveys | ||
| } | ||
|
|
||
| // Adds a new document in subcollection surveys/{surveyId}/questions | ||
| // questionData: { title, type, options?, required?, description?, order? } | ||
| export const addQuestionToSurvey = async (surveyId, questionType, questionData = {}) => { | ||
| try { | ||
| const surveyRef = doc(db, 'surveys', surveyId) | ||
| const questionsCol = collection(surveyRef, 'questions') | ||
|
|
||
| const emptyDefault = { | ||
| title: 'New Question', | ||
| type: questionType, | ||
| isRequired: false, | ||
| options: [], | ||
| description: '', | ||
| order: Date.now() | ||
| } | ||
|
|
||
| const newQuestion = { | ||
| ...emptyDefault, | ||
| ...(questionData || {}) | ||
| } | ||
|
|
||
| const qRef = await addDoc(questionsCol, newQuestion) | ||
| // increment questionsCount | ||
| await updateDoc(surveyRef, { | ||
| questionsCount: increment(1), | ||
| updatedAt: serverTimestamp() | ||
| }) | ||
|
|
||
| return qRef.id | ||
| } catch (error) { | ||
| console.error('Error adding question to survey:', error) | ||
| throw error | ||
| } | ||
| } | ||
|
|
||
| // Subscribe to survey meta changes | ||
| export const subscribeToSurvey = (surveyId, callback) => { | ||
| const surveyRef = doc(db, 'surveys', surveyId) | ||
| return onSnapshot(surveyRef, callback) | ||
| } | ||
|
|
||
| // Subscribe to questions (ordered) | ||
| export const subscribeToQuestions = (surveyId, callback) => { | ||
| const qRef = collection(db, 'surveys', surveyId, 'questions') | ||
| const q = query(qRef, orderBy('order', 'asc')) | ||
| return onSnapshot(q, (snap) => { | ||
| const list = snap.docs.map((d) => ({ id: d.id, ...d.data() })) | ||
| callback(list) | ||
| }) | ||
| } | ||
|
|
||
| // Update survey metadata (debounced from UI) | ||
| export const updateSurveyMeta = async (surveyId, patch) => { | ||
| const surveyRef = doc(db, 'surveys', surveyId) | ||
| try { | ||
| await updateDoc(surveyRef, { ...patch, updatedAt: serverTimestamp() }) | ||
| return { ok: true } | ||
| } catch (error) { | ||
| console.error('Error updating survey meta:', error) | ||
| return { ok: false, error: error.message } | ||
| } | ||
| } | ||
|
|
||
| // Upsert a question (debounced from UI) | ||
| export const upsertQuestion = async (surveyId, questionId, patch) => { | ||
| const qRef = doc(db, 'surveys', surveyId, 'questions', questionId) | ||
| await setDoc(qRef, { ...patch }, { merge: true }) | ||
| } | ||
|
|
||
| // Delete a question and decrement count | ||
| export const deleteQuestionFromSurvey = async (surveyId, questionId) => { | ||
| const surveyRef = doc(db, 'surveys', surveyId) | ||
| const qRef = doc(db, 'surveys', surveyId, 'questions', questionId) | ||
| await deleteDoc(qRef) | ||
| await updateDoc(surveyRef, { | ||
| questionsCount: increment(-1), | ||
| updatedAt: serverTimestamp() | ||
| }) | ||
| } | ||
|
|
||
| export const submitSurveyResponse = async (surveyId, responses, userId) => { | ||
| try { | ||
| const responsesCol = collection(db, 'surveys', surveyId, 'responses') | ||
| const docRef = await addDoc(responsesCol, { answers: responses, submittedAt: serverTimestamp(), userId }) | ||
| return { ok: true, id: docRef.id } | ||
| } catch (error) { | ||
| console.error('Error submitting survey response:', error) | ||
| return { ok: false, error: error.message } | ||
| } | ||
| } | ||
|
|
||
| export const getSurveyResponses = async (surveyId) => { | ||
| try { | ||
| const responsesCol = collection(db, 'surveys', surveyId, 'responses') | ||
| const responsesSnap = await getDocs(responsesCol) | ||
| const responses = responsesSnap.docs.map(doc => ({ id: doc.id, ...doc.data() })) | ||
| return { ok: true, responses } | ||
| } catch (error) { | ||
| console.error('Error fetching survey responses:', error) | ||
| return { ok: false, error: error.message } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
React and react-dom versions are pinned to exact versions (removed
^prefix). This prevents automatic minor and patch updates, which may cause issues with dependency resolution and miss important bug fixes. Consider using semantic versioning with the caret operator (^18.2.0) unless there's a specific reason for pinning.