Files
homebox/frontend/components/global/LabelMaker.vue
Tonya d4e28e6f3b Upgrade frontend deps, including nuxt (#982)
* feat: begin upgrading deps, still very buggy

* feat: progress

* feat: sort all type issues

* fix: sort type issues

* fix: import sonner styles

* fix: nuxt is the enemy

* fix: try sorting issue with workflows

* fix: update vitest config for dynamic import of path and defineConfig

* fix: add missing import

* fix: add time out to try and fix issues

* fix: add ui:ci:preview task for frontend build in CI mode

* fix: i was silly

* feat: add go:ci:with-frontend task for CI mode and remove ui:ci:preview from e2e workflow

* fix: update baseURL in Playwright config for local testing to use port 7745

* fix: update E2E_BASE_URL and remove wait for timeout in login test for smoother execution
2025-09-04 09:00:25 +01:00

155 lines
4.6 KiB
Vue

<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { route } from "../../lib/api/base";
import PageQRCode from "./PageQRCode.vue";
import { DialogID } from "@/components/ui/dialog-provider/utils";
import { toast } from "@/components/ui/sonner";
import MdiLoading from "~icons/mdi/loading";
import MdiPrinterPos from "~icons/mdi/printer-pos";
import MdiFileDownload from "~icons/mdi/file-download";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { useDialog } from "@/components/ui/dialog-provider";
import { Button, ButtonGroup } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
const { t } = useI18n();
const { openDialog, closeDialog } = useDialog();
const props = defineProps<{
type: string;
id: string;
}>();
const pubApi = usePublicApi();
const { data: status } = useAsyncData(async () => {
const { data, error } = await pubApi.status();
if (error) {
toast.error(t("components.global.label_maker.toast.load_status_failed"));
return;
}
return data;
});
const serverPrinting = ref(false);
function browserPrint() {
const printWindow = window.open(getLabelUrl(false), "popup=true");
if (printWindow !== null) {
printWindow.onload = () => {
printWindow.print();
};
}
}
async function serverPrint() {
serverPrinting.value = true;
try {
await fetch(getLabelUrl(true));
} catch (err) {
console.error("Failed to print labels:", err);
serverPrinting.value = false;
toast.error(t("components.global.label_maker.toast.print_failed"));
return;
}
toast.success(t("components.global.label_maker.toast.print_success"));
closeDialog(DialogID.PrintLabel);
serverPrinting.value = false;
}
function downloadLabel() {
const link = document.createElement("a");
link.download = `label-${props.id}.png`;
link.href = getLabelUrl(false);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
function getLabelUrl(print: boolean): string {
const params = { print };
if (props.type === "item") {
return route(`/labelmaker/item/${props.id}`, params);
} else if (props.type === "location") {
return route(`/labelmaker/location/${props.id}`, params);
} else if (props.type === "asset") {
return route(`/labelmaker/asset/${props.id}`, params);
} else {
throw new Error(`Unexpected labelmaker type ${props.type}`);
}
}
</script>
<template>
<div>
<Dialog :dialog-id="DialogID.PrintLabel">
<DialogContent>
<DialogHeader>
<DialogTitle>
{{ $t("components.global.label_maker.print") }}
</DialogTitle>
<DialogDescription>
{{ $t("components.global.label_maker.confirm_description") }}
</DialogDescription>
</DialogHeader>
<img :src="getLabelUrl(false)" />
<DialogFooter>
<ButtonGroup>
<Button v-if="status?.labelPrinting || false" type="submit" :disabled="serverPrinting" @click="serverPrint">
<MdiLoading v-if="serverPrinting" class="animate-spin" />
{{ $t("components.global.label_maker.server_print") }}
</Button>
<Button type="submit" @click="browserPrint">
{{ $t("components.global.label_maker.browser_print") }}
</Button>
</ButtonGroup>
</DialogFooter>
</DialogContent>
</Dialog>
<TooltipProvider :delay-duration="0">
<ButtonGroup>
<Button variant="outline" disabled class="disabled:opacity-100">
{{ $t("components.global.label_maker.titles") }}
</Button>
<Tooltip>
<TooltipTrigger as-child>
<Button size="icon" @click="downloadLabel">
<MdiFileDownload name="mdi-file-download" />
</Button>
</TooltipTrigger>
<TooltipContent>
{{ $t("components.global.label_maker.download") }}
</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger as-child>
<Button size="icon" @click="openDialog(DialogID.PrintLabel)">
<MdiPrinterPos name="mdi-printer-pos" />
</Button>
</TooltipTrigger>
<TooltipContent>
{{ $t("components.global.label_maker.browser_print") }}
</TooltipContent>
</Tooltip>
<PageQRCode />
</ButtonGroup>
</TooltipProvider>
</div>
</template>