2022-09-16 21:11:44 +02:00
|
|
|
<template>
|
2023-01-27 19:09:09 +01:00
|
|
|
<div @click.self="showModel = false" v-if="showModel" class="fixed flex justify-center items-center h-screen w-full bg-black top-0 left-0 z-50 bg-opacity-50" >
|
|
|
|
<form @submit.prevent="submitModelForm" class="dark:bg-neutral-800 bg-neutral-200 p-10 rounded-xl flex flex-col w-full max-w-sm">
|
|
|
|
<h1 class="font-bold text-center text-lg mb-5">Beheer Persoon</h1>
|
|
|
|
|
|
|
|
<div class="text-default mb-2">
|
|
|
|
Relatiecode: <b>{{ modelData.relatiecode }}</b>
|
|
|
|
</div>
|
|
|
|
<div class="text-default mb-2">
|
|
|
|
Naam: <b>{{ modelData.fullName }}</b>
|
|
|
|
</div>
|
|
|
|
<div class="text-default mb-5">
|
|
|
|
Groepen: <b>{{ modelData.groups.join(', ') }}</b>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<label class="relative inline-flex items-center cursor-pointer mb-5">
|
|
|
|
<input type="checkbox" value="" v-model="modelData.wedstrijdteam" class="sr-only peer">
|
2023-02-02 19:35:03 +01:00
|
|
|
<div class="w-11 h-6 bg-neutral-300 peer-focus:outline-none peer-focus:ring-none rounded-full peer dark:bg-neutral-700 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-primary"></div>
|
2023-01-27 19:09:09 +01:00
|
|
|
<span class="ml-3 font-medium text-gray-900 dark:text-gray-300">Wedstrijd Team</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input :disabled="disableButtons" type="submit" class="btn" :value="'Bewerken'" />
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<div class="flex flex-col gap-5 mx-auto p-2 w-full max-w-md">
|
|
|
|
<div class="mb-5">
|
|
|
|
<form @submit.prevent="submitLedenlijst" class="flex flex-col">
|
|
|
|
<input required="true" @change="handleFileChanged" accept=".csv" class="my-2" type="file">
|
|
|
|
<span class="text-sm"><i>Met de volgende kolommen: Relatiecode, Volledige naam(1), Roepnaam, E-mail, 2e E-mail, Verenigingssporten, Diploma</i></span>
|
|
|
|
<button :disabled="disableButtons" class="btn mx-auto mt-2">Publish Ledenlijst</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<div class="flex flex-col gap-3">
|
|
|
|
<input v-model="searchTerm" class="input mb-2 font-bold" type="search" placeholder="Search">
|
|
|
|
<div v-for="lid in filteredLedenlijst" :key="lid.relatiecode">
|
|
|
|
<div @click="handleModel(lid)" class="item container flex flex-wrap hover:cursor-pointer">
|
|
|
|
<b class="w-24">{{ lid.relatiecode }}</b> {{ lid.fullName }}
|
|
|
|
</div>
|
2022-09-19 15:11:13 +02:00
|
|
|
</div>
|
2022-09-16 21:11:44 +02:00
|
|
|
</div>
|
2023-01-27 19:09:09 +01:00
|
|
|
</div>
|
2022-09-16 21:11:44 +02:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
2023-02-05 22:47:13 +01:00
|
|
|
import { doc, getDocs, collection, writeBatch, updateDoc, setDoc } from "firebase/firestore";
|
2022-09-18 20:07:37 +02:00
|
|
|
import { useToast } from 'vue-toastification'
|
|
|
|
|
2022-09-16 21:11:44 +02:00
|
|
|
definePageMeta({
|
2023-01-27 19:09:09 +01:00
|
|
|
title: 'Ledenlijst',
|
|
|
|
key: 'back'
|
2022-09-16 21:11:44 +02:00
|
|
|
})
|
2022-09-18 10:47:43 +02:00
|
|
|
|
2022-12-17 13:01:49 +01:00
|
|
|
const { db, ledenlijst, users } = inject('firebase')
|
2022-09-19 15:11:13 +02:00
|
|
|
|
2022-09-18 20:07:37 +02:00
|
|
|
const toast = useToast()
|
|
|
|
|
2023-01-27 19:09:09 +01:00
|
|
|
const modelData = ref(null)
|
|
|
|
|
2022-09-18 10:47:43 +02:00
|
|
|
const file = ref(null)
|
|
|
|
const disableButtons = ref(false)
|
2022-09-19 15:11:13 +02:00
|
|
|
const searchTerm = ref('')
|
2023-01-27 15:08:58 +01:00
|
|
|
const newLedenlijst = ref([])
|
2023-01-27 19:09:09 +01:00
|
|
|
const showModel = ref(false)
|
2022-09-19 15:11:13 +02:00
|
|
|
|
|
|
|
onMounted(async () => {
|
2023-01-26 11:34:52 +01:00
|
|
|
if (!ledenlijst.value.length) {
|
|
|
|
try {
|
|
|
|
const querySnapshot = await getDocs(collection(db, "ledenlijst"));
|
|
|
|
querySnapshot.forEach((doc) => {
|
|
|
|
ledenlijst.value.push(doc.data())
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
2022-09-19 15:11:13 +02:00
|
|
|
}
|
2023-01-26 11:34:52 +01:00
|
|
|
|
|
|
|
ledenlijst.value.sort((a, b) => a.fullName.localeCompare(b.fullName))
|
|
|
|
}
|
2022-09-19 15:11:13 +02:00
|
|
|
})
|
|
|
|
|
2023-01-27 19:09:09 +01:00
|
|
|
const handleModel = (lid) => {
|
|
|
|
modelData.value = lid
|
|
|
|
if (!modelData.value.hasOwnProperty('wedstrijdteam')) modelData.value.wedstrijdteam = false
|
|
|
|
|
2023-02-05 22:47:13 +01:00
|
|
|
modelData.value.oldWedstrijdteam = modelData.value.wedstrijdteam
|
|
|
|
|
2023-01-27 19:09:09 +01:00
|
|
|
showModel.value = true
|
|
|
|
}
|
|
|
|
|
|
|
|
const submitModelForm = async () => {
|
|
|
|
disableButtons.value = true
|
|
|
|
ledenlijst.value.filter(a => a.relatiecode === modelData.value.relatiecode)[0].wedstrijdteam = modelData.value.wedstrijdteam
|
|
|
|
|
2023-02-05 22:47:13 +01:00
|
|
|
if (modelData.value.wedstrijdteam === modelData.value.oldWedstrijdteam) {
|
|
|
|
disableButtons.value = false
|
|
|
|
showModel.value = false
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let docRef = doc(db, "ledenlijst", modelData.value.relatiecode)
|
2023-01-27 19:09:09 +01:00
|
|
|
|
|
|
|
await updateDoc(docRef, {
|
|
|
|
wedstrijdteam: modelData.value.wedstrijdteam
|
|
|
|
})
|
|
|
|
|
2023-02-05 22:47:13 +01:00
|
|
|
docRef = doc(db, "competitors", modelData.value.relatiecode)
|
|
|
|
|
|
|
|
if (modelData.value.wedstrijdteam) {
|
|
|
|
await setDoc(docRef, {
|
|
|
|
relatiecode: modelData.value.relatiecode,
|
|
|
|
name: modelData.value.fullName,
|
|
|
|
active: true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await updateDoc(docRef, {
|
|
|
|
active: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-27 19:09:09 +01:00
|
|
|
toast.success('Persoon is bewerkt')
|
|
|
|
|
|
|
|
disableButtons.value = false
|
|
|
|
showModel.value = false
|
|
|
|
}
|
|
|
|
|
2022-09-19 15:11:13 +02:00
|
|
|
const filteredLedenlijst = computed(() => {
|
2023-01-26 11:34:52 +01:00
|
|
|
return ledenlijst.value.filter(lid => lid.fullName.toLowerCase().includes(searchTerm.value.toLowerCase()))
|
2022-09-19 15:11:13 +02:00
|
|
|
})
|
2022-09-18 10:47:43 +02:00
|
|
|
|
|
|
|
const handleFileChanged = (event) => {
|
2023-01-26 11:34:52 +01:00
|
|
|
const target = event.target;
|
2022-09-18 10:47:43 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
if (target && target.files) {
|
|
|
|
file.value = target.files[0];
|
|
|
|
}
|
2022-09-18 10:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const submitLedenlijst = () => {
|
2023-01-26 11:34:52 +01:00
|
|
|
disableButtons.value = true
|
2022-09-18 10:47:43 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
let reader = new FileReader()
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
reader.onload = function() {
|
|
|
|
csvToJson(reader.result);
|
|
|
|
};
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
reader.onerror = function() {
|
|
|
|
console.log(reader.error);
|
|
|
|
};
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
reader.readAsText(file.value)
|
2022-09-18 20:07:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const csvToJson = (csv) => {
|
2023-01-26 11:34:52 +01:00
|
|
|
let arr = csv.split('\n');
|
|
|
|
|
|
|
|
var result = [];
|
|
|
|
var headers = arr[0].split(';');
|
|
|
|
for(var i = 1; i < arr.length; i++) {
|
|
|
|
var data = arr[i].split(';');
|
|
|
|
var obj = {};
|
|
|
|
for(var j = 0; j < data.length; j++) {
|
|
|
|
obj[headers[j].trim()] = data[j].trim();
|
2022-12-29 12:54:58 +01:00
|
|
|
}
|
2023-01-26 11:34:52 +01:00
|
|
|
result.push(obj);
|
|
|
|
}
|
2022-12-29 12:54:58 +01:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
if (!Object.hasOwn(result[0], 'Relatiecode') || !Object.hasOwn(result[0], 'Volledige naam (1)') || !Object.hasOwn(result[0], 'E-mail') || !Object.hasOwn(result[0], '2e E-mail') || !Object.hasOwn(result[0], 'Verenigingssporten') || !Object.hasOwn(result[0], 'Diploma dropdown 1')) return toast.error('Missing properties')
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
newLedenlijst.value = []
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
for (let i in result) {
|
|
|
|
let groups = []
|
|
|
|
let correctGroups = null
|
2023-01-27 15:08:58 +01:00
|
|
|
if (!result[i].Relatiecode) break;
|
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
if (result[i].Verenigingssporten.includes(',')) correctGroups = result[i].Verenigingssporten.split(',')
|
|
|
|
else correctGroups = [result[i].Verenigingssporten]
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-26 11:34:52 +01:00
|
|
|
correctGroups.forEach(group => {
|
|
|
|
const x = group.split(' - ')
|
|
|
|
|
|
|
|
if (x[2] === 'Week') groups.push('Vrijdag')
|
|
|
|
else if (x[2] === 'Zaterdag' && x[1] !== 'Wedstrijd') groups.push('Zaterdag')
|
|
|
|
|
|
|
|
groups.push(x[1])
|
|
|
|
})
|
|
|
|
|
|
|
|
if (groups[2] === 'Week') groups[2] = 'Vrijdag'
|
2022-09-18 20:07:37 +02:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
newLedenlijst.value.push({ relatiecode: result[i].Relatiecode, fullName: result[i]['Volledige naam (1)'], email: [result[i]['E-mail'], result[i]['2e E-mail']], groups: [...new Set(groups)], diploma: result[i]['Diploma dropdown 1'] })
|
2023-01-26 11:34:52 +01:00
|
|
|
}
|
|
|
|
uploadLedenlijst()
|
2022-09-19 15:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const uploadLedenlijst = async () => {
|
2023-01-27 15:08:58 +01:00
|
|
|
try {
|
|
|
|
const batch = writeBatch(db)
|
2022-09-19 15:11:13 +02:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
newLedenlijst.value.forEach(lid => {
|
|
|
|
const docRef = doc(db, "ledenlijst", lid.relatiecode)
|
2022-09-19 15:11:13 +02:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
const exists = ledenlijst.value.filter(a => a.relatiecode === lid.relatiecode).length > 1
|
2022-09-19 15:11:13 +02:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
if (!exists) {
|
|
|
|
return batch.set(docRef, lid);
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.update(docRef, { fullName: lid.relatiecode, email: lid.email, groups: lid.groups, diploma: lid.diploma})
|
|
|
|
})
|
|
|
|
|
|
|
|
const deleteLeden = ledenlijst.value.filter(a => newLedenlijst.value.map(x => x.relatiecode).indexOf(a.relatiecode) === -1)
|
2022-12-17 13:01:49 +01:00
|
|
|
|
2023-01-27 15:08:58 +01:00
|
|
|
deleteLeden.forEach(lid => {
|
|
|
|
const docRef = doc(db, "ledenlijst", lid.relatiecode)
|
|
|
|
|
|
|
|
batch.delete(docRef)
|
|
|
|
})
|
|
|
|
|
|
|
|
await batch.commit();
|
|
|
|
|
|
|
|
toast.success('Published ledenlijst')
|
|
|
|
} catch (e) {
|
|
|
|
toast.error("Error updating ledenlijst");
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
ledenlijst.value = newLedenlijst.value
|
|
|
|
|
|
|
|
updateUsers()
|
2022-12-17 13:01:49 +01:00
|
|
|
}
|
|
|
|
|
2022-09-19 15:11:13 +02:00
|
|
|
|
2022-12-17 13:01:49 +01:00
|
|
|
const updateUsers = async () => {
|
|
|
|
try {
|
|
|
|
const querySnapshot = await getDocs(collection(db, "users"));
|
|
|
|
querySnapshot.forEach((doc) => {
|
|
|
|
const data = doc.data()
|
|
|
|
data.id = doc.id
|
|
|
|
users.value.push(data)
|
|
|
|
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
|
|
|
|
toast.error('Error getting users')
|
|
|
|
}
|
|
|
|
|
|
|
|
const batch = writeBatch(db);
|
|
|
|
|
|
|
|
users.value.forEach(user => {
|
|
|
|
const lid = ledenlijst.value.filter(a => a.email.includes(user.email))
|
|
|
|
const newRelatiecodes = lid.map(a => a.relatiecode)
|
|
|
|
|
|
|
|
user.allRelatiecodes = newRelatiecodes
|
|
|
|
user.relatiecodes.forEach((relatiecode, index) => {
|
2023-01-27 15:08:58 +01:00
|
|
|
if (!newRelatiecodes.includes(relatiecode)) { user.relatiecodes.splice(index, 1); console.log('removed item', relatiecode)}
|
2022-12-17 13:01:49 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
const userRef = doc(db, "users", user.id)
|
|
|
|
batch.update(userRef, user)
|
|
|
|
})
|
|
|
|
|
|
|
|
await batch.commit();
|
|
|
|
|
|
|
|
disableButtons.value = false
|
2022-09-18 10:47:43 +02:00
|
|
|
}
|
2022-12-17 13:01:49 +01:00
|
|
|
</script>
|