Prevent editing of attachment file on test result after upload (#8032)

This commit is contained in:
Oliver 2024-08-29 14:34:34 +10:00 committed by GitHub
parent 3911694342
commit 368d59ee55
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 8 deletions

View File

@ -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,

View File

@ -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 (
<Group justify="space-between">
@ -224,6 +232,7 @@ export default function StockItemTestResultTable({
accessor: 'finished_datetime',
sortable: true,
title: t`Finished`,
hidden: !includeTestStation,
render: (record: any) => {
return (
<Group justify="space-between">
@ -236,21 +245,29 @@ export default function StockItemTestResultTable({
}
}
];
}, [itemId]);
}, [itemId, includeTestStation]);
const [selectedTemplate, setSelectedTemplate] = useState<number | undefined>(
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`