wrbapp/frontend/pages/settings/admin/ledenlijst.vue

205 lines
5.9 KiB
Vue
Raw Normal View History

2022-09-16 21:11:44 +02:00
<template>
2022-09-19 15:11:13 +02:00
<div class="flex flex-col gap-5 mx-auto p-2 w-full max-w-md">
<div class="mb-5">
2022-09-18 10:47:43 +02:00
<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>
2022-09-20 17:33:15 +02:00
<button :disabled="disableButtons" class="enabled:btn mx-auto mt-2">Publish Ledenlijst</button>
2022-09-18 10:47:43 +02:00
</form>
</div>
2022-09-19 15:11:13 +02:00
<div class="flex flex-col gap-3">
2022-09-19 22:06:26 +02:00
<input v-model="searchTerm" class="input mb-2 font-bold" type="search" placeholder="Search">
2022-09-19 15:11:13 +02:00
<div v-for="lid in filteredLedenlijst" :key="lid.relatiecode">
<div class="item container flex flex-wrap">
<b class="w-24">{{ lid.relatiecode }}</b> {{ lid.fullName }}
</div>
</div>
</div>
2022-09-16 21:11:44 +02:00
</div>
</template>
<script setup>
2022-12-17 13:01:49 +01:00
import { doc, getDocs, collection, writeBatch, runTransaction } 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({
title: 'Ledenlijst',
key: 'back'
})
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()
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('')
onMounted(async () => {
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)
}
ledenlijst.value.sort((a, b) => a.fullName.localeCompare(b.fullName))
}
})
const filteredLedenlijst = computed(() => {
return ledenlijst.value.filter(lid => lid.fullName.toLowerCase().includes(searchTerm.value.toLowerCase()))
})
2022-09-18 10:47:43 +02:00
const handleFileChanged = (event) => {
const target = event.target;
if (target && target.files) {
file.value = target.files[0];
}
}
const submitLedenlijst = () => {
disableButtons.value = true
2022-09-18 20:07:37 +02:00
let reader = new FileReader()
reader.onload = function() {
csvToJson(reader.result);
};
reader.onerror = function() {
console.log(reader.error);
};
reader.readAsText(file.value)
}
const csvToJson = (csv) => {
2022-12-29 12:54:58 +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();
}
result.push(obj);
}
/*
2022-09-18 20:07:37 +02:00
let array = csv.split("\n")
let result = [];
let headers = array[0].split(",")
for (let i = 1; i < array.length - 1; i++) {
let obj = {}
let str = array[i]
let s = ''
let flag = 0
for (let ch of str) {
if (ch === '"' && flag === 0) {
flag = 1
}
else if (ch === '"' && flag == 1) flag = 0
if (ch === ',' && flag === 0) ch = '|'
if (ch !== '"') s += ch
}
let properties = s.split("|")
for (let j in headers) {
if (properties[j].includes(",")) {
obj[headers[j]] = properties[j]
.split(",").map(item => item.trim())
}
else obj[headers[j]] = properties[j]
}
result.push(obj)
}
2022-12-29 12:54:58 +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
2022-09-19 15:11:13 +02:00
ledenlijst.value = []
2022-09-18 20:07:37 +02:00
for (let i in result) {
let groups = []
2023-01-07 20:09:18 +01:00
const correctGroups = result[i].Verenigingssporten.replace(/,/g, " -")
console.log(correctGroups)
groups = correctGroups.split(' - ')
if (groups[2] === 'Week') groups[2] = 'Vrijdag'
groups = groups.filter((item) => item !== "Groep")
2022-12-29 12:54:58 +01:00
ledenlijst.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'] })
2022-09-18 20:07:37 +02:00
}
2022-09-19 15:11:13 +02:00
uploadLedenlijst()
}
const uploadLedenlijst = async () => {
try {
const batch = writeBatch(db)
for (let i = 0; i < ledenlijst.value.length; i++) {
const docRef = doc(db, "ledenlijst", ledenlijst.value[i].relatiecode)
batch.set(docRef, ledenlijst.value[i]);
}
await batch.commit();
toast.success('Published ledenlijst')
} catch (e) {
toast.error("Error updating ledenlijst");
console.log(e)
}
2022-12-17 13:01:49 +01:00
updateUsers()
}
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) => {
if (!newRelatiecodes.includes(relatiecode)) { user.relatiecodes.splice(index, 1); console.log('removed item')}
})
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>