Skip to content

Commit

Permalink
Add Link/Unlink Barcode action
Browse files Browse the repository at this point in the history
  • Loading branch information
matmair committed Aug 20, 2024
1 parent 5515e07 commit e3b675c
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 35 deletions.
85 changes: 68 additions & 17 deletions src/frontend/src/components/items/ActionDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ReactNode, useMemo } from 'react';
import { ModelType } from '../../enums/ModelType';
import { identifierString } from '../../functions/conversion';
import { InvenTreeIcon } from '../../functions/icons';
import { InvenTreeQRCode } from './QRCode';
import { InvenTreeQRCode, QRCodeLink, QRCodeUnlink } from './QRCode';

export type ActionDropdownItem = {
icon?: ReactNode;
Expand Down Expand Up @@ -151,28 +151,79 @@ export function ViewBarcodeAction({
};
}

// Common action button for linking a custom barcode
export function LinkBarcodeAction(
props: ActionDropdownItem
): ActionDropdownItem {
function GeneralBarcodeAction({
hidden = false,
model,
pk,
title,
icon,
tooltip,
ChildItem
}: {
hidden?: boolean;
model: ModelType;
pk: number;
title: string;
icon: ReactNode;
tooltip: string;
ChildItem: any;
}): ActionDropdownItem {
const onClick = () => {
modals.open({
title: title,
children: <ChildItem model={model} pk={pk} />
});
};

return {
...props,
icon: <IconLink />,
name: t`Link Barcode`,
tooltip: t`Link custom barcode`
icon: icon,
name: title,
tooltip: tooltip,
onClick: onClick,
hidden: hidden
};
}

// Common action button for linking a custom barcode
export function LinkBarcodeAction({
hidden = false,
model,
pk
}: {
hidden?: boolean;
model: ModelType;
pk: number;
}): ActionDropdownItem {
return GeneralBarcodeAction({
hidden: hidden,
model: model,
pk: pk,
title: t`Link Barcode`,
icon: <IconLink />,
tooltip: t`Link a custom barcode to this item`,
ChildItem: QRCodeLink
});
}

// Common action button for un-linking a custom barcode
export function UnlinkBarcodeAction(
props: ActionDropdownItem
): ActionDropdownItem {
return {
...props,
export function UnlinkBarcodeAction({
hidden = false,
model,
pk
}: {
hidden?: boolean;
model: ModelType;
pk: number;
}): ActionDropdownItem {
return GeneralBarcodeAction({
hidden: hidden,
model: model,
pk: pk,
title: t`Unlink Barcode`,
icon: <IconUnlink />,
name: t`Unlink Barcode`,
tooltip: t`Unlink custom barcode`
};
tooltip: t`Unlink custom barcode`,
ChildItem: QRCodeUnlink
});
}

// Common action button for editing an item
Expand Down
60 changes: 59 additions & 1 deletion src/frontend/src/components/items/QRCode.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Trans, t } from '@lingui/macro';
import {
Box,
Button,
Code,
Group,
Image,
Select,
Skeleton,
Stack,
Text
Text,
TextInput
} from '@mantine/core';
import { modals } from '@mantine/modals';
import { useQuery } from '@tanstack/react-query';
import QR from 'qrcode';
import { useEffect, useMemo, useState } from 'react';
Expand Down Expand Up @@ -128,3 +131,58 @@ export const InvenTreeQRCode = ({
</Stack>
);
};

export const QRCodeLink = ({ model, pk }: { model: ModelType; pk: number }) => {
const [barcode, setBarcode] = useState<string>();
function linkBarcode() {
api
.post(apiUrl(ApiEndpoints.barcode_link), {
[model]: pk,
barcode: barcode
})
.then((response) => {
modals.closeAll();
location.reload();
});
}
return (
<Box>
<TextInput
label={t`Barcode`}
value={barcode}
placeholder={t`Scan barcode data here using barcode scanner`}
onChange={(event) => setBarcode(event.target.value)}
/>
<Button color="green" onClick={linkBarcode} mt="lg" fullWidth>
<Trans>Link</Trans>
</Button>
</Box>
);
};

export const QRCodeUnlink = ({
model,
pk
}: {
model: ModelType;
pk: number;
}) => {
function unlinkBarcode() {
api
.post(apiUrl(ApiEndpoints.barcode_unlink), { [model]: pk })
.then((response) => {
modals.closeAll();
location.reload();
});
}
return (
<Box>
<Text>
<Trans>This will remove the link to the associated barcode</Trans>
</Text>
<Button color="red" onClick={unlinkBarcode}>
<Trans>Unlink Barcode</Trans>
</Button>
</Box>
);
};
2 changes: 2 additions & 0 deletions src/frontend/src/enums/ApiEndpoints.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export enum ApiEndpoints {
settings_global_list = 'settings/global/',
settings_user_list = 'settings/user/',
barcode = 'barcode/',
barcode_link = 'barcode/link/',
barcode_unlink = 'barcode/unlink/',
generate_barcode = 'barcode/generate/',
news = 'news/',
global_status = 'generic/status/',
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/build/BuildDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -479,11 +479,13 @@ export default function BuildDetail() {
}),
LinkBarcodeAction({
hidden: build?.barcode_hash,
onClick: notYetImplemented
model: ModelType.build,
pk: build.pk
}),
UnlinkBarcodeAction({
hidden: !build?.barcode_hash,
onClick: notYetImplemented
model: ModelType.build,
pk: build.pk
})
]}
/>,
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/company/SupplierPartDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,15 @@ export default function SupplierPartDetail() {
hidden:
supplierPart.barcode_hash ||
!user.hasChangeRole(UserRoles.purchase_order),
onClick: notYetImplemented
model: ModelType.supplierpart,
pk: supplierPart.pk
}),
UnlinkBarcodeAction({
hidden:
!supplierPart.barcode_hash ||
!user.hasChangeRole(UserRoles.purchase_order),
onClick: notYetImplemented
model: ModelType.supplierpart,
pk: supplierPart.pk
})
]}
/>,
Expand Down
7 changes: 4 additions & 3 deletions src/frontend/src/pages/part/PartDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ import {
useTransferStockItem
} from '../../forms/StockForms';
import { InvenTreeIcon } from '../../functions/icons';
import { notYetImplemented } from '../../functions/notifications';
import { getDetailUrl } from '../../functions/urls';
import {
useCreateApiFormModal,
Expand Down Expand Up @@ -980,11 +979,13 @@ export default function PartDetail() {
}),
LinkBarcodeAction({
hidden: part?.barcode_hash || !user.hasChangeRole(UserRoles.part),
onClick: notYetImplemented
model: ModelType.part,
pk: part.pk
}),
UnlinkBarcodeAction({
hidden: !part?.barcode_hash || !user.hasChangeRole(UserRoles.part),
onClick: notYetImplemented
model: ModelType.part,
pk: part.pk
})
]}
key="action_dropdown"
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/purchasing/PurchaseOrderDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -410,11 +410,13 @@ export default function PurchaseOrderDetail() {
}),
LinkBarcodeAction({
hidden: order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.purchaseorder,
pk: order.pk
}),
UnlinkBarcodeAction({
hidden: !order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.purchaseorder,
pk: order.pk
})
]}
/>,
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/sales/ReturnOrderDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,13 @@ export default function ReturnOrderDetail() {
}),
LinkBarcodeAction({
hidden: order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.returnorder,
pk: order.pk
}),
UnlinkBarcodeAction({
hidden: !order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.returnorder,
pk: order.pk
})
]}
/>,
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/sales/SalesOrderDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,13 @@ export default function SalesOrderDetail() {
}),
LinkBarcodeAction({
hidden: order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.salesorder,
pk: order.pk
}),
UnlinkBarcodeAction({
hidden: !order?.barcode_hash,
onClick: notYetImplemented
model: ModelType.salesorder,
pk: order.pk
})
]}
/>,
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/stock/LocationDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,12 @@ export default function Stock() {
pk: location.pk
}),
LinkBarcodeAction({
onClick: notYetImplemented
model: ModelType.stocklocation,
pk: location.pk
}),
UnlinkBarcodeAction({
onClick: notYetImplemented
model: ModelType.stocklocation,
pk: location.pk
}),
{
name: 'Scan in stock items',
Expand Down
6 changes: 4 additions & 2 deletions src/frontend/src/pages/stock/StockDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,14 @@ export default function StockDetail() {
LinkBarcodeAction({
hidden:
stockitem?.barcode_hash || !user.hasChangeRole(UserRoles.stock),
onClick: notYetImplemented
model: ModelType.stockitem,
pk: stockitem.pk
}),
UnlinkBarcodeAction({
hidden:
!stockitem?.barcode_hash || !user.hasChangeRole(UserRoles.stock),
onClick: notYetImplemented
model: ModelType.stockitem,
pk: stockitem.pk
})
]}
/>,
Expand Down

0 comments on commit e3b675c

Please sign in to comment.