Files
jandl-taskmanager-app/app/src/plugins/tm@modal/task/z-task-modal.vue
T
2023-02-16 07:56:31 +01:00

272 lines
8.6 KiB
Vue

<template>
<v-dialog v-model="visible" max-width="750" max-height="90%" height="700px">
<W-loadable :content="task" class="h-100">
<v-card v-if="editMode" height="100%" class="d-flex flex-column scrolling">
<div class="d-flex justify-space-between align-center modal-header">
<h1>{{ task.id ? "Upraviť task" : "Vytvoriť task" }}</h1>
<v-icon @click.stop="closeModal()" icon="mdi-close" size="large"></v-icon>
</div>
<v-card-actions class="d-flex flex-column modal-content">
<v-text-field v-model="task.project_code" label="Kód projektu" :value="task.project_code" @focus="onInputFocus" class="modal-input" variant="solo" disabled></v-text-field>
<Z-autocomplete label="Master task" :options="mastertasks.map((masterTask) => ({ key: masterTask.id, value: masterTask.name }))" :validations="v$.task.master_task.id" :clear-on-submit="false" :value="task?.master_task?.name" :class="{ error: v$.task.master_task.id.$errors.length }" class="modal-input" @option-selected="onMasterTaskSelected" />
<v-select v-model="task.employee_id" @focus="onInputFocus" class="modal-input border-select" label="Priradený" :class="{ error: v$.task.employee_id.$errors.length }" variant="solo" :items="filteredEmployees" item-value="id" item-title="_fullName"></v-select>
<v-text-field v-model="task.duration" @focus="onInputFocus" class="modal-input" variant="solo" type="number" label="Časový odhad" :class="{ error: v$.task.duration.$errors.length }" :hint="v$.task.duration.$errors ? errorUtils.parseErrors(v$.task.duration.$errors) : null" append-inner-icon="mdi-clock-outline"></v-text-field>
<div class="w-100 modal-input" :class="{ error: v$.task.description.$errors.length }">
<QuillEditor ref="description" v-model="task.description" placeholder="Popis" theme="snow" class="rich-text-editor" @input="task.description = $event.srcElement.innerHTML" />
</div>
<Z-files-upload class="mt-15 files" :files="task.files" @files-uploaded="filesUploaded" />
</v-card-actions>
<v-btn class="button primary" @click="save()">{{ task.id ? "UPRAVIŤ" : "VYTVORIŤ" }}</v-btn>
</v-card>
<v-card v-else height="100%" class="d-flex flex-column scrolling">
<div class="d-flex justify-space-between align-center">
<p>{{ task.project_code }}</p>
<v-icon @click.stop="closeModal()" icon="mdi-close" size="large" class="pb-5"></v-icon>
</div>
<div class="d-flex justify-space-between align-center modal-header border-bottom pb-4">
<h1>{{ task.title }}</h1>
<div class="d-flex justify-start">
<img class="icon" :src="require('@/plugins/tm@overview/_img/pencil-icon.svg')" alt="pencil-icon" @click.stop="editMode = !editMode" />
<img class="icon trash" :src="require('@/plugins/tm@overview/_img/trash-icon.svg')" alt="trash-icon" @click.stop="openDeleteTaskModal(task)" />
</div>
</div>
<v-card-actions class="d-flex flex-column modal-content">
<div class="w-100">
<Z-task-readonly-info :task="task" />
<div class="d-flex justify-start align-center mt-8">
<v-btn class="done-btn" @click.stop="task.is_finished = 1; save()">Dokončené</v-btn>
<v-btn class="button secondary" @click="save()">Uložiť zmeny</v-btn>
</div>
</div>
</v-card-actions>
</v-card>
</W-loadable>
</v-dialog>
</template>
<script>
import ZFilesUpload from "@/plugins/tm@_components/z-files-upload.vue"
import useVuelidate from "@vuelidate/core"
import { required } from "@vuelidate/validators"
import errorUtils from "@/plugins/lib@auth/_utils/error.utils"
import { QuillEditor } from "@vueup/vue-quill"
import VuetifyFixFocusMixin from "@/plugins/tm@modal/_mixins/vuetify-fix-focus"
import SelectsMixin from "../master-task/_mixins/selects.mixin"
import * as FileSaver from "file-saver"
import tmUtils from "@/plugins/tm@_utils/tm.utils"
import "@vueup/vue-quill/dist/vue-quill.snow.css"
import ZAutocomplete from "@/plugins/tm@_components/z-autocomplete.vue"
import ZTaskReadonlyInfo from "./_components/z-task-readonly-info.vue"
import { mapGetters } from "vuex"
import moment from "moment"
export default {
emits: ["close", "refresh"],
mixins: [VuetifyFixFocusMixin, SelectsMixin],
components: {
ZTaskReadonlyInfo,
ZFilesUpload,
QuillEditor,
ZAutocomplete
},
data() {
return {
tmUtils,
editMode: false,
visible: false,
task: null,
errorUtils,
v$: useVuelidate(),
preventInputFocus: false,
masterTaskIdSelect: null,
showDatePicker: false,
selectedNewDate: []
}
},
computed: {
...mapGetters('tm', [
'mastertasks',
'enrichedEmployees'
]),
filteredEmployees() {
if (!this.enrichedEmployees || !this.task?.master_task) return []
const selectedEmployees = this.task.master_task.employees?.map((employee) => employee.id) || []
return this.enrichedEmployees.filter((employee) => !selectedEmployees.includes(employee.id))
}
},
validations() {
return {
task: {
description: { required },
master_task: {
id: { required }
},
duration: { required },
employee_id: { required }
}
}
},
mounted() {
this.$events.on("on-task-edited", (task) => {
if (task?.id == this.task?.id)
this.task = this._enrichTask(task)
// TODO: Remove? riesil som neodstranovanie jedineho zanamu v trvani a odpracovanom case
this.$forceUpdate()
})
this.$events.on('on-task-deleted', (task) => {
if (task?.id == this.task?.id) this.closeModal()
})
},
methods: {
async showModal(task, editMode = false, masterTask = {}) {
this.task = null
this.visible = true
this.$nextTick(() => this.v$.$reset())
try {
if (task?.id) {
const subtask = await this.$axios.get_auth_data(`v1/subtasks/${task.id}`)
this.task = this._enrichTask(subtask)
} else {
this.task = { master_task: masterTask, timeblocks: [{start: tmUtils.formatDateTimeForBackend(moment()), end: tmUtils.formatDateTimeForBackend(moment())}] }
this.masterTaskIdSelect = masterTask.id
}
this.editMode = editMode || !this.task.id
} catch (error) {
this.$toast.error(error)
}
},
closeModal() {
this.visible = false
},
async save() {
if (!(await this.v$.$validate()))
return
this.task.description = this.$refs["description"].getHTML()
try {
if (this.task?.id) {
const { subtask } = await this.$axios.put_auth(`v1/subtasks/${this.task.id}`, this.task)
this.$store.commit('tm/update_subtask', subtask)
} else {
const { subtask } = await this.$axios.post_auth(`v1/mastertasks/${this.task.master_task.id}/subtasks`, this.task)
this.task.id = subtask.id
this.$store.commit('tm/update_subtask', subtask)
}
this.$events.emit("on-task-edited", this.task)
this.closeModal()
} catch (error) {
this.$toast.error(error)
}
},
async addNewDate() {
if (this.selectedNewDate) {
this.task.timeblocks.push({
start: this.selectedNewDate[0],
end: this.selectedNewDate[1]
})
this.showDatePicker = false
this.selectedNewDate = []
}
},
async downloadFile(id) {
try {
const file = await this.$axios.get_auth_data(`v1/subtasks/${this.task.id}`, { responseType: "arraybuffer" })
FileSaver.saveAs(file.files[id].path, file.files[id].name)
} catch (error) {
this.$toast.error(error)
}
},
removeDate(index) {
this.task.timeblocks.splice(index, 1)
},
openDeleteTaskModal(task) {
this.$events.emit("show-delete-task-modal", { task })
},
filesUploaded(files) {
this.task.files = files
},
_enrichTask(task) {
task.master_task = this.mastertasks.find((masterTask) => masterTask.id == task.master_task_id)
task.employee = this.enrichedEmployees.find((employee) => employee.id == task.employee_id)
this.masterTaskIdSelect = task.master_task?.id
return task
}
},
watch: {
masterTaskIdSelect(value) {
if (!value || !this.task) return
this.task.master_task = this.mastertasks.find((masterTask) => masterTask.id == value)
this.task.project_code = this.task.master_task.project_code
},
"task.employee_id"(value) {
if (!value) return
this.task.employee = this.enrichedEmployees.find((employee) => employee.id == value)
},
editMode() {
this.$nextTick(() => {
console.log(this.task.description)
this.$refs["description"]?.setHTML(this.task.description || "")
})
}
}
}
</script>
<style lang="sass" scoped>
.scrolling
overflow: hidden !important
.border-bottom
border-bottom: 1px solid rgba(0,0,0,.1)
.done-btn
background: #7f39fb !important
border-radius: 4px !important
color: #fff !important
height: 50px
width: 203px
@media screen and (max-width: 522px)
.files
margin-top: 104px !important
</style>