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

109 lines
3.4 KiB
Vue
Raw Normal View History

2022-09-16 21:11:44 +02:00
<template>
2022-09-18 10:47:43 +02:00
<div class="flex flex-col gap-5 mx-auto p-2 w-full max-w-sm">
<div>
<form @submit.prevent="submitLedenlijst" class="flex flex-col">
<span class="">Last updated: <b>37 sep</b></span>
<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="enabled:btn disabled:btn-disabled mx-auto mt-2">Publish Ledenlijst</button>
</form>
</div>
2022-09-16 21:11:44 +02:00
</div>
</template>
<script setup>
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-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-18 20:07:37 +02:00
const ledenlijst = ref([])
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) => {
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)
}
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')
for (let i in result) {
let groups = []
if (Array.isArray(result[i].Verenigingssporten)) {
for (let j = 0; j < result[i].Verenigingssporten.length; j++ ) {
let tmp = result[i].Verenigingssporten[j].split(' - ')
tmp.shift()
if (tmp[1] === 'Week') tmp[1] = 'Vrijdag'
groups.push(...tmp)
}
} else {
groups = result[i].Verenigingssporten.split(' - ')
groups.shift()
if (groups[1] === 'Week') groups[1] = 'Vrijdag'
}
ledenlijst.value.push({ relatiecode: result[i].Relatiecode, fullName: result[i]['Volledige naam (1)'].join(' '), email: [result[i]['E-mail'], result[i]['2e E-mail']], groups: groups, diploma: result[i]['Diploma dropdown 1'] })
}
console.log(ledenlijst.value)
2022-09-18 10:47:43 +02:00
}
2022-09-16 21:11:44 +02:00
</script>