2022-10-27 12:59:19 +00:00
|
|
|
<script setup>
|
2023-01-26 13:29:01 +00:00
|
|
|
import axios from 'axios';
|
2022-10-27 12:59:19 +00:00
|
|
|
import { onMounted, onUnmounted, computed, ref, watch } from 'vue';
|
|
|
|
import { useI18n } from 'vue-i18n';
|
|
|
|
import { useQuasar } from 'quasar';
|
|
|
|
import { useState } from 'src/composables/useState';
|
2022-10-31 08:34:01 +00:00
|
|
|
import { useValidator } from 'src/composables/useValidator';
|
2022-11-17 07:04:12 +00:00
|
|
|
import SkeletonForm from 'components/ui/SkeletonForm.vue';
|
2022-10-27 12:59:19 +00:00
|
|
|
|
|
|
|
const quasar = useQuasar();
|
|
|
|
const { t } = useI18n();
|
|
|
|
const state = useState();
|
2022-10-31 08:34:01 +00:00
|
|
|
const { validate } = useValidator();
|
2022-10-27 12:59:19 +00:00
|
|
|
|
|
|
|
const $props = defineProps({
|
|
|
|
url: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
model: {
|
|
|
|
type: String,
|
|
|
|
default: '',
|
|
|
|
},
|
|
|
|
filter: {
|
|
|
|
type: Object,
|
|
|
|
default: null,
|
|
|
|
},
|
2023-07-10 04:04:05 +00:00
|
|
|
urlUpdate: {
|
|
|
|
type: String,
|
|
|
|
default: null,
|
|
|
|
},
|
2022-10-27 12:59:19 +00:00
|
|
|
});
|
|
|
|
|
2022-10-31 08:34:01 +00:00
|
|
|
const emit = defineEmits(['onFetch']);
|
2022-10-27 12:59:19 +00:00
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
save,
|
|
|
|
});
|
|
|
|
|
|
|
|
onMounted(async () => await fetch());
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
state.unset($props.model);
|
|
|
|
});
|
|
|
|
|
|
|
|
const isLoading = ref(false);
|
|
|
|
const hasChanges = ref(false);
|
|
|
|
const formData = computed(() => state.get($props.model));
|
|
|
|
const originalData = ref();
|
2023-01-26 13:29:01 +00:00
|
|
|
const formUrl = computed(() => $props.url);
|
2022-10-27 12:59:19 +00:00
|
|
|
|
|
|
|
async function fetch() {
|
|
|
|
const { data } = await axios.get($props.url, {
|
|
|
|
params: { filter: $props.filter },
|
|
|
|
});
|
|
|
|
|
|
|
|
state.set($props.model, data);
|
|
|
|
originalData.value = Object.assign({}, data);
|
|
|
|
|
|
|
|
watch(formData.value, () => (hasChanges.value = true));
|
|
|
|
|
2022-10-31 08:34:01 +00:00
|
|
|
emit('onFetch', state.get($props.model));
|
2022-10-27 12:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function save() {
|
|
|
|
if (!hasChanges.value) {
|
|
|
|
return quasar.notify({
|
|
|
|
type: 'negative',
|
|
|
|
message: t('globals.noChanges'),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
isLoading.value = true;
|
2023-07-10 04:04:05 +00:00
|
|
|
await axios.patch($props.urlUpdate || $props.url, formData.value);
|
2022-10-31 08:34:01 +00:00
|
|
|
|
|
|
|
originalData.value = formData.value;
|
|
|
|
hasChanges.value = false;
|
2022-10-27 12:59:19 +00:00
|
|
|
isLoading.value = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
state.set($props.model, originalData.value);
|
|
|
|
hasChanges.value = false;
|
|
|
|
}
|
2023-06-01 07:09:54 +00:00
|
|
|
// eslint-disable-next-line vue/no-dupe-keys
|
2022-10-31 08:34:01 +00:00
|
|
|
function filter(value, update, filterOptions) {
|
|
|
|
update(
|
|
|
|
() => {
|
|
|
|
const { options, filterFn } = filterOptions;
|
|
|
|
|
|
|
|
options.value = filterFn(options, value);
|
|
|
|
},
|
|
|
|
(ref) => {
|
|
|
|
ref.setOptionIndex(-1);
|
|
|
|
ref.moveOptionSelection(1, true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2023-01-26 13:29:01 +00:00
|
|
|
|
|
|
|
watch(formUrl, async () => {
|
|
|
|
originalData.value = null;
|
|
|
|
reset();
|
|
|
|
fetch();
|
|
|
|
});
|
2022-10-27 12:59:19 +00:00
|
|
|
</script>
|
|
|
|
<template>
|
2023-04-11 11:31:03 +00:00
|
|
|
<QBanner v-if="hasChanges" class="text-white bg-warning">
|
|
|
|
<QIcon name="warning" size="md" class="q-mr-md" />
|
2022-10-31 08:34:01 +00:00
|
|
|
<span>{{ t('globals.changesToSave') }}</span>
|
2023-04-11 11:31:03 +00:00
|
|
|
</QBanner>
|
|
|
|
<QForm v-if="formData" @submit="save" @reset="reset" class="q-pa-md">
|
2022-10-31 08:34:01 +00:00
|
|
|
<slot name="form" :data="formData" :validate="validate" :filter="filter"></slot>
|
|
|
|
<div class="q-mt-lg">
|
|
|
|
<slot name="actions">
|
2023-04-11 11:31:03 +00:00
|
|
|
<QBtn :label="t('globals.save')" type="submit" color="primary" />
|
|
|
|
<QBtn
|
2022-10-31 08:34:01 +00:00
|
|
|
:label="t('globals.reset')"
|
|
|
|
type="reset"
|
|
|
|
class="q-ml-sm"
|
|
|
|
color="primary"
|
|
|
|
flat
|
|
|
|
:disable="!hasChanges"
|
|
|
|
/>
|
|
|
|
</slot>
|
|
|
|
</div>
|
2023-04-11 11:31:03 +00:00
|
|
|
</QForm>
|
|
|
|
<SkeletonForm v-if="!formData" />
|
|
|
|
<QInnerLoading
|
|
|
|
:showing="isLoading"
|
|
|
|
:label="t('globals.pleaseWait')"
|
|
|
|
color="primary"
|
|
|
|
/>
|
2022-10-27 12:59:19 +00:00
|
|
|
</template>
|