Skip to content

Commit

Permalink
Merge pull request #461 from samturner3/master
Browse files Browse the repository at this point in the history
Feature: Add option to use full URL in QR codes instead of web+spoolman: prefix
  • Loading branch information
Donkie authored Aug 26, 2024
2 parents 93c9f46 + e65b87c commit a6d271a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 3 deletions.
13 changes: 13 additions & 0 deletions client/public/locales/cs/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,15 @@
"showSpoolComment": "Poznámka k cívce",
"showFilamentComment": "Poznámka k filamentu",
"showVendorComment": "Poznámka k výrobci",
"useHTTPUrl": {
"label": "Odkaz na QR kód",
"tooltip": "Použije proprietární odkaz, který bude fungovat pouze v případě, že bude skenován pomocí funkce skenování Spoolman (výchozí). Adresa URL používá buď základní adresu URL zadanou v nastavení, nebo adresu URL aktuální stránky, pokud není nastavena.",
"options": {
"default": "Výchozí",
"url": "URL"
},
"preview": "Náhled:"
},
"template": "Šablona štítku",
"templateHelp": "Pomocí {} vložíte hodnoty objektu cívky jako text. Například {id} bude nahrazeno ID cívky nebo {filament.material} bude nahrazeno materiálem cívky. Text uzavřete dvojitou hvězdičkou **, aby byl tučný. Kliknutím na tlačítko zobrazíte seznam všech dostupných značek.",
"showQRCode": "Tisk QR kódu",
Expand Down Expand Up @@ -374,6 +383,10 @@
"tab": "Obecné",
"currency": {
"label": "Měna"
},
"base_url": {
"label": "Základní URL",
"tooltip": "Základní adresa URL, která se má použít při generování funkcí, jako jsou QR kódy"
}
},
"settings": "Nastavení"
Expand Down
13 changes: 13 additions & 0 deletions client/public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@
"templateHelp": "Use {} to insert values of the spool object as text. For example {id} will be replaced with the spool id, or {filament.material} will be replaced with the material of the spool. Enclose text with double asterix ** to make it bold. Click the button to view a list of all available tags.",
"textSize": "Label Text Size",
"showContent": "Print Label",
"useHTTPUrl": {
"label": "QR code link",
"tooltip": "Will use proprietary link that will work only if scanned from Spoolman's scanning feature (default). URL uses either the base URL specified in settings, or the current page URL if not set.",
"options": {
"default": "Default",
"url": "URL"
},
"preview": "Preview:"
},
"showQRCode": "Print QR Code",
"showQRCodeMode": {
"no": "No",
Expand Down Expand Up @@ -300,6 +309,10 @@
"tab": "General",
"currency": {
"label": "Currency"
},
"base_url": {
"label": "Base URL",
"tooltip": "The base URL to use when generating features such as QR codes."
}
},
"extra_fields": {
Expand Down
5 changes: 5 additions & 0 deletions client/src/components/qrCodeScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const QRCodeScannerModal: React.FC = () => {
setVisible(false);
navigate(`/spool/show/${match.groups.id}`);
}
const fullURLmatch = result.match(/^https?:\/\/[^/]+\/spool\/show\/(?<id>[0-9]+)$/);
if (fullURLmatch && fullURLmatch.groups) {
setVisible(false);
navigate(`/spool/show/${fullURLmatch.groups.id}`);
}
};

return (
Expand Down
27 changes: 26 additions & 1 deletion client/src/pages/printing/qrCodePrintingDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useTranslate } from "@refinedev/core";
import { Col, Form, InputNumber, QRCode, Radio, RadioChangeEvent, Row, Slider, Switch } from "antd";
import { Col, Form, InputNumber, QRCode, Radio, RadioChangeEvent, Row, Slider, Switch, Typography } from "antd";
import { QRCodePrintSettings } from "./printing";
import PrintingDialog from "./printingDialog";

const { Text } = Typography;

interface QRCodeData {
value: string;
label?: JSX.Element;
Expand All @@ -16,6 +18,9 @@ interface QRCodePrintingDialogProps {
extraSettings?: JSX.Element;
extraSettingsStart?: JSX.Element;
extraButtons?: JSX.Element;
baseUrlRoot: string;
useHTTPUrl: boolean;
setUseHTTPUrl: (value: boolean) => void;
}

const QRCodePrintingDialog: React.FC<QRCodePrintingDialogProps> = ({
Expand All @@ -25,6 +30,9 @@ const QRCodePrintingDialog: React.FC<QRCodePrintingDialogProps> = ({
extraSettings,
extraSettingsStart,
extraButtons,
baseUrlRoot,
useHTTPUrl,
setUseHTTPUrl,
}) => {
const t = useTranslate();

Expand Down Expand Up @@ -87,6 +95,23 @@ const QRCodePrintingDialog: React.FC<QRCodePrintingDialogProps> = ({
buttonStyle="solid"
/>
</Form.Item>
{showQRCodeMode !== "no" && (
<>
<Form.Item
label={t("printing.qrcode.useHTTPUrl.label")}
tooltip={t("printing.qrcode.useHTTPUrl.tooltip")}
style={{ marginBottom: 0 }}
>
<Radio.Group onChange={(e) => setUseHTTPUrl(e.target.value)} value={useHTTPUrl}>
<Radio value={false}>{t("printing.qrcode.useHTTPUrl.options.default")}</Radio>
<Radio value={true}>{t("printing.qrcode.useHTTPUrl.options.url")}</Radio>
</Radio.Group>
</Form.Item>
<Form.Item label={t("printing.qrcode.useHTTPUrl.preview")}>
<Text> {useHTTPUrl ? `${baseUrlRoot}/spool/show/{id}` : `web+spoolman:s-{id}`}</Text>
</Form.Item>
</>
)}
<Form.Item label={t("printing.qrcode.showContent")}>
<Switch
checked={showContent}
Expand Down
12 changes: 11 additions & 1 deletion client/src/pages/printing/spoolQrCodePrintingDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CopyOutlined, DeleteOutlined, PlusOutlined, SaveOutlined } from "@ant-design/icons";
import { useGetSetting } from "../../utils/querySettings";
import { useTranslate } from "@refinedev/core";
import { Button, Flex, Form, Input, Modal, Popconfirm, Select, Table, Typography, message } from "antd";
import TextArea from "antd/es/input/TextArea";
Expand All @@ -24,7 +25,13 @@ interface SpoolQRCodePrintingDialog {

const SpoolQRCodePrintingDialog: React.FC<SpoolQRCodePrintingDialog> = ({ spoolIds }) => {
const t = useTranslate();
const baseUrlSetting = useGetSetting("base_url");
const baseUrlRoot =
baseUrlSetting.data?.value !== undefined && JSON.parse(baseUrlSetting.data?.value) !== ""
? JSON.parse(baseUrlSetting.data?.value)
: window.location.origin;
const [messageApi, contextHolder] = message.useMessage();
const [useHTTPUrl, setUseHTTPUrl] = useSavedState("print-useHTTPUrl", false);

const itemQueries = useGetSpoolsByIds(spoolIds);
const items = itemQueries
Expand Down Expand Up @@ -231,6 +238,9 @@ Lot Nr: {lot_nr}
curPreset.labelSettings = newSettings;
updateCurrentPreset(curPreset);
}}
baseUrlRoot={baseUrlRoot}
useHTTPUrl={useHTTPUrl}
setUseHTTPUrl={setUseHTTPUrl}
extraSettingsStart={
<>
<Form.Item label={t("printing.generic.settings")}>
Expand Down Expand Up @@ -290,7 +300,7 @@ Lot Nr: {lot_nr}
</>
}
items={items.map((spool) => ({
value: `web+spoolman:s-${spool.id}`,
value: useHTTPUrl ? `${baseUrlRoot}/spool/show/${spool.id}` : `web+spoolman:s-${spool.id}`,
label: (
<p
style={{
Expand Down
24 changes: 23 additions & 1 deletion client/src/pages/settings/generalSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useGetSettings, useSetSetting } from "../../utils/querySettings";

export function GeneralSettings() {
const settings = useGetSettings();
const setBaseUrl = useSetSetting("base_url");
const setCurrency = useSetSetting("currency");
const [form] = Form.useForm();
const [messageApi, contextHolder] = message.useMessage();
Expand All @@ -15,6 +16,7 @@ export function GeneralSettings() {
if (settings.data) {
form.setFieldsValue({
currency: JSON.parse(settings.data.currency.value),
base_url: JSON.parse(settings.data.base_url.value),
});
}
}, [settings.data, form]);
Expand All @@ -27,11 +29,15 @@ export function GeneralSettings() {
}, [setCurrency.isSuccess, messageApi, t]);

// Handle form submit
const onFinish = (values: { currency: string }) => {
const onFinish = (values: { currency: string; base_url: string }) => {
// Check if the currency has changed
if (settings.data?.currency.value !== JSON.stringify(values.currency)) {
setCurrency.mutate(values.currency);
}
// Check if the base URL has changed
if (settings.data?.base_url.value !== JSON.stringify(values.base_url)) {
setBaseUrl.mutate(values.base_url);
}
};

return (
Expand Down Expand Up @@ -64,6 +70,22 @@ export function GeneralSettings() {
<Input />
</Form.Item>

<Form.Item
label={t("settings.general.base_url.label")}
tooltip={t("settings.general.base_url.tooltip")}
name="base_url"
rules={[
{
required: false,
},
{
pattern: /^https?:\/\/.+(?<!\/)$/,
},
]}
>
<Input placeholder="https://example.com:8000" />
</Form.Item>

<Form.Item wrapperCol={{ offset: 8, span: 16 }}>
<Button type="primary" htmlType="submit" loading={settings.isFetching || setCurrency.isLoading}>
{t("buttons.save")}
Expand Down
1 change: 1 addition & 0 deletions spoolman/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ def parse_setting(key: str) -> SettingDefinition:
register_setting("extra_fields_vendor", SettingType.ARRAY, json.dumps([]))
register_setting("extra_fields_filament", SettingType.ARRAY, json.dumps([]))
register_setting("extra_fields_spool", SettingType.ARRAY, json.dumps([]))
register_setting("base_url", SettingType.STRING, json.dumps(""))

0 comments on commit a6d271a

Please sign in to comment.