Skip to content
Snippets Groups Projects
Commit 649e1b5b authored by Nick Anton Christoph Kleine-Tebbe's avatar Nick Anton Christoph Kleine-Tebbe
Browse files

Fixed issue with deleting Kolloqs

parent 94bd3406
No related branches found
No related tags found
No related merge requests found
...@@ -6,9 +6,15 @@ var fs = require("fs") ...@@ -6,9 +6,15 @@ var fs = require("fs")
const kolloquiumDirectory = 'Kolloquiums' const kolloquiumDirectory = 'Kolloquiums'
function getDirectories(path) { function getDirectories(path) {
// check if directory exists
if (fs.existsSync(path)) {
return fs.readdirSync(path).filter(function (file) { return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory(); return fs.statSync(path+'/'+file).isDirectory();
}); });
} else {
console.log('Directory not found.');
return []
}
} }
function isEmpty(checkString) { function isEmpty(checkString) {
...@@ -54,6 +60,7 @@ fs.mkdir(kolloquiumDirectory,function(err) { ...@@ -54,6 +60,7 @@ fs.mkdir(kolloquiumDirectory,function(err) {
router.use('/getKolloquiums', (req, res) => { router.use('/getKolloquiums', (req, res) => {
console.log('/getKolloquiums') console.log('/getKolloquiums')
var directories = getDirectories(kolloquiumDirectory) var directories = getDirectories(kolloquiumDirectory)
console.log('Kolloquiums: ' + directories)
return res.json({ return res.json({
kolloquiums: directories kolloquiums: directories
}); });
...@@ -96,7 +103,18 @@ router.use('/deleteKolloquium', (req, res) => { ...@@ -96,7 +103,18 @@ router.use('/deleteKolloquium', (req, res) => {
}) })
} }
fs.rmdir(kolloquiumDirectory + '/' + safeTitle, function(err) { let dirname = kolloquiumDirectory + '/' + safeTitle
fs.readdir(dirname, function(err, files) {
if (err) {
console.error(err);
return res.json({
success: false,
message: err
})
} else {
if (!files.length) {
// directory appears to be empty
fs.rmdir(dirname, function(err) {
if (err) { if (err) {
console.error(err); console.error(err);
return res.json({ return res.json({
...@@ -104,11 +122,21 @@ router.use('/deleteKolloquium', (req, res) => { ...@@ -104,11 +122,21 @@ router.use('/deleteKolloquium', (req, res) => {
message: err message: err
}) })
} }
});
return res.json({ return res.json({
success: true, success: true,
message: 'removed Kolloquium ' + safeTitle message: 'removed Kolloquium ' + safeTitle
}) })
});
} else {
// TODO: ask for confirmation to delete anyway
console.log('Directory is not empty. TODO: Ask for confirmation to delete anyway')
return res.json({
success: false,
message: 'Kolloquium ' + safeTitle + ' was not empty'
})
}
}
});
}) })
// Create Kolloquium // Create Kolloquium
......
...@@ -3,10 +3,19 @@ ...@@ -3,10 +3,19 @@
:selected="selected" :selected="selected"
> >
<div class="flex flex-row w-full"> <div class="flex flex-row w-full">
<div v-if="!inEdit" class="w-full select-none text-left mr-1"> <div
v-if="!inEdit"
class="w-full select-none text-left mr-1"
@click="selectMe()"
>
{{ title }} {{ title }}
</div> </div>
<input v-if="inEdit" class="w-full rounded border p-1" v-model="title" placeholder="Neues Kolloquium..." /> <input
v-if="inEdit"
class="w-full rounded border p-1"
v-model="title"
placeholder="Neues Kolloquium..."
/>
<client-only> <client-only>
<button <button
v-if="inEdit" v-if="inEdit"
...@@ -65,6 +74,9 @@ export default { ...@@ -65,6 +74,9 @@ export default {
}, },
deleteMe() { deleteMe() {
this.$emit("deleteKolloquium") this.$emit("deleteKolloquium")
},
selectMe() {
this.$emit("selectKolloquium")
} }
} }
} }
......
...@@ -16,12 +16,12 @@ ...@@ -16,12 +16,12 @@
<KolloquiumItem <KolloquiumItem
v-for="kolloquium in kolloquiums" v-for="kolloquium in kolloquiums"
:key="kolloquium.title" :key="kolloquium.title"
@click.native="selectKolloquium(kolloquium)"
:selected="selectedKolloquium===kolloquium.title" :selected="selectedKolloquium===kolloquium.title"
:title="kolloquium.title" :title="kolloquium.title"
@update:title="kolloquium.title=$event"
:inEdit="kolloquium.inEdit" :inEdit="kolloquium.inEdit"
@update:title="kolloquium.title=$event"
@update:inEdit="toggleEdit(kolloquium, $event)" @update:inEdit="toggleEdit(kolloquium, $event)"
@selectKolloquium="selectKolloquium(kolloquium.title)"
@deleteKolloquium="deleteKolloquium(kolloquium.title)" @deleteKolloquium="deleteKolloquium(kolloquium.title)"
/> />
<ListItem <ListItem
...@@ -37,14 +37,20 @@ ...@@ -37,14 +37,20 @@
</div> </div>
</template> </template>
<template slot="content"> <template slot="content">
<div v-if="selectedKolloquium">
<p class="text-xl"><span class="font-semibold">Titel:</span> {{ selectedKolloquium }}</p> <p class="text-xl"><span class="font-semibold">Titel:</span> {{ selectedKolloquium }}</p>
<p class="font-semibold mt-4 mb-1">Abgaben:</p> <p class="font-semibold mt-4 mb-1">Abgaben:</p>
<div v-if="abgaben.length > 0">
<AbgabeItem <AbgabeItem
v-for="abgabe in abgaben" v-for="abgabe in abgaben"
:key="abgabe" :key="abgabe"
@click.native="selectAbgabe(abgabe)" @click.native="selectAbgabe(abgabe)"
:title="abgabe" :title="abgabe"
/> />
</div>
<div v-else>
Keine Abgaben
</div>
<div class="flex flex-row justify-between"> <div class="flex flex-row justify-between">
<button class="border rounded mt-4 p-2 font-semibold text-white bg-green-500 hover:bg-green-600 focus:bg-green-700"> <button class="border rounded mt-4 p-2 font-semibold text-white bg-green-500 hover:bg-green-600 focus:bg-green-700">
Aktivieren Aktivieren
...@@ -55,6 +61,10 @@ ...@@ -55,6 +61,10 @@
</button> </button>
</n-link> </n-link>
</div> </div>
</div>
<div v-else class="font-semibold">
Kein Kolloquium ausgewählt
</div>
</template> </template>
</box> </box>
</div> </div>
...@@ -80,11 +90,11 @@ export default { ...@@ -80,11 +90,11 @@ export default {
} }
}, },
methods: { methods: {
async selectKolloquium(kolloquium) { async selectKolloquium(kolloquiumTitle) {
this.selectedKolloquium = kolloquium.title this.selectedKolloquium = kolloquiumTitle
this.selectedAbgabe = '' this.selectedAbgabe = ''
this.abgaben = [] this.abgaben = []
const data = await this.$axios.$post('/api/getAbgaben/', { kolloquium: kolloquium.title }) const data = await this.$axios.$post('/api/getAbgaben/', { kolloquium: kolloquiumTitle })
this.abgaben = data.abgaben this.abgaben = data.abgaben
}, },
selectAbgabe(abgabe) { selectAbgabe(abgabe) {
...@@ -98,7 +108,7 @@ export default { ...@@ -98,7 +108,7 @@ export default {
this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title.length > 0); this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title.length > 0);
this.kolloquiums = [...this.kolloquiums, {title: '', inEdit: true, isNew: true}]; this.kolloquiums = [...this.kolloquiums, {title: '', inEdit: true, isNew: true}];
}, },
toggleEdit(kolloquium, {inEdit, title}) { async toggleEdit(kolloquium, {inEdit, title}) {
let createNew = kolloquium.inEdit && kolloquium.isNew; let createNew = kolloquium.inEdit && kolloquium.isNew;
let changeName = kolloquium.inEdit && !kolloquium.isNew; let changeName = kolloquium.inEdit && !kolloquium.isNew;
...@@ -115,34 +125,55 @@ export default { ...@@ -115,34 +125,55 @@ export default {
return return
} }
this.$axios.post('api/createKolloquium', { title: title }) this.$axios.post('api/createKolloquium', { title: title })
this.refreshKolloquiumList()
} }
else if (changeName) { else if (changeName) {
// TODO: Request confirmation before changing name so the link does not break
// TODO: Or make sure the link stays the same if the folder is renamed?
if (!title || title.trim().length == 0) { if (!title || title.trim().length == 0) {
return return
} }
kolloquium.title = title
this.$axios.post('api/renameKolloquium', { oldTitle: kolloquium.title, newTitle: title}) this.$axios.post('api/renameKolloquium', { oldTitle: kolloquium.title, newTitle: title})
this.refreshKolloquiumList()
} }
}, },
deleteKolloquium(kolloquiumToDelete) { async deleteKolloquium(kolloquiumTitle) {
this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title != kolloquiumToDelete); this.selectedKolloquium = ''
this.selectedKolloquium = "" if(kolloquiumTitle != ''){
if(kolloquiumToDelete != ''){ this.$axios.post('api/deleteKolloquium', { title: kolloquiumTitle })
this.$axios.post('api/deleteKolloquium', { title: kolloquiumToDelete })
} }
this.refreshKolloquiumList()
}, },
async refreshKolloquiumList() {
this.abgaben = []
this.kolloquiums = []
this.selectedKolloquium = ''
this.selectedAbgabe = ''
const dataKolloquiums = await this.$axios.$get('/api/getKolloquiums/')
dataKolloquiums.kolloquiums.forEach(title => {
this.kolloquiums.push({
title: title,
inEdit: false
})
})
}
}, },
async asyncData ({ $axios }) { async asyncData ({ $axios }) {
const dataKolloquiums = await $axios.$get('/api/getKolloquiums/')
let kolloquiumList = [] let kolloquiumList = []
let abgaben = []
const dataKolloquiums = await $axios.$get('/api/getKolloquiums/')
dataKolloquiums.kolloquiums.forEach(title => { dataKolloquiums.kolloquiums.forEach(title => {
kolloquiumList.push({ kolloquiumList.push({
title: title, title: title,
inEdit: false inEdit: false
}) })
}) })
if (kolloquiumList.length !== 0){
const dataAbgaben = await $axios.$post('/api/getAbgaben/', { kolloquium: kolloquiumList[0].title }) const dataAbgaben = await $axios.$post('/api/getAbgaben/', { kolloquium: kolloquiumList[0].title })
return { kolloquiums: kolloquiumList, abgaben: dataAbgaben.abgaben, selectedKolloquium: kolloquiumList[0].title } abgaben = dataAbgaben.abgaben
}
return { kolloquiums: kolloquiumList, abgaben: abgaben }
}, },
} }
</script> </script>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment