Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/frontend/src/components/forms/ApiForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export function OptionsApiForm({
for (const [k, v] of Object.entries(_props.fields)) {
_props.fields[k] = constructField({
field: v,
definition: optionsQuery?.data?.[k]
definition: optionsQuery?.data?.[k],
field_name: k
});

// If the user has specified initial data, use that value here
Expand Down Expand Up @@ -459,9 +460,16 @@ export function ApiForm({
props.onFormSuccess(response.data, form);
}

if (props.follow && props.modelType && response.data?.pk) {
const keepOpen = form.getValues()?.keep_form_open ?? false;

if (
props.follow &&
props.modelType &&
response.data?.pk &&
!keepOpen
) {
// If we want to automatically follow the returned data
if (!!navigate) {
if (!!navigate && !keepOpen) {
navigate(getDetailUrl(props.modelType, response.data?.pk));
}
} else if (props.table) {
Expand Down
11 changes: 10 additions & 1 deletion src/frontend/src/forms/CommonForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useMemo, useState } from 'react';
import { ApiEndpoints } from '@lib/enums/ApiEndpoints';
import type { ModelType } from '@lib/enums/ModelType';
import { apiUrl } from '@lib/functions/Api';
import type { ApiFormFieldSet } from '@lib/types/Forms';
import type { ApiFormFieldSet, ApiFormFieldType } from '@lib/types/Forms';
import { t } from '@lingui/core/macro';
import type {
StatusCodeInterface,
Expand Down Expand Up @@ -209,3 +209,12 @@ export function useParameterFields({
};
}, [data, modelType, fieldType, choices, modelId]);
}

export const ExtraFormData: Record<string, ApiFormFieldType> = {
keep_form_open: {
label: 'Keep form open',
field_type: 'boolean',
exclude: true,
description: 'Keep form open after submitting'
}
};
7 changes: 5 additions & 2 deletions src/frontend/src/forms/StockForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ export function useDeleteStockItem(props: StockOperationProps) {
});
}

export function stockLocationFields(): ApiFormFieldSet {
export function stockLocationFields(create = false): ApiFormFieldSet {
const fields: ApiFormFieldSet = {
parent: {
description: t`Parent stock location`,
Expand All @@ -1408,7 +1408,10 @@ export function stockLocationFields(): ApiFormFieldSet {
custom_icon: {
field_type: 'icon'
},
location_type: {}
location_type: {},
keep_form_open: {
hidden: !create
}
};

return fields;
Expand Down
12 changes: 10 additions & 2 deletions src/frontend/src/functions/forms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { apiUrl } from '@lib/functions/Api';
import type { PathParams } from '@lib/types/Core';
import type { ApiFormFieldSet, ApiFormFieldType } from '@lib/types/Forms';
import { invalidResponse, permissionDenied } from './notifications';
import {ExtraFormData} from "../forms/CommonForms";

/**
* Construct an API url from the provided ApiFormProps object
Expand Down Expand Up @@ -133,13 +134,19 @@ export function mapFields(
*/
export function constructField({
field,
definition
definition,
field_name
}: {
field: ApiFormFieldType;
definition?: ApiFormFieldType;
field_name?: string;
}) {

const extra_definition = ExtraFormData[field_name ?? ''] ?? {};

const def = {
...definition,
...extra_definition,
...field
};

Expand All @@ -149,7 +156,8 @@ export function constructField({
for (const k of Object.keys(field.children ?? {})) {
def.children[k] = constructField({
field: field.children?.[k] ?? {},
definition: definition?.children?.[k] ?? {}
definition: definition?.children?.[k] ?? {},
field_name: k
});
}
break;
Expand Down
4 changes: 3 additions & 1 deletion src/frontend/src/hooks/UseForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export function useApiFormModal(props: ApiFormModalProps) {
}
],
onFormSuccess: (data, form) => {
if (props.checkClose?.(data, form) ?? true) {
const keepOpen = form.getValues()?.keep_form_open ?? false;

if (!keepOpen && (props.checkClose?.(data, form) ?? true)) {
modalClose.current();
}
props.onFormSuccess?.(data, form);
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/src/tables/stock/StockLocationTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export function StockLocationTable({ parentId }: Readonly<{ parentId?: any }>) {
const newLocation = useCreateApiFormModal({
url: ApiEndpoints.stock_location_list,
title: t`Add Stock Location`,
fields: stockLocationFields(),
fields: stockLocationFields(true),
focus: 'name',
initialData: {
parent: parentId
Expand Down