fix reset, animated fade out on create contribution

This commit is contained in:
einhornimmond 2025-05-13 20:34:23 +02:00
parent 7c991efbb8
commit 9d5eb5f239
4 changed files with 49 additions and 24 deletions

View File

@ -41,7 +41,12 @@
/> />
</div> </div>
</BContainer> </BContainer>
<div v-if="contribution.contributionStatus === 'PENDING' || contribution.contributionStatus === 'IN_PROGRESS'"> <div
v-if="
contribution.contributionStatus === 'PENDING' ||
contribution.contributionStatus === 'IN_PROGRESS'
"
>
<contribution-messages-formular <contribution-messages-formular
:contribution-id="contribution.id" :contribution-id="contribution.id"
:contribution-memo="contribution.memo" :contribution-memo="contribution.memo"

View File

@ -1,12 +1,15 @@
<template> <template>
<contribution-form <transition name="fade-out" @after-leave="resetForm">
v-if="maxForMonths" <div v-if="showForm">
:model-value="form" <contribution-form
:max-gdd-last-month="parseFloat(maxForMonths.openCreations[1].amount)" v-if="maxForMonths"
:max-gdd-this-month="parseFloat(maxForMonths.openCreations[2].amount)" :model-value="form"
@upsert-contribution="handleCreateContribution" :max-gdd-last-month="parseFloat(maxForMonths.openCreations[1].amount)"
@reset-form="resetForm" :max-gdd-this-month="parseFloat(maxForMonths.openCreations[2].amount)"
/> @upsert-contribution="handleCreateContribution"
/>
</div>
</transition>
</template> </template>
<script setup> <script setup>
@ -29,6 +32,7 @@ const { result: maxForMonths, refetch } = useQuery(
const { mutate: createContributionMutation } = useMutation(createContribution) const { mutate: createContributionMutation } = useMutation(createContribution)
const form = ref(emptyForm()) const form = ref(emptyForm())
const showForm = ref(true)
function emptyForm() { function emptyForm() {
return { return {
@ -43,14 +47,25 @@ async function handleCreateContribution(contribution) {
try { try {
await createContributionMutation({ ...contribution }) await createContributionMutation({ ...contribution })
toastSuccess(t('contribution.submitted')) toastSuccess(t('contribution.submitted'))
resetForm() showForm.value = false
refetch()
} catch (err) { } catch (err) {
toastError(err.message) toastError(err.message)
} }
} }
function resetForm() { function resetForm() {
form.value = emptyForm() refetch()
showForm.value = true
} }
</script> </script>
<style scoped>
.fade-out-enter-active,
.fade-out-leave-active {
transition: opacity 0.5s ease;
}
.fade-out-enter-from,
.fade-out-leave-to {
opacity: 0;
}
</style>

View File

@ -4,7 +4,6 @@
:max-gdd-last-month="maxForMonths[0]" :max-gdd-last-month="maxForMonths[0]"
:max-gdd-this-month="maxForMonths[1]" :max-gdd-this-month="maxForMonths[1]"
@upsert-contribution="handleUpdateContribution" @upsert-contribution="handleUpdateContribution"
@reset-form="emit('reset-form')"
/> />
</template> </template>
@ -19,7 +18,7 @@ import { useI18n } from 'vue-i18n'
const { toastError, toastSuccess } = useAppToast() const { toastError, toastSuccess } = useAppToast()
const { t } = useI18n() const { t } = useI18n()
const emit = defineEmits(['contribution-updated', 'reset-form']) const emit = defineEmits(['contribution-updated'])
const props = defineProps({ const props = defineProps({
modelValue: { type: Object, required: true }, modelValue: { type: Object, required: true },

View File

@ -63,7 +63,7 @@
type="reset" type="reset"
variant="secondary" variant="secondary"
data-test="button-cancel" data-test="button-cancel"
@click="emit('reset-form')" @click="resetForm"
> >
{{ $t('form.cancel') }} {{ $t('form.cancel') }}
</BButton> </BButton>
@ -84,7 +84,6 @@
</BForm> </BForm>
</div> </div>
</template> </template>
<script setup> <script setup>
import { reactive, computed, watch, ref, onMounted, onUnmounted, toRaw } from 'vue' import { reactive, computed, watch, ref, onMounted, onUnmounted, toRaw } from 'vue'
import { useI18n } from 'vue-i18n' import { useI18n } from 'vue-i18n'
@ -96,13 +95,6 @@ import { GDD_PER_HOUR } from '../../constants'
const amountToHours = (amount) => parseFloat(amount / GDD_PER_HOUR).toFixed(2) const amountToHours = (amount) => parseFloat(amount / GDD_PER_HOUR).toFixed(2)
const hoursToAmount = (hours) => parseFloat(hours * GDD_PER_HOUR).toFixed(2) const hoursToAmount = (hours) => parseFloat(hours * GDD_PER_HOUR).toFixed(2)
const entityDataToForm = (entityData) => ({
...entityData,
hours: entityData.hours !== undefined ? entityData.hours : amountToHours(entityData.amount),
contributionDate: entityData.contributionDate
? new Date(entityData.contributionDate).toISOString().slice(0, 10)
: undefined,
})
const props = defineProps({ const props = defineProps({
modelValue: { type: Object, required: true }, modelValue: { type: Object, required: true },
@ -114,7 +106,18 @@ const emit = defineEmits(['upsert-contribution', 'update:modelValue', 'reset-for
const { t } = useI18n() const { t } = useI18n()
const form = reactive(entityDataToForm(props.modelValue)) const entityDataToForm = computed(() => ({
...props.modelValue,
hours:
props.modelValue.hours !== undefined
? props.modelValue.hours
: amountToHours(props.modelValue.amount),
contributionDate: props.modelValue.contributionDate
? new Date(props.modelValue.contributionDate).toISOString().slice(0, 10)
: undefined,
}))
const form = reactive({ ...entityDataToForm.value })
const now = ref(new Date()) // checked every minute, updated if day, month or year changed const now = ref(new Date()) // checked every minute, updated if day, month or year changed
@ -209,6 +212,9 @@ const updateField = (newValue, name) => {
function submit() { function submit() {
emit('upsert-contribution', toRaw(form)) emit('upsert-contribution', toRaw(form))
} }
function resetForm() {
Object.assign(form, entityDataToForm.value)
}
</script> </script>
<style> <style>
.form-style { .form-style {