diff --git a/frontend/components/EntityShowSubPhases.vue b/frontend/components/EntityShowSubPhases.vue index 823e54e..9ef46d6 100644 --- a/frontend/components/EntityShowSubPhases.vue +++ b/frontend/components/EntityShowSubPhases.vue @@ -25,30 +25,42 @@ const emit = defineEmits(["updateNeeded"]); const router = useRouter() const profileStore = useProfileStore() const auth = useAuthStore() +const openPhaseKey = ref(null) + +const isPhaseAvailable = (phase, index, phases) => { + if (phase.label === "Abgeschlossen" || phase.active) { + return true + } + + const activeIndex = phases.findIndex((item) => item.active) + + if (activeIndex > index) { + return true + } + + if (activeIndex === -1) { + return index === 0 + } + + if (index === activeIndex + 1) { + return true + } + + if (index <= activeIndex) { + return true + } + + return phases.slice(activeIndex + 1, index).every((item) => item.optional) +} const renderedPhases = computed(() => { if(props.topLevelType === "projects" && props.item.phases) { return props.item.phases.map((phase,index,array) => { - let isAvailable = false - - if(phase.active) { - isAvailable = true - } else if(index > 0 && array[index-1].active ){ - isAvailable = true - } else if(index > 1 && array[index-1].optional && array[index-2].active){ - isAvailable = true - } else if(array.findIndex(i => i.active) > index) { - isAvailable = true - } else if(phase.label === "Abgeschlossen") { - isAvailable = true - } - - return { ...phase, label: phase.optional ? `${phase.label}(optional)`: phase.label, - disabled: !isAvailable, + disabled: !isPhaseAvailable(phase, index, array), defaultOpen: phase.active ? true : false } }) @@ -57,6 +69,33 @@ const renderedPhases = computed(() => { } }) +watch(renderedPhases, (phases) => { + if (!phases.length) { + openPhaseKey.value = null + return + } + + const activePhase = phases.find((phase) => phase.active) + const currentPhaseStillExists = phases.some((phase) => phase.key === openPhaseKey.value) + + if (activePhase) { + openPhaseKey.value = activePhase.key + return + } + + if (!currentPhaseStillExists) { + openPhaseKey.value = phases[0].key + } +}, { immediate: true }) + +const togglePhasePanel = (phase) => { + if (phase.disabled) { + return + } + + openPhaseKey.value = openPhaseKey.value === phase.key ? null : phase.key +} + const changeActivePhase = async (key) => { console.log(props.item) let item = await useEntities("projects").selectSingle(props.item.id,'*') @@ -92,41 +131,41 @@ const changeActivePhase = async (key) => { - - - - + +