Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
279629a
[PP-3820] Add patron blocking rules editor to the Library Settings UI…
dbernstein Feb 27, 2026
5548ff4
Cosmetic improvements.
dbernstein Feb 28, 2026
5fa5431
Ensure that the error message appears next to the rule expression field.
dbernstein Mar 2, 2026
7b63f60
This update converts, to the extent that makes sense without trying t…
dbernstein Mar 3, 2026
dbf00db
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein Mar 6, 2026
c89add6
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein Mar 6, 2026
b166d77
Change the protocolName parameter type in supportsPatronBlockingRules…
dbernstein Mar 6, 2026
a44924b
Rename renderExtraExpandedLibrarySettings to renderExtraAssociatedLib…
dbernstein Mar 6, 2026
9d399e7
protocolHasLibrarySettings is now grouped with renderAdditionalConten…
dbernstein Mar 6, 2026
3fdf73a
use stable unique ID instead of array index as the key prop
dbernstein Mar 6, 2026
4bc8080
Disable"Add Rule" when any existing rule has an incomplete required f…
dbernstein Mar 6, 2026
cf8f1e8
Remove the rules.length > 0 condition entirely. The serverErrorMessa…
dbernstein Mar 6, 2026
b727a1a
Extract a RuleFormListItem function component
dbernstein Mar 6, 2026
a20be10
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein Mar 6, 2026
40c7d4f
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein Mar 6, 2026
b7963a1
Remove overly broad index signature from LibraryWithSettingsData.
dbernstein Mar 6, 2026
2bb3b86
Fix imports.
dbernstein Mar 6, 2026
fe4d9bb
Implement blocking rule validations on the server on blur
dbernstein Mar 6, 2026
adca26c
Ensure that save is disabled while there are client side patron block…
dbernstein Mar 8, 2026
2052416
This update ensures that when a new library is newly configured but n…
dbernstein Mar 8, 2026
bc8aa7d
Fix linting issue.
dbernstein Mar 8, 2026
055e842
Update src/components/PatronBlockingRulesEditor.tsx
dbernstein Mar 10, 2026
c07d2e7
Beefs up tests for PatronAuthServiceEditForm and PatronBlockingRulesE…
dbernstein Mar 10, 2026
efb9ef0
Fix bug where rule error messages were disappearing when a valid rule…
dbernstein Mar 10, 2026
d7101d3
Added note regarding possible race condition.
dbernstein Mar 10, 2026
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
10 changes: 9 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@
"react/no-find-dom-node": 0,
"jsx-a11y/label-has-for": 0,
"react/no-unescaped-entities": 0,
"@typescript-eslint/no-inferrable-types": 0
"@typescript-eslint/no-inferrable-types": 0,
"@typescript-eslint/no-unused-vars": [
"warn",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
]
},
"settings": {
"react": {
Expand Down
49 changes: 49 additions & 0 deletions src/api/patronBlockingRules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { PatronBlockingRule } from "../interfaces";

const VALIDATE_URL = "/admin/patron_auth_service_validate_patron_blocking_rule";

/**
* Validate a patron blocking rule expression against live ILS data on the server.
*
* The server loads the saved PatronAuthService by serviceId, makes a live
* authentication call using its configured test credentials, and evaluates
* the rule expression against the real patron data returned. Only parse/eval
* success or failure is reported — the boolean result is discarded.
*
* Returns null on success, or an error message string on failure.
*/
export const validatePatronBlockingRuleExpression = async (
serviceId: number | undefined,
rule: PatronBlockingRule,
csrfToken: string | undefined
): Promise<string | null> => {
const formData = new FormData();
if (serviceId !== undefined) {
formData.append("service_id", String(serviceId));
}
formData.append("rule", rule.rule);
formData.append("name", rule.name);

const headers: Record<string, string> = {};
if (csrfToken) {
headers["X-CSRF-Token"] = csrfToken;
}

const res = await fetch(VALIDATE_URL, {
method: "POST",
headers,
body: formData,
credentials: "same-origin",
});

if (res.ok) {
return null;
}

try {
const data = await res.json();
return data.detail || "Rule validation failed.";
} catch {
return "Rule validation failed.";
}
};
183 changes: 183 additions & 0 deletions src/components/PatronAuthServiceEditForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import * as React from "react";
import {
LibraryWithSettingsData,
PatronAuthServicesData,
ProtocolData,
} from "../interfaces";
import ServiceEditForm from "./ServiceEditForm";
import PatronBlockingRulesEditor, {
PatronBlockingRulesEditorHandle,
} from "./PatronBlockingRulesEditor";
import { supportsPatronBlockingRules } from "../utils/patronBlockingRules";

const NEW_LIBRARY_KEY = "__new__";

/** Extends ServiceEditForm with patron-blocking-rules support for protocols that
* support it. The editor is injected via the hook methods added
* to ServiceEditForm; editLibrary and addLibrary are overridden to collect the
* rules from the editor ref and persist them in library state. */
export default class PatronAuthServiceEditForm extends ServiceEditForm<
PatronAuthServicesData
> {
private newLibraryRulesRef = React.createRef<
PatronBlockingRulesEditorHandle
>();
private libraryRulesRefs = new Map<
string,
React.RefObject<PatronBlockingRulesEditorHandle>
>();

// Tracks whether any rule editor is currently blocking save due to pending
// or failed validation, or duplicate names. Updated via onValidationStateChange.
// Stored as an instance variable (not React state) to avoid TypeScript state
// type conflicts with the parent class; forceUpdate() triggers re-render.
private rulesBlockingSave: { [shortName: string]: boolean } = {};

private getOrCreateLibraryRef(
shortName: string
): React.RefObject<PatronBlockingRulesEditorHandle> {
if (!this.libraryRulesRefs.has(shortName)) {
this.libraryRulesRefs.set(
shortName,
React.createRef<PatronBlockingRulesEditorHandle>()
);
}
return this.libraryRulesRefs.get(shortName);
}

handleRulesValidationStateChange(
shortName: string,
isBlocking: boolean
): void {
if (this.rulesBlockingSave[shortName] !== isBlocking) {
this.rulesBlockingSave = {
...this.rulesBlockingSave,
[shortName]: isBlocking,
};
this.forceUpdate();
}
}

isLibrarySaveDisabled(library: LibraryWithSettingsData): boolean {
return !!this.rulesBlockingSave[library.short_name];
}

isAddLibraryDisabled(): boolean {
return !!this.rulesBlockingSave[NEW_LIBRARY_KEY];
}

protocolHasLibrarySettings(protocol: ProtocolData): boolean {
return (
super.protocolHasLibrarySettings(protocol) ||
supportsPatronBlockingRules(protocol && protocol.name)
);
}

renderExtraAssociatedLibrarySettings(
library: LibraryWithSettingsData,
protocol: ProtocolData,
disabled: boolean
): React.ReactNode {
if (!supportsPatronBlockingRules(protocol && protocol.name)) {
return null;
}
return (
<PatronBlockingRulesEditor
ref={this.getOrCreateLibraryRef(library.short_name)}
value={library.patron_blocking_rules || []}
disabled={disabled}
error={this.props.error}
csrfToken={this.props.additionalData?.csrfToken}
serviceId={
this.props.item?.id !== undefined
? Number(this.props.item.id)
: undefined
}
onValidationStateChange={(isBlocking) =>
this.handleRulesValidationStateChange(library.short_name, isBlocking)
}
/>
);
}

renderExtraNewLibrarySettings(
protocol: ProtocolData,
disabled: boolean
): React.ReactNode {
if (!supportsPatronBlockingRules(protocol && protocol.name)) {
return null;
}
return (
<PatronBlockingRulesEditor
ref={this.newLibraryRulesRef}
value={[]}
disabled={disabled}
error={this.props.error}
csrfToken={this.props.additionalData?.csrfToken}
serviceId={
this.props.item?.id !== undefined
? Number(this.props.item.id)
: undefined
}
onValidationStateChange={(isBlocking) =>
this.handleRulesValidationStateChange(NEW_LIBRARY_KEY, isBlocking)
}
/>
);
}

editLibrary(library: LibraryWithSettingsData, protocol: ProtocolData) {
const libraries = this.state.libraries.filter(
(stateLibrary) => stateLibrary.short_name !== library.short_name
);
const expandedLibraries = this.state.expandedLibraries.filter(
(shortName) => shortName !== library.short_name
);
const newLibrary: LibraryWithSettingsData = {
short_name: library.short_name,
};
for (const setting of this.protocolLibrarySettings(protocol)) {
const value = (this.refs[
`${library.short_name}_${setting.key}`
] as any).getValue();
if (value) {
((newLibrary as unknown) as Record<string, string>)[
setting.key
] = value;
}
}
if (supportsPatronBlockingRules(protocol && protocol.name)) {
const editorRef = this.libraryRulesRefs.get(library.short_name);
if (editorRef?.current) {
newLibrary.patron_blocking_rules = editorRef.current.getValue();
}
}
libraries.push(newLibrary);
this.setState(
Object.assign({}, this.state, { libraries, expandedLibraries })
);
}

addLibrary(protocol: ProtocolData) {
const name = this.state.selectedLibrary;
const newLibrary: LibraryWithSettingsData = { short_name: name };
for (const setting of this.protocolLibrarySettings(protocol)) {
const value = (this.refs[setting.key] as any).getValue();
if (value) {
((newLibrary as unknown) as Record<string, string>)[
setting.key
] = value;
}
(this.refs[setting.key] as any).clear();
}
if (supportsPatronBlockingRules(protocol && protocol.name)) {
if (this.newLibraryRulesRef.current) {
newLibrary.patron_blocking_rules = this.newLibraryRulesRef.current.getValue();
}
}
const libraries = this.state.libraries.concat(newLibrary);
this.setState(
Object.assign({}, this.state, { libraries, selectedLibrary: null })
);
}
}
5 changes: 3 additions & 2 deletions src/components/PatronAuthServices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import EditableConfigList, {
import { connect } from "react-redux";
import ActionCreator from "../actions";
import { PatronAuthServicesData, PatronAuthServiceData } from "../interfaces";
import ServiceEditForm from "./ServiceEditForm";
import PatronAuthServiceEditForm from "./PatronAuthServiceEditForm";
import NeighborhoodAnalyticsForm from "./NeighborhoodAnalyticsForm";

/** Right panel for patron authentication services on the system
Expand All @@ -18,7 +18,7 @@ export class PatronAuthServices extends EditableConfigList<
PatronAuthServicesData,
PatronAuthServiceData
> {
EditForm = ServiceEditForm;
EditForm = PatronAuthServiceEditForm;
ExtraFormSection = NeighborhoodAnalyticsForm;
extraFormKey = "neighborhood_mode";
listDataKey = "patron_auth_services";
Expand Down Expand Up @@ -70,6 +70,7 @@ function mapStateToProps(state, ownProps) {
// of the create/edit form.
return {
data: data,
additionalData: { csrfToken: ownProps.csrfToken },
responseBody:
state.editor.patronAuthServices &&
state.editor.patronAuthServices.successMessage,
Expand Down
Loading