feat: barcode image output in PrintBarcodeModal

This commit is contained in:
2024-11-02 00:55:12 +04:00
parent a3fdefb774
commit e900901765
2 changed files with 48 additions and 26 deletions

View File

@@ -121,12 +121,7 @@ const BarcodeImageDropzone: FC<Props> = (props: Props) => {
accept={[ accept={[
"image/png", "image/png",
"image/jpeg", "image/jpeg",
"image/bmp",
"image/tiff",
"image/x-icon",
"image/webp", "image/webp",
"image/svg+xml",
"image/heic",
]} ]}
multiple={false} multiple={false}
onDrop={onDrop}> onDrop={onDrop}>

View File

@@ -7,6 +7,9 @@ import { notifications } from "../../shared/lib/notifications.ts";
import PrintBarcodeContainer from "./PrintBarcodeContainer.tsx"; import PrintBarcodeContainer from "./PrintBarcodeContainer.tsx";
import { base64ToBlob } from "../../shared/lib/utils.ts"; import { base64ToBlob } from "../../shared/lib/utils.ts";
// Barcode image aspects ratio should be equal 58/40
const BARCODE_IMAGE_RATIO = 1.45;
type Props = { type Props = {
productId: number; productId: number;
defaultQuantity?: number; defaultQuantity?: number;
@@ -16,6 +19,7 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
const [quantity, setQuantity] = useState(defaultQuantity); const [quantity, setQuantity] = useState(defaultQuantity);
const [barcode, setBarcode] = useState<string | undefined>(); const [barcode, setBarcode] = useState<string | undefined>();
const [barcodeData, setBarcodeData] = useState<BarcodeSchema | undefined>(); const [barcodeData, setBarcodeData] = useState<BarcodeSchema | undefined>();
const [barcodeImageUrl, setBarcodeImageUrl] = useState<string | undefined>();
const { product, refetch } = useGetProductById(productId); const { product, refetch } = useGetProductById(productId);
@@ -63,9 +67,18 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
setBarcodeData(barcode); setBarcodeData(barcode);
}); });
}; };
const fetchBarcodeImage = () => {
if (!product) return;
ProductService.getProductBarcodeImage({
productId: product.id,
}).then(res => {
if (res.barcodeImageUrl) setBarcodeImageUrl(res.barcodeImageUrl);
});
}
useEffect(() => { useEffect(() => {
if (!product) return; if (!product) return;
if (product.barcodes.length === 1) setBarcode(product.barcodes[0]); if (product.barcodes.length === 1) setBarcode(product.barcodes[0]);
fetchBarcodeImage();
}, [product]); }, [product]);
useEffect(() => { useEffect(() => {
@@ -78,6 +91,14 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
<Flex <Flex
gap={rem(10)} gap={rem(10)}
direction={"column"}> direction={"column"}>
{
barcodeImageUrl ? (
<object style={{
alignSelf: "center",
aspectRatio: BARCODE_IMAGE_RATIO,
width: "240px",
}} data={barcodeImageUrl} />
) : (
<Select <Select
value={barcode} value={barcode}
onChange={value => setBarcode(value || undefined)} onChange={value => setBarcode(value || undefined)}
@@ -85,6 +106,8 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
label={"Штрихкод"} label={"Штрихкод"}
placeholder={"Выберите штрихкод"} placeholder={"Выберите штрихкод"}
/> />
)
}
<NumberInput <NumberInput
label={"Количество копий"} label={"Количество копий"}
placeholder={"Введите количество копий"} placeholder={"Введите количество копий"}
@@ -99,6 +122,8 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
<Flex <Flex
direction={"column"} direction={"column"}
gap={rem(10)}> gap={rem(10)}>
{
!barcodeImageUrl && (
<Flex gap={rem(10)}> <Flex gap={rem(10)}>
<Button <Button
onClick={() => onAddClick()} onClick={() => onAddClick()}
@@ -113,6 +138,8 @@ const PrintBarcodeModal = ({ innerProps }: ContextModalProps<Props>) => {
Сгенерировать Сгенерировать
</Button> </Button>
</Flex> </Flex>
)
}
<Button <Button
size={"lg"} size={"lg"}
disabled={!barcode} disabled={!barcode}