Skip to content

Commit a9cff84

Browse files
committed
fix: code cleanup and update
1 parent 219bd12 commit a9cff84

8 files changed

Lines changed: 46 additions & 31 deletions

File tree

packages/pluggableWidgets/barcode-generator-web/src/BarcodeGenerator.editorConfig.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { StructurePreviewProps } from "@mendix/widget-plugin-platform/preview/structure-preview-api";
22
import { hidePropertiesIn, hidePropertyIn, Properties } from "@mendix/pluggable-widgets-tools";
3-
import { BarcodeGeneratorPreviewProps } from "../typings/BarcodeGeneratorProps";
3+
import { BarcodeGeneratorPreviewProps, CodeFormatEnum, CustomCodeFormatEnum } from "../typings/BarcodeGeneratorProps";
44
import { validateAddonValue, validateBarcodeValue } from "./config/validation";
55

66
export type Problem = {
@@ -144,7 +144,7 @@ export function check(_values: BarcodeGeneratorPreviewProps): Problem[] {
144144
return errors.concat(valueProblems);
145145
}
146146

147-
function getActiveFormat(values: BarcodeGeneratorPreviewProps): string {
147+
function getActiveFormat(values: BarcodeGeneratorPreviewProps): CodeFormatEnum | CustomCodeFormatEnum {
148148
if (values.codeFormat === "Custom") {
149149
return values.customCodeFormat || "CODE128";
150150
}

packages/pluggableWidgets/barcode-generator-web/src/BarcodeGenerator.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@
176176
</property>
177177
<property key="qrOverlay" type="boolean" required="true" defaultValue="false">
178178
<caption>Overlay image</caption>
179-
<description>Include an image on top the QR code</description>
179+
<description>Include an image overlay on the QR code</description>
180180
</property>
181181
<property key="qrOverlaySrc" type="image" required="true">
182182
<caption>Image source</caption>

packages/pluggableWidgets/barcode-generator-web/src/components/QRCode.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface QRCodeRendererProps {
1111
export function QRCodeRenderer({ config }: QRCodeRendererProps): ReactElement {
1212
const ref = useRef<SVGSVGElement>(null);
1313

14-
const { codeValue, downloadButton, size, margin, title, level, image } = config;
14+
const { codeValue, downloadButton, size, margin, title, level, overlay } = config;
1515
const buttonPosition = downloadButton?.buttonPosition ?? "bottom";
1616

1717
const button = downloadButton && (
@@ -33,7 +33,7 @@ export function QRCodeRenderer({ config }: QRCodeRendererProps): ReactElement {
3333
level={level}
3434
marginSize={margin}
3535
title={title}
36-
imageSettings={image}
36+
imageSettings={overlay}
3737
/>
3838
{buttonPosition === "bottom" && button}
3939
</div>

packages/pluggableWidgets/barcode-generator-web/src/config/Barcode.config.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { BarcodeGeneratorContainerProps, QrLevelEnum } from "../../typings/BarcodeGeneratorProps";
1+
import {
2+
AddonFormatEnum,
3+
BarcodeGeneratorContainerProps,
4+
CodeFormatEnum,
5+
CustomCodeFormatEnum,
6+
QrLevelEnum
7+
} from "../../typings/BarcodeGeneratorProps";
28

39
interface DownloadButtonConfig {
410
caption?: string;
@@ -7,9 +13,9 @@ interface DownloadButtonConfig {
713
buttonPosition: "top" | "bottom";
814
}
915

10-
type codeType = "barcode" | "qrcode";
16+
type CodeType = "barcode" | "qrcode";
1117

12-
export interface CodeBaseTypeConfig<T = codeType> extends Pick<BarcodeGeneratorContainerProps, "logLevel"> {
18+
export interface CodeBaseTypeConfig<T = CodeType> extends Pick<BarcodeGeneratorContainerProps, "logLevel"> {
1319
type: T;
1420
codeValue: string;
1521
margin: number;
@@ -20,7 +26,7 @@ export interface CodeBaseTypeConfig<T = codeType> extends Pick<BarcodeGeneratorC
2026
export interface BarcodeTypeConfig extends CodeBaseTypeConfig<"barcode"> {
2127
width: number;
2228
height: number;
23-
format: string;
29+
format: CodeFormatEnum | CustomCodeFormatEnum;
2430
displayValue: boolean;
2531

2632
// Advanced barcode options
@@ -29,7 +35,7 @@ export interface BarcodeTypeConfig extends CodeBaseTypeConfig<"barcode"> {
2935
lastChar: string;
3036
enableMod43: boolean;
3137
addonValue: string;
32-
addonFormat: string;
38+
addonFormat: AddonFormatEnum | null | undefined;
3339
addonSpacing: number;
3440
}
3541

@@ -38,7 +44,7 @@ export interface QRCodeTypeConfig extends CodeBaseTypeConfig<"qrcode"> {
3844
size: number;
3945
title: string;
4046
level: QrLevelEnum;
41-
image?: {
47+
overlay?: {
4248
src: string;
4349
x: number | undefined;
4450
y: number | undefined;
@@ -79,7 +85,7 @@ export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeCon
7985
size: props.qrSize ?? 128,
8086
title: props.qrTitle ?? "",
8187
level: props.qrLevel ?? "L",
82-
image:
88+
overlay:
8389
props.qrOverlaySrc?.status === "available"
8490
? {
8591
src: props.qrOverlaySrc.value.uri,

packages/pluggableWidgets/barcode-generator-web/src/config/validation.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
export type ValidationResult = {
2-
valid: boolean;
3-
// `message` is a plain, non-localized message shown in Studio if validation fails.
4-
message?: string;
5-
};
1+
import { AddonFormatEnum, CodeFormatEnum, CustomCodeFormatEnum } from "../../typings/BarcodeGeneratorProps";
2+
3+
export type ValidationResult =
4+
| {
5+
valid: true;
6+
}
7+
| {
8+
valid: false;
9+
// `message` is a plain, non-localized message shown in Studio if validation fails.
10+
message: string;
11+
};
612

713
/** Validate barcode value for a given format. */
8-
export function validateBarcodeValue(format: string, value: string): ValidationResult {
14+
export function validateBarcodeValue(format: CustomCodeFormatEnum | CodeFormatEnum, value: string): ValidationResult {
915
// If no value is present at design time, assume dynamic binding will provide it at runtime.
1016
if (!value) {
1117
return { valid: true };
@@ -135,7 +141,7 @@ export function validateBarcodeValue(format: string, value: string): ValidationR
135141
}
136142

137143
/** Validate addon (EAN-5 / EAN-2) values. */
138-
export function validateAddonValue(addonFormat: string | null | undefined, value: string): ValidationResult {
144+
export function validateAddonValue(addonFormat: AddonFormatEnum | null | undefined, value: string): ValidationResult {
139145
if (!addonFormat || addonFormat === "None") {
140146
return { valid: true };
141147
}

packages/pluggableWidgets/barcode-generator-web/src/hooks/useRenderBarcode.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import { BarcodeTypeConfig } from "../config/Barcode.config";
22
import { RefObject, useEffect, useRef, useState } from "react";
33
import { type BarcodeRenderOptions, renderBarcode } from "../utils/barcodeRenderer-utils";
44
import { validateAddonValue, validateBarcodeValue } from "../config/validation";
5-
import { LogLevelEnum } from "../../typings/BarcodeGeneratorProps";
6-
7-
function printError(message: string, logLevel: LogLevelEnum) {
8-
if (logLevel === "Debug") {
9-
console.error(`[Barcode Generator] ${message}`);
10-
}
11-
}
5+
import { printError } from "../utils/helpers";
126

137
export const useRenderBarcode = (
148
config: BarcodeTypeConfig
@@ -40,7 +34,7 @@ export const useRenderBarcode = (
4034
// Validate barcode value at runtime
4135
const validationResult = validateBarcodeValue(format, value);
4236
if (!validationResult.valid) {
43-
const errorMsg = validationResult.message || "Invalid barcode value";
37+
const errorMsg = validationResult.message;
4438
// Log detailed error for developers
4539

4640
printError(
@@ -55,7 +49,7 @@ export const useRenderBarcode = (
5549
if (addonValue && addonFormat && addonFormat !== "None") {
5650
const addonResult = validateAddonValue(addonFormat, addonValue);
5751
if (!addonResult.valid) {
58-
const errorMsg = addonResult.message || "Invalid addon value";
52+
const errorMsg = addonResult.message;
5953
// Log detailed error for developers
6054
printError(
6155
`Addon validation failed for format "${addonFormat}": ${errorMsg} \nProvided addon value: "${addonValue}"`,
@@ -108,7 +102,8 @@ export const useRenderBarcode = (
108102
enableMod43,
109103
addonValue,
110104
addonFormat,
111-
addonSpacing
105+
addonSpacing,
106+
config.logLevel
112107
]);
113108

114109
return { ref, error };

packages/pluggableWidgets/barcode-generator-web/src/utils/barcodeRenderer-utils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import JsBarcode from "jsbarcode";
22
import { type ForwardedRef } from "react";
3+
import { AddonFormatEnum } from "typings/BarcodeGeneratorProps";
34

45
interface BarcodeMethodOptions {
56
width?: number;
@@ -42,7 +43,7 @@ export interface BarcodeRenderOptions {
4243
lastChar?: string;
4344
mod43?: boolean;
4445
addonValue?: string;
45-
addonFormat?: string;
46+
addonFormat?: AddonFormatEnum | null | undefined;
4647
addonSpacing?: number;
4748
}
4849

@@ -54,7 +55,7 @@ export const createBarcodeWithAddon = (
5455
value: string,
5556
mainFormat: string,
5657
addonValue: string,
57-
addonFormat: string,
58+
addonFormat: AddonFormatEnum,
5859
options: BarcodeOptions,
5960
addonSpacing: number
6061
): void => {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { LogLevelEnum } from "../../typings/BarcodeGeneratorProps";
2+
3+
export function printError(message: string, logLevel: LogLevelEnum) {
4+
if (logLevel === "Debug") {
5+
console.error(`[Barcode Generator] ${message}`);
6+
}
7+
}

0 commit comments

Comments
 (0)