feat(ui): custom label and options formatting and display api url

This commit is contained in:
Pihkaal
2024-10-02 15:03:02 +02:00
parent 84f74250cc
commit e6c7bb2cfb

121
app.vue
View File

@@ -1,13 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { renderQRCodeToCanvas } from "@/utils/renderer"; // TODO: remove error message
import { // TODO: disable buttons when no qr code is generated (it is currenly possible to download default qr code)
IMAGE_FORMATS,
LOGOS,
settingsSchema,
type Settings,
} from "@/utils/settings";
import type { FormSubmitEvent } from "#ui/types"; import { renderQRCodeToCanvas } from "@/utils/renderer";
import { IMAGE_FORMATS, LOGOS, settingsSchema } from "@/utils/settings";
const canvas = ref(null); const canvas = ref(null);
const qrCode = ref("/default.webp"); const qrCode = ref("/default.webp");
@@ -17,11 +13,25 @@ const copyLabel = ref("Copy");
const isQRCodeEmpty = computed(() => qrCode.value.length === 0); const isQRCodeEmpty = computed(() => qrCode.value.length === 0);
const state = reactive({ const state = reactive({
logo: LOGOS[0], logo: undefined,
format: IMAGE_FORMATS[0], format: IMAGE_FORMATS[0],
content: undefined, content: undefined,
}); });
const BASE_API_URL = "http://localhost:3000/api";
const apiUrl = computed(() => {
if (!state.content) return "";
const params = new URLSearchParams({
logo: state.logo,
format: state.format,
content: state.content,
});
return `${BASE_API_URL}?${params}`;
});
const updateQRCode = async () => { const updateQRCode = async () => {
if (!state.content) return; if (!state.content) return;
@@ -60,6 +70,10 @@ const copyQRCode = async () => {
copyLabel.value = "Copy"; copyLabel.value = "Copy";
}, 3000); }, 3000);
}; };
const capitalize = (str: string) => str.charAt(0).toUpperCase() + str.slice(1);
const upperCase = (str: string) => str.toUpperCase();
</script> </script>
<template> <template>
@@ -68,7 +82,7 @@ const copyQRCode = async () => {
<canvas ref="canvas" class="hidden" /> <canvas ref="canvas" class="hidden" />
<div <div
class="w-full h-full max-w-[850px] max-h-[375px] flex justify-between gap-8" class="w-full h-full max-w-[850px] max-h-[375px] flex justify-between space-x-10"
> >
<img :src="qrCode" class="h-full aspect-square" /> <img :src="qrCode" class="h-full aspect-square" />
@@ -79,49 +93,86 @@ const copyQRCode = async () => {
name="content" name="content"
@input="updateQRCode" @input="updateQRCode"
> >
<UInput v-model="state.content" /> <UInput
v-model="state.content"
placeholder="Your username or profile link"
/>
</UFormGroup> </UFormGroup>
<UFormGroup label="Logo" name="logo"> <UFormGroup label="Logo" name="logo">
<USelectMenu <USelectMenu
v-model="state.logo" v-model="state.logo"
:options="LOGOS" :options="LOGOS"
placeholder="Select logo"
searchable searchable
@change="updateQRCode" @change="updateQRCode"
/> >
<template #label>
<span v-if="state.logo">{{ capitalize(state.logo) }}</span>
</template>
<template #option="props">
<span>{{ capitalize(props.option) }}</span>
</template>
</USelectMenu>
</UFormGroup> </UFormGroup>
<UFormGroup label="Format" name="format"> <UFormGroup label="Format" name="format">
<USelectMenu <USelectMenu
v-model="state.format" v-model="state.format"
:options="IMAGE_FORMATS" :options="IMAGE_FORMATS"
placeholder="Select format"
@change="updateQRCode" @change="updateQRCode"
/> >
<template #label>
<span v-if="state.format">{{ upperCase(state.format) }}</span>
</template>
<template #option="props">
<span>{{ upperCase(props.option) }}</span>
</template>
</USelectMenu>
</UFormGroup> </UFormGroup>
<UButton <UFormGroup label="API">
block <UButtonGroup size="sm" orientation="horizontal" class="w-full">
:icon="copyIcon" <UInput
size="md" v-model="apiUrl"
color="primary" disabled
variant="solid" placeholder="Generate QRCode first"
:label="copyLabel" class="w-full"
:trailing="false" />
:disabled="isQRCodeEmpty" <UButton icon="i-heroicons-clipboard-document" />
@click="copyQRCode" </UButtonGroup>
/> </UFormGroup>
<UButton <div class="flex space-x-4 pt-2">
block <UButton
icon="i-heroicons-arrow-down-tray" class="flex-1"
size="md" block
color="primary" :icon="copyIcon"
variant="solid" size="md"
label="Download" color="primary"
:trailing="false" variant="solid"
:disabled="isQRCodeEmpty" :label="copyLabel"
@click="downloadQRCode" :trailing="false"
/> :disabled="isQRCodeEmpty"
@click="copyQRCode"
/>
<UButton
class="flex-1"
block
icon="i-heroicons-arrow-down-tray"
size="md"
color="primary"
variant="solid"
label="Download"
:trailing="false"
:disabled="isQRCodeEmpty"
@click="downloadQRCode"
/>
</div>
</UForm> </UForm>
</div> </div>
</div> </div>