Skip to content

Commit

Permalink
[chore/frogend] Restructure form data default values / update from Qu…
Browse files Browse the repository at this point in the history
…ery data (#1422)

* eslint: set console use to error to catch debug littering in CI

* remove debug logging

* some form field restructuring, fixes submitted updates not being reflected

* more form field restructuring

* remove debug logger

* simplify field updates

* fix react state set during render when submitting import file

* className instead of class

* show Select hints again
  • Loading branch information
f0x52 authored Feb 6, 2023
1 parent 0a98743 commit 47daddc
Show file tree
Hide file tree
Showing 19 changed files with 153 additions and 86 deletions.
3 changes: 2 additions & 1 deletion web/source/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module.exports = {
"extends": ["@joepie91/eslint-config/react"],
"plugins": ["license-header"],
"rules": {
"license-header/header": ["error", __dirname + "/.license-header.js"]
"license-header/header": ["error", __dirname + "/.license-header.js"],
"no-console": 'error'
}
};
1 change: 1 addition & 0 deletions web/source/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@reduxjs/toolkit": "^1.8.6",
"ariakit": "^2.0.0-next.41",
"bluebird": "^3.7.2",
"get-by-dot": "^1.0.2",
"is-valid-domain": "^0.1.6",
"js-file-download": "^0.4.12",
"langs": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion web/source/settings/admin/emoji/local/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function EmojiDetailRoute() {
function EmojiDetailForm({ data: emoji }) {
const form = {
id: useValue("id", emoji.id),
category: useComboBoxInput("category", { defaultValue: emoji.category }),
category: useComboBoxInput("category", { source: emoji }),
image: useFileInput("image", {
withPreview: true,
maxSize: 50 * 1024 // TODO: get from instance api
Expand Down
29 changes: 21 additions & 8 deletions web/source/settings/admin/federation/detail.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"use strict";

const React = require("react");
const { useRoute, Redirect } = require("wouter");
const { useRoute, Redirect, useLocation } = require("wouter");

const query = require("../../lib/query");

Expand Down Expand Up @@ -69,12 +69,12 @@ module.exports = function InstanceDetail({ baseUrl }) {
<div>
<h1 className="text-cutoff"><BackButton to={baseUrl} /> Federation settings for: <span title={domain}>{domain}</span></h1>
{infoContent}
<DomainBlockForm defaultDomain={domain} block={existingBlock} />
<DomainBlockForm defaultDomain={domain} block={existingBlock} baseUrl={baseUrl} />
</div>
);
};

function DomainBlockForm({ defaultDomain, block = {} }) {
function DomainBlockForm({ defaultDomain, block = {}, baseUrl }) {
const isExistingBlock = block.domain != undefined;

const disabledForm = isExistingBlock
Expand All @@ -85,18 +85,31 @@ function DomainBlockForm({ defaultDomain, block = {} }) {
: {};

const form = {
domain: useTextInput("domain", { defaultValue: block.domain ?? defaultDomain }),
obfuscate: useBoolInput("obfuscate", { defaultValue: block.obfuscate }),
commentPrivate: useTextInput("private_comment", { defaultValue: block.private_comment }),
commentPublic: useTextInput("public_comment", { defaultValue: block.public_comment })
domain: useTextInput("domain", { source: block, defaultValue: defaultDomain }),
obfuscate: useBoolInput("obfuscate", { source: block }),
commentPrivate: useTextInput("private_comment", { source: block }),
commentPublic: useTextInput("public_comment", { source: block })
};

const [submitForm, addResult] = useFormSubmit(form, query.useAddInstanceBlockMutation(), { changedOnly: false });

const [removeBlock, removeResult] = query.useRemoveInstanceBlockMutation({ fixedCacheKey: block.id });

const [location, setLocation] = useLocation();

function verifyUrlThenSubmit(e) {
// Adding a new block happens on /settings/admin/federation/domain.com
// but if domain input changes, that doesn't match anymore and causes issues later on
// so, before submitting the form, silently change url, then submit
let correctUrl = `${baseUrl}/${form.domain.value}`;
if (location != correctUrl) {
setLocation(correctUrl);
}
return submitForm(e);
}

return (
<form onSubmit={submitForm}>
<form onSubmit={verifyUrlThenSubmit}>
<TextInput
field={form.domain}
label="Domain"
Expand Down
10 changes: 2 additions & 8 deletions web/source/settings/admin/federation/import-export/form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ const ExportFormatTable = require("./export-format-table");
module.exports = function ImportExportForm({ form, submitParse, parseResult }) {
const [submitExport, exportResult] = useFormSubmit(form, query.useExportDomainListMutation());

const [updateFromFile, setUpdateFromFile] = React.useState(false);

function fileChanged(e) {
const reader = new FileReader();
reader.onload = function (read) {
form.domains.setter(read.target.result);
setUpdateFromFile(true);
form.domains.value = read.target.result;
submitParse();
};
reader.readAsText(e.target.files[0]);
}
Expand All @@ -54,10 +52,6 @@ module.exports = function ImportExportForm({ form, submitParse, parseResult }) {
/* eslint-disable-next-line react-hooks/exhaustive-deps */
}, [exportResult]);

if (updateFromFile) {
setUpdateFromFile(false);
submitParse();
}
return (
<>
<h1>Import / Export suspended domains</h1>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = function ImportExport() {
exportType: useTextInput("exportType", { defaultValue: "plain", dontReset: true })
};

const [submitParse, parseResult] = useFormSubmit(form, query.useProcessDomainListMutation());
const [submitParse, parseResult] = useFormSubmit(form, query.useProcessDomainListMutation(), { changedOnly: false });

const [_location, setLocation] = useLocation();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const UpdateableEntry = React.memo(
return (
<>
<span className="text-cutoff">{entry.domain}</span>
<i class="fa fa-long-arrow-right" aria-hidden="true"></i>
<i className="fa fa-long-arrow-right" aria-hidden="true"></i>
<span>{entry.suggest}</span>
<a role="button" onClick={() =>
updateEntry(entry.key, { domain: entry.suggest, suggest: null })
Expand Down
17 changes: 10 additions & 7 deletions web/source/settings/admin/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ module.exports = function AdminSettings() {

function AdminSettingsForm({ data: instance }) {
const form = {
title: useTextInput("title", { defaultValue: instance.title }),
title: useTextInput("title", {
source: instance,
validator: (val) => val.length <= 40 ? "" : "Instance title must be 40 characters or less"
}),
thumbnail: useFileInput("thumbnail", { withPreview: true }),
thumbnailDesc: useTextInput("thumbnail_description", { defaultValue: instance.thumbnail_description }),
shortDesc: useTextInput("short_description", { defaultValue: instance.short_description }),
description: useTextInput("description", { defaultValue: instance.description }),
contactUser: useTextInput("contact_username", { defaultValue: instance.contact_account?.username }),
contactEmail: useTextInput("contact_email", { defaultValue: instance.email }),
terms: useTextInput("terms", { defaultValue: instance.terms })
thumbnailDesc: useTextInput("thumbnail_description", { source: instance }),
shortDesc: useTextInput("short_description", { source: instance }),
description: useTextInput("description", { source: instance }),
contactUser: useTextInput("contact_username", { source: instance, valueSelector: (s) => s.contact_account?.username }),
contactEmail: useTextInput("contact_email", { source: instance, valueSelector: (s) => s.email }),
terms: useTextInput("terms", { source: instance })
};

const [submitForm, result] = useFormSubmit(form, query.useUpdateInstanceMutation());
Expand Down
5 changes: 2 additions & 3 deletions web/source/settings/components/form/inputs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const React = require("react");

function TextInput({ label, field, ...inputProps }) {
const { onChange, value, ref } = field;
console.log(field.name, field.valid, field.value);

return (
<div className={`form-field text${field.valid ? "" : " invalid"}`}>
Expand Down Expand Up @@ -93,13 +92,13 @@ function Checkbox({ label, field, ...inputProps }) {
);
}

function Select({ label, field, options, ...inputProps }) {
function Select({ label, field, options, children, ...inputProps }) {
const { onChange, value, ref } = field;

return (
<div className="form-field select">
<label>
{label}
{label} {children}
<select
{...{ onChange, value, ref }}
{...inputProps}
Expand Down
10 changes: 6 additions & 4 deletions web/source/settings/lib/form/bool.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@

const React = require("react");

module.exports = function useBoolInput({ name, Name }, { defaultValue = false } = {}) {
const [value, setValue] = React.useState(defaultValue);
const _default = false;
module.exports = function useBoolInput({ name, Name }, { initialValue = _default }) {
const [value, setValue] = React.useState(initialValue);

function onChange(e) {
setValue(e.target.checked);
}

function reset() {
setValue(defaultValue);
setValue(initialValue);
}

// Array / Object hybrid, for easier access in different contexts
Expand All @@ -45,6 +46,7 @@ module.exports = function useBoolInput({ name, Name }, { defaultValue = false }
reset,
value,
setter: setValue,
hasChanged: () => value != defaultValue
hasChanged: () => value != initialValue,
_default
});
};
12 changes: 6 additions & 6 deletions web/source/settings/lib/form/check-list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ const { reducer, actions } = createSlice({
}
});

function initialState({ entries, uniqueKey, defaultValue }) {
function initialState({ entries, uniqueKey, initialValue }) {
const selectedEntries = new Set();
return {
entries: syncpipe(entries, [
(_) => _.map((entry) => {
let key = entry[uniqueKey];
let checked = entry.checked ?? defaultValue;
let checked = entry.checked ?? initialValue;

if (checked) {
selectedEntries.add(key);
Expand All @@ -110,9 +110,9 @@ function initialState({ entries, uniqueKey, defaultValue }) {
};
}

module.exports = function useCheckListInput({ name }, { entries, uniqueKey = "key", defaultValue = false }) {
module.exports = function useCheckListInput({ name }, { entries, uniqueKey = "key", initialValue = false }) {
const [state, dispatch] = React.useReducer(reducer, null,
() => initialState({ entries, uniqueKey, defaultValue }) // initial state
() => initialState({ entries, uniqueKey, initialValue }) // initial state
);

const toggleAllRef = React.useRef(null);
Expand All @@ -132,8 +132,8 @@ module.exports = function useCheckListInput({ name }, { entries, uniqueKey = "ke
}, [state.selectedEntries]);

const reset = React.useCallback(
() => dispatch(actions.updateAll(defaultValue)),
[defaultValue]
() => dispatch(actions.updateAll(initialValue)),
[initialValue]
);

const onChange = React.useCallback(
Expand Down
13 changes: 8 additions & 5 deletions web/source/settings/lib/form/combo-box.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ const React = require("react");

const { useComboboxState } = require("ariakit/combobox");

module.exports = function useComboBoxInput({ name, Name }, { defaultValue } = {}) {
const _default = "";
module.exports = function useComboBoxInput({ name, Name }, { initialValue = _default }) {
const [isNew, setIsNew] = React.useState(false);

const state = useComboboxState({
defaultValue,
defaultValue: initialValue,
gutter: 0,
sameWidth: true
});

function reset() {
state.setValue("");
state.setValue(initialValue);
}

return Object.assign([
Expand All @@ -48,9 +49,11 @@ module.exports = function useComboBoxInput({ name, Name }, { defaultValue } = {}
name,
state,
value: state.value,
hasChanged: () => state.value != defaultValue,
setter: (val) => state.setValue(val),
hasChanged: () => state.value != initialValue,
isNew,
setIsNew,
reset
reset,
_default
});
};
49 changes: 43 additions & 6 deletions web/source/settings/lib/form/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,52 @@

"use strict";

const React = require("react");
const getByDot = require("get-by-dot").default;

function capitalizeFirst(str) {
return str.slice(0, 1).toUpperCase() + str.slice(1);
return str.slice(0, 1).toUpperCase + str.slice(1);
}

function selectorByKey(key) {
if (key.includes("[")) {
// get-by-dot does not support 'nested[deeper][key]' notation, convert to 'nested.deeper.key'
key = key
.replace(/\[/g, ".") // nested.deeper].key]
.replace(/\]/g, ""); // nested.deeper.key
}

return function selector(obj) {
if (obj == undefined) {
return undefined;
} else {
return getByDot(obj, key);
}
};
}

function makeHook(func) {
return (name, ...args) => func({
name,
Name: capitalizeFirst(name)
}, ...args);
function makeHook(hookFunction) {
return function (name, opts = {}) {
// for dynamically generating attributes like 'setName'
const Name = React.useMemo(() => capitalizeFirst(name), [name]);

const selector = React.useMemo(() => selectorByKey(name), [name]);
const valueSelector = opts.valueSelector ?? selector;

opts.initialValue = React.useMemo(() => {
if (opts.source == undefined) {
return opts.defaultValue;
} else {
return valueSelector(opts.source) ?? opts.defaultValue;
}
}, [opts.source, opts.defaultValue, valueSelector]);

const hook = hookFunction({ name, Name }, opts);

return Object.assign(hook, {
name, Name,
});
};
}

module.exports = {
Expand Down
10 changes: 6 additions & 4 deletions web/source/settings/lib/form/radio.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@

const React = require("react");

module.exports = function useRadioInput({ name, Name }, { defaultValue, options } = {}) {
const [value, setValue] = React.useState(defaultValue);
const _default = "";
module.exports = function useRadioInput({ name, Name }, { initialValue = _default, options }) {
const [value, setValue] = React.useState(initialValue);

function onChange(e) {
setValue(e.target.value);
}

function reset() {
setValue(defaultValue);
setValue(initialValue);
}

// Array / Object hybrid, for easier access in different contexts
Expand All @@ -46,6 +47,7 @@ module.exports = function useRadioInput({ name, Name }, { defaultValue, options
value,
setter: setValue,
options,
hasChanged: () => value != defaultValue
hasChanged: () => value != initialValue,
_default
});
};
Loading

0 comments on commit 47daddc

Please sign in to comment.