From 368d59ee55eeae5a83b4b6d335e254e758165e48 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 29 Aug 2024 14:34:34 +1000 Subject: [PATCH] Prevent editing of attachment file on test result after upload (#8032) --- src/frontend/src/forms/StockForms.tsx | 14 +++++++-- .../tables/stock/StockItemTestResultTable.tsx | 29 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/frontend/src/forms/StockForms.tsx b/src/frontend/src/forms/StockForms.tsx index 772d704d98..2b519f6a8e 100644 --- a/src/frontend/src/forms/StockForms.tsx +++ b/src/frontend/src/forms/StockForms.tsx @@ -914,11 +914,13 @@ export function useTestResultFields({ partId, itemId, templateId, + editing = false, editTemplate = false }: { partId: number; itemId: number; templateId: number | undefined; + editing?: boolean; editTemplate?: boolean; }): ApiFormFieldSet { // Valid field choices @@ -927,7 +929,7 @@ export function useTestResultFields({ // Field type for the "value" input const [fieldType, setFieldType] = useState<'string' | 'choice'>('string'); - const settings = useGlobalSettingsState.getState(); + const settings = useGlobalSettingsState(); const includeTestStation = useMemo( () => settings.isSet('TEST_STATION_DATA'), @@ -935,7 +937,7 @@ export function useTestResultFields({ ); return useMemo(() => { - return { + let fields: ApiFormFieldSet = { stock_item: { value: itemId, hidden: true @@ -985,8 +987,16 @@ export function useTestResultFields({ hidden: !includeTestStation } }; + + if (editing) { + // Prevent changing uploaded attachments + delete fields.attachment; + } + + return fields; }, [ choices, + editing, editTemplate, fieldType, partId, diff --git a/src/frontend/src/tables/stock/StockItemTestResultTable.tsx b/src/frontend/src/tables/stock/StockItemTestResultTable.tsx index 4eca85588f..bc0ae0085b 100644 --- a/src/frontend/src/tables/stock/StockItemTestResultTable.tsx +++ b/src/frontend/src/tables/stock/StockItemTestResultTable.tsx @@ -27,6 +27,7 @@ import { } from '../../hooks/UseForm'; import { useTable } from '../../hooks/UseTable'; import { apiUrl } from '../../states/ApiState'; +import { useGlobalSettingsState } from '../../states/SettingsState'; import { useUserState } from '../../states/UserState'; import { TableColumn } from '../Column'; import { DateColumn, DescriptionColumn, NoteColumn } from '../ColumnRenderers'; @@ -49,6 +50,11 @@ export default function StockItemTestResultTable({ const user = useUserState(); const table = useTable('stocktests'); + const globalSettings = useGlobalSettingsState(); + const includeTestStation = useMemo( + () => globalSettings.isSet('TEST_STATION_DATA'), + [globalSettings] + ); // Fetch the test templates required for this stock item const { data: testTemplates } = useQuery({ queryKey: ['stocktesttemplates', partId, itemId], @@ -203,12 +209,14 @@ export default function StockItemTestResultTable({ { accessor: 'test_station', sortable: true, - title: t`Test station` + title: t`Test station`, + hidden: !includeTestStation }, { accessor: 'started_datetime', sortable: true, title: t`Started`, + hidden: !includeTestStation, render: (record: any) => { return ( @@ -224,6 +232,7 @@ export default function StockItemTestResultTable({ accessor: 'finished_datetime', sortable: true, title: t`Finished`, + hidden: !includeTestStation, render: (record: any) => { return ( @@ -236,21 +245,29 @@ export default function StockItemTestResultTable({ } } ]; - }, [itemId]); + }, [itemId, includeTestStation]); const [selectedTemplate, setSelectedTemplate] = useState( undefined ); - const resultFields: ApiFormFieldSet = useTestResultFields({ + const newResultFields: ApiFormFieldSet = useTestResultFields({ partId: partId, itemId: itemId, - templateId: selectedTemplate + templateId: selectedTemplate, + editing: false + }); + + const editResultFields: ApiFormFieldSet = useTestResultFields({ + partId: partId, + itemId: itemId, + templateId: selectedTemplate, + editing: true }); const newTestModal = useCreateApiFormModal({ url: ApiEndpoints.stock_test_result_list, - fields: useMemo(() => ({ ...resultFields }), [resultFields]), + fields: useMemo(() => ({ ...newResultFields }), [newResultFields]), initialData: { template: selectedTemplate, result: true @@ -265,7 +282,7 @@ export default function StockItemTestResultTable({ const editTestModal = useEditApiFormModal({ url: ApiEndpoints.stock_test_result_list, pk: selectedTest, - fields: useMemo(() => ({ ...resultFields }), [resultFields]), + fields: useMemo(() => ({ ...editResultFields }), [editResultFields]), title: t`Edit Test Result`, table: table, successMessage: t`Test result updated`