diff --git a/api/routes/kolloquiums.js b/api/routes/kolloquiums.js index 651986f29cb59c47694f81da14d706e0d39da4a2..91d05dc898627479bb96bdd435fae395cd6a520d 100644 --- a/api/routes/kolloquiums.js +++ b/api/routes/kolloquiums.js @@ -6,9 +6,15 @@ var fs = require("fs") const kolloquiumDirectory = 'Kolloquiums' function getDirectories(path) { - return fs.readdirSync(path).filter(function (file) { - return fs.statSync(path+'/'+file).isDirectory(); - }); + // check if directory exists + if (fs.existsSync(path)) { + return fs.readdirSync(path).filter(function (file) { + return fs.statSync(path+'/'+file).isDirectory(); + }); + } else { + console.log('Directory not found.'); + return [] + } } function isEmpty(checkString) { @@ -54,6 +60,7 @@ fs.mkdir(kolloquiumDirectory,function(err) { router.use('/getKolloquiums', (req, res) => { console.log('/getKolloquiums') var directories = getDirectories(kolloquiumDirectory) + console.log('Kolloquiums: ' + directories) return res.json({ kolloquiums: directories }); @@ -96,19 +103,40 @@ 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 - }) + 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) { + console.error(err); + return res.json({ + success: false, + message: err + }) + } + return res.json({ + success: true, + 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' + }) + } } - }); - return res.json({ - success: true, - message: 'removed Kolloquium ' + safeTitle - }) + }); }) // Create Kolloquium diff --git a/components/KolloquiumItem.vue b/components/KolloquiumItem.vue index b3fe5b1cfbb5ab0fab32b082bbe53d6237f18df2..aa14704492fd07ace19fc0630ab5b3547b5d27af 100644 --- a/components/KolloquiumItem.vue +++ b/components/KolloquiumItem.vue @@ -3,10 +3,19 @@ :selected="selected" > <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 }} </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> <button v-if="inEdit" @@ -65,6 +74,9 @@ export default { }, deleteMe() { this.$emit("deleteKolloquium") + }, + selectMe() { + this.$emit("selectKolloquium") } } } diff --git a/pages/index.vue b/pages/index.vue index 4087f5ce625f0e17eaaecddafd2a4f8ae76bb2c2..083b897ccc6e2b766ab0f156a3e1be379b19cafb 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -16,12 +16,12 @@ <KolloquiumItem v-for="kolloquium in kolloquiums" :key="kolloquium.title" - @click.native="selectKolloquium(kolloquium)" :selected="selectedKolloquium===kolloquium.title" :title="kolloquium.title" - @update:title="kolloquium.title=$event" :inEdit="kolloquium.inEdit" + @update:title="kolloquium.title=$event" @update:inEdit="toggleEdit(kolloquium, $event)" + @selectKolloquium="selectKolloquium(kolloquium.title)" @deleteKolloquium="deleteKolloquium(kolloquium.title)" /> <ListItem @@ -37,23 +37,33 @@ </div> </template> <template slot="content"> - <p class="text-xl"><span class="font-semibold">Titel:</span> {{ selectedKolloquium }}</p> - <p class="font-semibold mt-4 mb-1">Abgaben:</p> - <AbgabeItem - v-for="abgabe in abgaben" - :key="abgabe" - @click.native="selectAbgabe(abgabe)" - :title="abgabe" - /> - <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"> - Aktivieren - </button> - <n-link :to="'/abgabe/' + selectedKolloquium"> - <button class="border rounded mt-4 p-2 font-semibold text-white bg-blue-500 hover:bg-blue-600 focus:bg-blue-700"> - Link Teilen + <div v-if="selectedKolloquium"> + <p class="text-xl"><span class="font-semibold">Titel:</span> {{ selectedKolloquium }}</p> + <p class="font-semibold mt-4 mb-1">Abgaben:</p> + <div v-if="abgaben.length > 0"> + <AbgabeItem + v-for="abgabe in abgaben" + :key="abgabe" + @click.native="selectAbgabe(abgabe)" + :title="abgabe" + /> + </div> + <div v-else> + Keine Abgaben + </div> + <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"> + Aktivieren </button> - </n-link> + <n-link :to="'/abgabe/' + selectedKolloquium"> + <button class="border rounded mt-4 p-2 font-semibold text-white bg-blue-500 hover:bg-blue-600 focus:bg-blue-700"> + Link Teilen + </button> + </n-link> + </div> + </div> + <div v-else class="font-semibold"> + Kein Kolloquium ausgewählt </div> </template> </box> @@ -80,11 +90,11 @@ export default { } }, methods: { - async selectKolloquium(kolloquium) { - this.selectedKolloquium = kolloquium.title + async selectKolloquium(kolloquiumTitle) { + this.selectedKolloquium = kolloquiumTitle this.selectedAbgabe = '' 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 }, selectAbgabe(abgabe) { @@ -98,7 +108,7 @@ export default { this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title.length > 0); 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 changeName = kolloquium.inEdit && !kolloquium.isNew; @@ -115,34 +125,55 @@ export default { return } this.$axios.post('api/createKolloquium', { title: title }) + this.refreshKolloquiumList() } 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) { return } - kolloquium.title = title this.$axios.post('api/renameKolloquium', { oldTitle: kolloquium.title, newTitle: title}) + this.refreshKolloquiumList() } }, - deleteKolloquium(kolloquiumToDelete) { - this.kolloquiums = this.kolloquiums.filter(kolloquium => kolloquium.title != kolloquiumToDelete); - this.selectedKolloquium = "" - if(kolloquiumToDelete != ''){ - this.$axios.post('api/deleteKolloquium', { title: kolloquiumToDelete }) + async deleteKolloquium(kolloquiumTitle) { + this.selectedKolloquium = '' + if(kolloquiumTitle != ''){ + this.$axios.post('api/deleteKolloquium', { title: kolloquiumTitle }) } + 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 }) { - const dataKolloquiums = await $axios.$get('/api/getKolloquiums/') let kolloquiumList = [] + let abgaben = [] + const dataKolloquiums = await $axios.$get('/api/getKolloquiums/') dataKolloquiums.kolloquiums.forEach(title => { kolloquiumList.push({ title: title, inEdit: false }) }) - const dataAbgaben = await $axios.$post('/api/getAbgaben/', { kolloquium: kolloquiumList[0].title }) - return { kolloquiums: kolloquiumList, abgaben: dataAbgaben.abgaben, selectedKolloquium: kolloquiumList[0].title } + if (kolloquiumList.length !== 0){ + const dataAbgaben = await $axios.$post('/api/getAbgaben/', { kolloquium: kolloquiumList[0].title }) + abgaben = dataAbgaben.abgaben + } + return { kolloquiums: kolloquiumList, abgaben: abgaben } }, } </script>