refactor: update QueryHeader component to new requests

This commit is contained in:
axolotle 2024-08-05 17:05:33 +02:00
parent ab2c8a9691
commit e553925e9a

View file

@ -1,108 +1,88 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed } from 'vue' import { computed, toRefs } from 'vue'
import { useStore } from 'vuex'
import type { Obj } from '@/types/commons' import type { APIRequest } from '@/composables/useRequests'
import { STATUS_VARIANT } from '@/composables/useRequests'
const props = withDefaults( const props = defineProps<{
defineProps<{ request: APIRequest
request: Obj type: 'overlay' | 'history'
statusSize?: string }>()
showTime?: boolean
showError?: boolean const emit = defineEmits<{ showError: [id: string] }>()
}>(),
{ const statusVariant = computed(() => STATUS_VARIANT[props.request.status])
statusSize: '', const { errors, warnings } = toRefs(
showTime: false, props.request.action || { errors: 0, warnings: 0 },
showError: false,
},
) )
const hour = computed(() => {
const store = useStore() return new Date(props.request.date).toLocaleTimeString()
const color = computed(() => {
const statuses = {
pending: 'primary',
success: 'success',
warning: 'warning',
error: 'danger',
}
return statuses[props.request.status]
}) })
const errorsCount = computed(() => {
return props.request.messages.filter(({ type }) => type === 'danger').length
})
const warningsCount = computed(() => {
return props.request.messages.filter(({ type }) => type === 'warning').length
})
function reviewError() {
store.dispatch('REVIEW_ERROR', props.request)
}
function hour(date: Date) {
return new Date(date).toLocaleTimeString()
}
</script> </script>
<template> <template>
<div class="query-header w-100"> <div class="query-header d-flex align-items-center w-100">
<!-- STATUS -->
<span <span
class="status" class="status"
:class="['bg-' + color, statusSize]" :class="[`bg-${statusVariant}`, type]"
:aria-label="$t('api.query_status.' + request.status)" :aria-label="$t(`api.query_status.${request.status}`)"
/> />
<!-- REQUEST DESCRIPTION --> <!-- tabindex 0 on title for focus-trap when no tabable elements -->
<strong class="request-desc"> <strong :tabindex="type === 'overlay' ? 0 : undefined">
{{ request.humanRoute }} {{ request.humanRoute }}
</strong> </strong>
<div v-if="request.errors || request.warnings"> <div v-if="errors || warnings">
<!-- WEBSOCKET ERRORS COUNT --> <span v-if="errors" class="ms-2">
<span class="count" v-if="request.errors"> {{ errors }}<YIcon iname="bug" class="text-danger ms-1" />
{{ request.errors }}<YIcon iname="bug" class="text-danger ms-1" />
</span> </span>
<!-- WEBSOCKET WARNINGS COUNT --> <span v-if="warnings" class="ms-2">
<span class="count" v-if="request.warnings"> {{ warnings }}<YIcon iname="warning" class="text-warning ms-1" />
{{ request.warnings
}}<YIcon iname="warning" class="text-warning ms-1" />
</span> </span>
</div> </div>
<!-- VIEW ERROR BUTTON --> <template v-if="type === 'history'">
<BButton <BButton
v-if="showError && request.error" v-if="request.err"
size="sm" size="sm"
pill pill
class="error-btn ms-auto py-0" class="error-btn ms-auto py-0"
variant="danger" variant="danger"
@click="reviewError" @click.stop="emit('showError', request.id)"
> >
<small v-t="'api_error.view_error'" /> <small v-t="'api_error.view_error'" />
</BButton> </BButton>
<!-- TIME DISPLAY --> <time :datetime="hour" :class="request.err ? 'ms-2' : 'ms-auto'">
<time {{ hour }}
v-if="showTime" </time>
:datetime="hour(request.date)" </template>
:class="request.error ? 'ms-2' : 'ms-auto'"
>
{{ hour(request.date) }}
</time>
</div> </div>
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
div { .query-header {
display: flex;
align-items: center;
font-size: $font-size-sm; font-size: $font-size-sm;
} }
.status {
display: inline-block;
border-radius: 50%;
&.history {
width: 0.75rem;
height: 0.75rem;
margin-right: 0.5rem;
}
&.overlay {
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
}
}
.error-btn { .error-btn {
height: 1.25rem; height: 1.25rem;
display: flex; display: flex;
@ -111,35 +91,8 @@ div {
min-width: 70px; min-width: 70px;
} }
.status {
display: inline-block;
border-radius: 50%;
width: 0.75rem;
min-width: 0.75rem;
height: 0.75rem;
margin-right: 0.25rem;
&.lg {
width: 1rem;
height: 1rem;
margin-right: 0.5rem;
}
}
time { time {
min-width: 3.5rem; min-width: 3rem;
text-align: right; text-align: right;
} }
.count {
display: flex;
align-items: center;
margin-left: 0.5rem;
}
@include media-breakpoint-down(sm) {
.xs-hide .request-desc {
display: none;
}
}
</style> </style>