-
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: add global statistics about number of questions
feat: add question progress on quiz feat: add global statistics about the questions
- Loading branch information
Showing
10 changed files
with
209 additions
and
8 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,48 @@ | ||
import { useEffect, useState } from "react" | ||
import { findStatistics } from "../services/statistics-store" | ||
import { AppStatistics, StoreResponse } from "../utils/types" | ||
import { getQuestionTagValue } from "../utils/utils" | ||
|
||
export const QuestionStatistics = () => { | ||
const [orderedTags, setOrderedTags] = useState([] as string[]) | ||
const [quantities, setQuantities] = useState({} as Record<string, number>) | ||
//const statistics = useMemo(findStatistics, []) | ||
|
||
useEffect(() => { | ||
findStatistics().then((response: StoreResponse<AppStatistics>) => { | ||
console.log(response) | ||
if (response.success) { | ||
const data = response.data[0].questions.quantity as Record<string, number> | ||
// Sort tags by value | ||
const keysTags = Object.keys(data) | ||
keysTags.sort((a, b) => data[b] - data[a]) | ||
// Remove total key | ||
keysTags.splice(keysTags.indexOf("total"), 1) | ||
// Set up sates | ||
setOrderedTags(keysTags) | ||
setQuantities(data) | ||
} | ||
}) | ||
}, []) | ||
|
||
// Wait for statistics to be loaded | ||
if (quantities.length === 0) { | ||
return <div className="w-full sm:w-auto px-4 py-8 sm:px-8 rounded-3xl">Waiting</div> | ||
} | ||
|
||
return ( | ||
<div className="flex flex-row space-x-2"> | ||
<span key="total" className="badge badge-lg badge-outline cursor-default"> | ||
{quantities["total"] + " questions !"} | ||
</span> | ||
{orderedTags.slice(0, 3).map((tag) => ( | ||
<span key={tag} className="badge badge-lg badge-outline cursor-default"> | ||
{quantities[tag] + " en " + getQuestionTagValue(tag)} | ||
</span> | ||
))} | ||
<span key="others" className="badge badge-lg badge-outline cursor-default"> | ||
... | ||
</span> | ||
</div> | ||
) | ||
} |
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
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,55 @@ | ||
import { FieldValue, collection, doc, documentId, increment, query, updateDoc, where } from "firebase/firestore" | ||
import { db } from "../config/firebase" | ||
import { findDataByQuery } from "./store" | ||
import { AppStatisticsSchema, QuestionData } from "../utils/types" | ||
import { getErrorStoreResponse, getSuccessStoreResponse } from "../utils/utils" | ||
|
||
const appRef = collection(db, "app") | ||
|
||
/* Basic CRUD methods */ | ||
|
||
/** | ||
* Finds the statistics in app collection | ||
* @returns | ||
*/ | ||
export async function findStatistics() { | ||
const q = query(appRef, where(documentId(), "==", "statistics")) | ||
const response = await findDataByQuery(q, AppStatisticsSchema) | ||
return response | ||
} | ||
|
||
/** | ||
* Updates statistics data. | ||
* @param data - Format as {"a.b":c} | ||
* @returns | ||
*/ | ||
export async function updateStatistics(data: object) { | ||
try { | ||
const statisticsRef = doc(db, "app/statistics") | ||
await updateDoc(statisticsRef, data) | ||
} catch (e) { | ||
return getErrorStoreResponse(e) | ||
} | ||
|
||
return getSuccessStoreResponse([data]) | ||
} | ||
|
||
/* Domain methods */ | ||
|
||
/** | ||
* Increments the number of questions by 1 for each tag. | ||
* Updates db statistics. | ||
* @param tags | ||
* @returns | ||
*/ | ||
export async function updateStatisticsByQuestion(question: QuestionData) { | ||
const data = { "questions.quantity.total": increment(1) } as Record<string, FieldValue> | ||
|
||
// Increment questions quantity for each tag | ||
for (const tag of question.tags) { | ||
data[`questions.quantity.${tag}`] = increment(1) | ||
} | ||
|
||
const response = await updateStatistics(data) | ||
return response | ||
} |
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