diff --git a/showbits-thermal-printer-ui/eslint.config.ts b/showbits-thermal-printer-ui/eslint.config.ts index cfe16b0..8dde095 100644 --- a/showbits-thermal-printer-ui/eslint.config.ts +++ b/showbits-thermal-printer-ui/eslint.config.ts @@ -37,7 +37,6 @@ export default tseslint.config( eqeqeq: "error", // https://typescript-eslint.io/rules/ - "@typescript-eslint/explicit-function-return-type": "warn", "@typescript-eslint/naming-convention": [ "warn", // Default settings diff --git a/showbits-thermal-printer-ui/src/apiRequest.ts b/showbits-thermal-printer-ui/src/apiRequest.ts new file mode 100644 index 0000000..51657dd --- /dev/null +++ b/showbits-thermal-printer-ui/src/apiRequest.ts @@ -0,0 +1,35 @@ +import { ref } from "vue"; + +async function waitAtLeast(duration: number, since: number): Promise { + const now = Date.now(); + const wait = duration - (now - since); + if (wait > 0) { + await new Promise((resolve) => setTimeout(resolve, wait)); + } +} + +export function useApiRequest() { + const disabled = ref(false); + const error = ref(); + + async function makeRequest(url: string, data: URLSearchParams | FormData) { + const start = Date.now(); + disabled.value = true; + + try { + const response = await fetch(url, { method: "POST", body: data }); + if (!response.ok) { + const status = `${response.status.toFixed()} ${response.statusText}`; + const text = await response.text(); + error.value = text.length > 0 ? `${status}: ${text}` : status; + } + } catch (err) { + error.value = String(err); + } + + await waitAtLeast(500, start); + disabled.value = false; + } + + return { disabled, error, makeRequest }; +} diff --git a/showbits-thermal-printer-ui/src/components/CSegmentCalendar.vue b/showbits-thermal-printer-ui/src/components/CSegmentCalendar.vue index 2af331c..185a74a 100644 --- a/showbits-thermal-printer-ui/src/components/CSegmentCalendar.vue +++ b/showbits-thermal-printer-ui/src/components/CSegmentCalendar.vue @@ -1,48 +1,22 @@ diff --git a/showbits-thermal-printer-ui/src/components/CSegmentText.vue b/showbits-thermal-printer-ui/src/components/CSegmentText.vue index 5a4304e..1e924fb 100644 --- a/showbits-thermal-printer-ui/src/components/CSegmentText.vue +++ b/showbits-thermal-printer-ui/src/components/CSegmentText.vue @@ -1,10 +1,10 @@