diff --git a/frontend/components/EntityEdit.vue b/frontend/components/EntityEdit.vue index 9976565..c594cfb 100644 --- a/frontend/components/EntityEdit.vue +++ b/frontend/components/EntityEdit.vue @@ -29,7 +29,6 @@ const props = defineProps({ const emit = defineEmits(["returnData"]) - const {type} = props defineShortcuts({ @@ -53,11 +52,10 @@ const route = useRoute() const dataStore = useDataStore() const modal = useModal() - const dataType = dataStore.dataTypes[type] const openTab = ref(0) const item = ref(JSON.parse(props.item)) -console.log(item.value) +// console.log(item.value) const oldItem = ref(null) const generateOldItemData = () => { @@ -66,6 +64,39 @@ const generateOldItemData = () => { generateOldItemData() +// --- ÄNDERUNG START: Computed Property statt Watcher/Function --- +// Dies berechnet den Status automatisch neu, egal woher die Daten kommen (Init oder User-Eingabe) +const saveAllowed = computed(() => { + if (!item.value) return false + + let allowedCount = 0 + // Nur Input-Felder berücksichtigen + const relevantColumns = dataType.templateColumns.filter(i => i.inputType) + + relevantColumns.forEach(datapoint => { + if(datapoint.required) { + if(datapoint.key.includes(".")){ + const [parentKey, childKey] = datapoint.key.split('.') + // Prüfung: Existiert Parent UND ist Child "truthy" (nicht null/undefined/empty) + if(item.value[parentKey] && item.value[parentKey][childKey]) { + allowedCount += 1 + } + } else { + if(item.value[datapoint.key]) { + allowedCount += 1 + } + } + } else { + // Wenn nicht required, zählt es immer als "erlaubt" + allowedCount += 1 + } + }) + + return allowedCount >= relevantColumns.length +}) +// --- ÄNDERUNG ENDE --- + + const setupCreate = () => { dataType.templateColumns.forEach(datapoint => { if(datapoint.key.includes(".")){ @@ -78,10 +109,7 @@ const setupCreate = () => { } else { item.value[datapoint.key] = {} } - - } - }) } setupCreate() @@ -91,49 +119,45 @@ const setupQuery = () => { console.log(props.mode) if(props.mode === "create" && (route.query || props.createQuery)) { - let data = !props.inModal ? route.query : props.createQuery Object.keys(data).forEach(key => { - if(dataType.templateColumns.find(i => i.key === key)) { + if (dataType.templateColumns.find(i => i.key === key)) { if (["customer", "contract", "plant", "contact", "project"].includes(key)) { item.value[key] = Number(data[key]) } else { item.value[key] = data[key] } - } else if(key === "resources") { + } else if (key === "resources") { /*item.value[key] = data[key]*/ JSON.parse(data[key]).forEach(async (i) => { console.log(i) - let type = i.substring(0,1) - let id = i.substring(2,i.length) + let type = i.substring(0, 1) + let id = i.substring(2, i.length) console.log(type) console.log(id) let holder = "" - if(type === "P"){ + if (type === "P") { holder = "profiles" - } else if(type === "F"){ + } else if (type === "F") { holder = "vehicles" id = Number(id) - } else if(type === "I"){ + } else if (type === "I") { holder = "inventoryitems" id = Number(id) - } else if(type === "G"){ + } else if (type === "G") { holder = "inventoryitemgroups" } - if(typeof item.value[holder] === "object") { + if (typeof item.value[holder] === "object") { item.value[holder].push(id) } else { item.value[holder] = [id] } - - }) - - } }) + // calcSaveAllowed() -> Entfernt, da computed automatisch reagiert } } setupQuery() @@ -148,14 +172,14 @@ const loadOptions = async () => { }) for await(const option of optionsToLoad) { - if(option.option === "countrys") { + if (option.option === "countrys") { loadedOptions.value[option.option] = useEntities("countrys").selectSpecial() - } else if(option.option === "units") { + } else if (option.option === "units") { loadedOptions.value[option.option] = useEntities("units").selectSpecial() } else { loadedOptions.value[option.option] = (await useEntities(option.option).select()) - if(dataType.templateColumns.find(x => x.key === option.key).selectDataTypeFilter){ + if (dataType.templateColumns.find(x => x.key === option.key).selectDataTypeFilter) { loadedOptions.value[option.option] = loadedOptions.value[option.option].filter(i => dataType.templateColumns.find(x => x.key === option.key).selectDataTypeFilter(i, item)) } } @@ -165,47 +189,23 @@ const loadOptions = async () => { loadOptions() const contentChanged = (content, datapoint) => { - if(datapoint.key.includes(".")){ - item[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].html = content.html - item[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].text = content.text - item[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].json = content.json + if (datapoint.key.includes(".")) { + item.value[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].html = content.html + item.value[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].text = content.text + item.value[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]].json = content.json } else { - item[datapoint.key].html = content.html - item[datapoint.key].text = content.text - item[datapoint.key].json = content.json + item.value[datapoint.key].html = content.html + item.value[datapoint.key].text = content.text + item.value[datapoint.key].json = content.json } } -const saveAllowed = ref(false) -const calcSaveAllowed = (item) => { - let allowedCount = 0 - - dataType.templateColumns.filter(i => i.inputType).forEach(datapoint => { - if(datapoint.required) { - - if(datapoint.key.includes(".")){ - if(item[datapoint.key.split('.')[0]][datapoint.key.split('.')[1]]) allowedCount += 1 - } else { - if(item[datapoint.key]) allowedCount += 1 - } - } else { - allowedCount += 1 - } - }) - - saveAllowed.value = allowedCount >= dataType.templateColumns.filter(i => i.inputType).length -} -//calcSaveAllowed() - -watch(item.value, async (newItem, oldItem) => { - calcSaveAllowed(newItem) -}) const createItem = async () => { let ret = null - if(props.inModal) { - ret = await useEntities(type).create(item.value, true) + if (props.inModal) { + ret = await useEntities(type).create(item.value, true) } else { ret = await useEntities(type).create(item.value)//dataStore.createNewItem(type,item.value) @@ -218,7 +218,7 @@ const createItem = async () => { const updateItem = async () => { let ret = null - if(props.inModal) { + if (props.inModal) { ret = await useEntities(type).update(item.value.id, item.value, true) emit('returnData', ret) modal.close() @@ -226,11 +226,7 @@ const updateItem = async () => { ret = await useEntities(type).update(item.value.id, item.value) emit('returnData', ret) } - - } - - - { :placeholder="datapoint.inputIsNumberRange ? 'Leer lassen für automatisch generierte Nummer' : ''" > { /> - { icon="i-heroicons-x-mark" /> - - - - - { :placeholder="datapoint.inputIsNumberRange ? 'Leer lassen für automatisch generierte Nummer' : ''" > { /> - { :placeholder="datapoint.inputIsNumberRange ? 'Leer lassen für automatisch generierte Nummer' : ''" > { /> - { { icon="i-heroicons-x-mark" /> - -