import { getDocs, collection, getFirestore, query } from 'firebase/firestore' export const useContestStore = defineStore('contest', () => { // General const db = getFirestore() // Contest competitors const competitors = ref([]) const getCompetitors = async () => { if (competitors.value[0]) return const querySnapshot = await getDocs(collection(db, "competitors")) querySnapshot.forEach((doc) => { const data = doc.data() data.checked = true competitors.value.push(data) }) } const selectCompetitors = (x, relatiecodes) => { if (x === 'user') { competitors.value.forEach(competitor => { if (relatiecodes.includes(competitor.relatiecode)) { competitor.checked = true } else { competitor.checked = false } }) } else if (x === 'all') { competitors.value.forEach(competitor => competitor.checked = true) } else if (x === 'none') { competitors.value.forEach(competitor => competitor.checked = false) } } // Contest Timings const timings = ref([]) const filteredTimings = computed(() => { timings.value = timings.value.sort((a, b) => a.time.combined.localeCompare(b.time.combined)) timings.value.forEach((time, index) => { if (time.dsq === true) { timings.value.push(timings.value.splice(index, 1)[0]) } }) return timings.value.filter(a => competitors.value.filter(b => b.checked === true).map(x => x.relatiecode).includes(a.relatiecode)) }) const getTimings = async () => { if (timings.value[0]) return; if (!competitors.value[0]) await getCompetitors() const timingsRef = collection(db, "timings"); const q = query(timingsRef); const querySnapshot = await getDocs(q); querySnapshot.forEach((doc) => { const data = doc.data() data.id = doc.id data.contest.date = data.contest.date.toDate() timings.value.push(data) }); } return { competitors, getCompetitors, getTimings, timings, filteredTimings, selectCompetitors } })