[bugfix/frontend] Add nosubmit option to form fields + use it when instance custom CSS disabled (#2290)

This commit is contained in:
tobi 2023-10-24 10:28:59 +02:00 committed by tsmethurst
parent 9114c5ca1b
commit 8a23ce2b3e
6 changed files with 29 additions and 6 deletions

View file

@ -27,6 +27,12 @@ export default function getFormMutations(
const mutationData: Array<[string, any]> = []; const mutationData: Array<[string, any]> = [];
Object.values(form).forEach((field) => { Object.values(form).forEach((field) => {
if (field.nosubmit) {
// Completely ignore
// this field.
return;
}
if ("selectedValues" in field) { if ("selectedValues" in field) {
// FieldArrayInputHook. // FieldArrayInputHook.
const selected = field.selectedValues(); const selected = field.selectedValues();

View file

@ -87,10 +87,10 @@ export default function useFormSubmit(
if (e.nativeEvent.submitter) { if (e.nativeEvent.submitter) {
// We want the name of the element that was invoked to submit this form, // We want the name of the element that was invoked to submit this form,
// which will be something that extends HTMLElement, though we don't know // which will be something that extends HTMLElement, though we don't know
// what at this point. // what at this point. If it's an empty string, fall back to undefined.
// //
// See: https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent/submitter // See: https://developer.mozilla.org/en-US/docs/Web/API/SubmitEvent/submitter
action = (e.nativeEvent.submitter as Object as { name: string }).name; action = (e.nativeEvent.submitter as Object as { name: string }).name || undefined;
} else { } else {
// No submitter defined. Fall back // No submitter defined. Fall back
// to just use the FormSubmitEvent. // to just use the FormSubmitEvent.

View file

@ -39,7 +39,8 @@ export default function useTextInput(
dontReset = false, dontReset = false,
validator, validator,
showValidation = true, showValidation = true,
initValidation initValidation,
nosubmit = false,
}: HookOpts<string> }: HookOpts<string>
): TextFormInputHook { ): TextFormInputHook {
const [text, setText] = useState(initialValue); const [text, setText] = useState(initialValue);
@ -91,6 +92,7 @@ export default function useTextInput(
reset, reset,
name, name,
Name: "", // Will be set by inputHook function. Name: "", // Will be set by inputHook function.
nosubmit,
value: text, value: text,
ref: textRef, ref: textRef,
setter: setText, setter: setText,

View file

@ -39,6 +39,13 @@ export interface HookOpts<T = any> {
initialValue?: T, initialValue?: T,
defaultValue?: T, defaultValue?: T,
/**
* If true, don't submit this field as
* part of a mutation query's body.
*
* Useful for 'internal' form fields.
*/
nosubmit?: boolean,
dontReset?: boolean, dontReset?: boolean,
validator?, validator?,
showValidation?: boolean, showValidation?: boolean,
@ -88,6 +95,14 @@ export interface FormInputHook<T = any> {
* to have been changed from the default / initial value. * to have been changed from the default / initial value.
*/ */
hasChanged: () => boolean; hasChanged: () => boolean;
/**
* If true, don't submit this field as
* part of a mutation query's body.
*
* Useful for 'internal' form fields.
*/
nosubmit?: boolean;
} }
interface _withReset { interface _withReset {

View file

@ -90,7 +90,7 @@ function makeCacheMutation(action: Action): CacheMutation {
); );
} catch (e) { } catch (e) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(`rolling back pessimistic update of ${queryName}: ${e}`); console.error(`rolling back pessimistic update of ${queryName}: ${JSON.stringify(e)}`);
} }
} }
}; };

View file

@ -79,7 +79,7 @@ function UserProfileForm({ data: profile }) {
header: useFileInput("header", { withPreview: true }), header: useFileInput("header", { withPreview: true }),
displayName: useTextInput("display_name", { source: profile }), displayName: useTextInput("display_name", { source: profile }),
note: useTextInput("note", { source: profile, valueSelector: (p) => p.source?.note }), note: useTextInput("note", { source: profile, valueSelector: (p) => p.source?.note }),
customCSS: useTextInput("custom_css", { source: profile }), customCSS: useTextInput("custom_css", { source: profile, nosubmit: !instanceConfig.allowCustomCSS }),
bot: useBoolInput("bot", { source: profile }), bot: useBoolInput("bot", { source: profile }),
locked: useBoolInput("locked", { source: profile }), locked: useBoolInput("locked", { source: profile }),
discoverable: useBoolInput("discoverable", { source: profile}), discoverable: useBoolInput("discoverable", { source: profile}),
@ -190,7 +190,7 @@ function UserProfileForm({ data: profile }) {
</div> </div>
<TextArea <TextArea
field={form.customCSS} field={form.customCSS}
label="Custom CSS" label={`Custom CSS` + (!instanceConfig.allowCustomCSS ? ` (not enabled on this instance)` : ``)}
className="monospace" className="monospace"
rows={8} rows={8}
disabled={!instanceConfig.allowCustomCSS} disabled={!instanceConfig.allowCustomCSS}