|
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <p style="margin: 100px 30px 0 30px; max-width: 600px"> |
| 4 | + <h2>Information Editor</h2> |
| 5 | + </p> |
| 6 | + <div id="edit-information-container"> |
| 7 | + <div class="information" v-for="information, j in informationToDisplay" :key="information.id" @input="edited.add(j)"> |
| 8 | + <label><span>Title: </span><input type="text" v-model="information.title" /></label> |
| 9 | + <label> |
| 10 | + <span>Photo: </span> |
| 11 | + <input type="string" v-model="information.photo" disabled /> |
| 12 | + <button v-if="information.photo" @click="information.photo=''" class="upload-button">❌</button> |
| 13 | + <button @click="upload(j)" class="upload-button" title="Upload photo">Upload</button> |
| 14 | + <input type="file" ref="fileUpload" style="display:none" accept="image/jpeg,image/png" /> |
| 15 | + </label> |
| 16 | + <label style="flex-direction: column;"> |
| 17 | + <span>Markdown Description:</span> |
| 18 | + <textarea v-model="information.descriptionMD" /> |
| 19 | + </label> |
| 20 | + <label><span>Link: </span><input type="text" v-model="information.link" /></label> |
| 21 | + <div style="display: flex; justify-content: space-evenly; gap: 10px"> |
| 22 | + <button style="width:100%" @click="update(information, j)"> |
| 23 | + {{(j==0 ? "➕" : "💾") + (edited.has(j) ? "❗" : "")}}{{ confirmation.has(j) ? ' ✅' : "" }} |
| 24 | + </button> |
| 25 | + <button style="width:100%" v-if="j!==0" @click="remove(information, j)"> |
| 26 | + 🗑️ |
| 27 | + </button> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | +</template> |
| 33 | + |
| 34 | +<script setup> |
| 35 | +import { ref, onMounted, onUnmounted, computed, watch } from "vue"; |
| 36 | +import { remult } from "remult"; |
| 37 | +import { Information } from "../../db/entities.js"; |
| 38 | +
|
| 39 | +import { Cropper } from 'vue-advanced-cropper'; |
| 40 | +import 'vue-advanced-cropper/dist/style.css'; |
| 41 | +
|
| 42 | +const informations = ref([]); |
| 43 | +
|
| 44 | +const confirmation = ref(new Set()); |
| 45 | +
|
| 46 | +const edited = ref(new Set()); |
| 47 | +
|
| 48 | +const repo = remult.repo(Information); |
| 49 | +
|
| 50 | +const newInformation = ref(repo.create()); |
| 51 | +
|
| 52 | +const informationToDisplay = computed(() => { |
| 53 | + return [newInformation.value].concat(informations.value); |
| 54 | +}); |
| 55 | +
|
| 56 | +let unsubscribe; |
| 57 | +onMounted(() => { |
| 58 | + unsubscribe = repo.liveQuery({orderBy: {date: "desc"}}) |
| 59 | + .subscribe(info => { |
| 60 | + informations.value = info.items; |
| 61 | + }); |
| 62 | +}); |
| 63 | +
|
| 64 | +onUnmounted(() => { |
| 65 | + if (unsubscribe){ |
| 66 | + unsubscribe(); |
| 67 | + } |
| 68 | +}); |
| 69 | +
|
| 70 | +const update = (information, j) => { |
| 71 | + let update; |
| 72 | + if (j == 0){ |
| 73 | + update = repo.insert(newInformation.value); |
| 74 | + } else { |
| 75 | + update = repo.update(information.id, information); |
| 76 | + } |
| 77 | + update.then(() => { |
| 78 | + confirmation.value.add(j); |
| 79 | + edited.value.delete(j); |
| 80 | + setTimeout(() => confirmation.value.delete(j), 3000); |
| 81 | + if (j == 0){ |
| 82 | + newInformation.value = repo.create(); |
| 83 | + } |
| 84 | + }).catch((e) => { |
| 85 | + alert("Error updating event: " + e); |
| 86 | + alert("e: " + e.message); |
| 87 | + }) |
| 88 | +}; |
| 89 | +
|
| 90 | +const remove = (information, j) => { |
| 91 | + if(confirm("really delete " + information.title + "?")){ |
| 92 | + repo.delete(information); |
| 93 | + } |
| 94 | +} |
| 95 | +
|
| 96 | +const fileUpload = ref(null); |
| 97 | +watch(fileUpload, (inputs) => { |
| 98 | + for (let i=0; i<inputs.length; ++i){ |
| 99 | + inputs[i].onchange = () => { |
| 100 | + const data = new FormData(); |
| 101 | + data.append("photo", inputs[i].files[0]); |
| 102 | + fetch("/information-photo-upload", { method: "POST", body: data }) |
| 103 | + .then(async (res) => { |
| 104 | + const path = await res.text(); |
| 105 | + informationToDisplay.value[i].photo = path; |
| 106 | + }); |
| 107 | + }; |
| 108 | + } |
| 109 | +}, { deep: true }); |
| 110 | +const upload = (i) => { |
| 111 | + if (fileUpload.value[i]){ |
| 112 | + fileUpload.value[i].click(); |
| 113 | + } |
| 114 | +}; |
| 115 | +
|
| 116 | +</script> |
| 117 | + |
| 118 | +<style scoped lang="scss"> |
| 119 | +* { |
| 120 | + color: black; |
| 121 | +} |
| 122 | +#edit-information-container { |
| 123 | + margin: 10px 10px; |
| 124 | + display: flex; |
| 125 | + flex-wrap: wrap; |
| 126 | +} |
| 127 | +.cover-photo-preview { |
| 128 | + width: 50%; |
| 129 | + height: auto; |
| 130 | + padding-bottom: 40%; |
| 131 | + display: flex; |
| 132 | + justify-content: center; |
| 133 | + align-items: center; |
| 134 | + position: relative; |
| 135 | + background-size: contain; |
| 136 | + background-position: center; |
| 137 | + background-repeat: no-repeat; |
| 138 | + button.add-photo { |
| 139 | + position: absolute; |
| 140 | + left: 50%; |
| 141 | + top: 50%; |
| 142 | + transform: translate(-50%, -50%); |
| 143 | + padding: 2px; |
| 144 | + font-size: 1.3rem; |
| 145 | + border-radius: 0; |
| 146 | + width: 30px; |
| 147 | + height: 30px; |
| 148 | + } |
| 149 | + button.remove-photo { |
| 150 | + position: absolute; |
| 151 | + top: 2px; |
| 152 | + right: 2px; |
| 153 | + padding: 2px; |
| 154 | + font-size: 0.9rem; |
| 155 | + border-radius: 0; |
| 156 | + width: 20px; |
| 157 | + height: 20px; |
| 158 | + font-size: small; |
| 159 | + } |
| 160 | +} |
| 161 | +.information { |
| 162 | + display: flex; |
| 163 | + flex-direction: column; |
| 164 | + gap: 10px; |
| 165 | + margin: 20px; |
| 166 | + width: 900px; |
| 167 | +} |
| 168 | +label { |
| 169 | + display: flex; |
| 170 | + width: 100%; |
| 171 | +} |
| 172 | +input { |
| 173 | + padding: 2px; |
| 174 | + width: 100%; |
| 175 | + border-radius: 5px; |
| 176 | +} |
| 177 | +label span { |
| 178 | + flex-shrink: 0; |
| 179 | + margin-right: 15px; |
| 180 | +} |
| 181 | +button, button:hover { |
| 182 | + color: black; |
| 183 | + border: 1px solid darkgray; |
| 184 | + transition: none; |
| 185 | +} |
| 186 | +textarea { |
| 187 | + width: 900px; |
| 188 | + height: 100px; |
| 189 | + overflow: scroll; |
| 190 | + resize: none; |
| 191 | +} |
| 192 | +.cropper { |
| 193 | + width: 600px; |
| 194 | + max-height: 600px; |
| 195 | +} |
| 196 | +</style> |
| 197 | + |
0 commit comments