-
Notifications
You must be signed in to change notification settings - Fork 6
[PP-3820] Add patron blocking rules editor to the Library Settings UI… #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dbernstein
wants to merge
25
commits into
main
Choose a base branch
from
feature/PP-3820-add-patron-blocks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 5548ff4
Cosmetic improvements.
dbernstein 5fa5431
Ensure that the error message appears next to the rule expression field.
dbernstein 7b63f60
This update converts, to the extent that makes sense without trying t…
dbernstein dbf00db
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein c89add6
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein b166d77
Change the protocolName parameter type in supportsPatronBlockingRules…
dbernstein a44924b
Rename renderExtraExpandedLibrarySettings to renderExtraAssociatedLib…
dbernstein 9d399e7
protocolHasLibrarySettings is now grouped with renderAdditionalConten…
dbernstein 3fdf73a
use stable unique ID instead of array index as the key prop
dbernstein 4bc8080
Disable"Add Rule" when any existing rule has an incomplete required f…
dbernstein cf8f1e8
Remove the rules.length > 0 condition entirely. The serverErrorMessa…
dbernstein b727a1a
Extract a RuleFormListItem function component
dbernstein a20be10
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein 40c7d4f
Update src/components/PatronAuthServiceEditForm.tsx
dbernstein b7963a1
Remove overly broad index signature from LibraryWithSettingsData.
dbernstein 2bb3b86
Fix imports.
dbernstein fe4d9bb
Implement blocking rule validations on the server on blur
dbernstein adca26c
Ensure that save is disabled while there are client side patron block…
dbernstein 2052416
This update ensures that when a new library is newly configured but n…
dbernstein bc8aa7d
Fix linting issue.
dbernstein 055e842
Update src/components/PatronBlockingRulesEditor.tsx
dbernstein c07d2e7
Beefs up tests for PatronAuthServiceEditForm and PatronBlockingRulesE…
dbernstein efb9ef0
Fix bug where rule error messages were disappearing when a valid rule…
dbernstein d7101d3
Added note regarding possible race condition.
dbernstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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."; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.