-
-
Notifications
You must be signed in to change notification settings - Fork 3
Add listing attributes for display and filtering #1683
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1aa4642
Add listing attributes
stefan-burke eabc071
Save WIP
stefan-burke 594fdfb
Show listing attributes on order cards and multi-listing ticket pages
stefan-burke 84b966b
Address CodeRabbit follow-up: attributes on package rows and closed r…
stefan-burke 0629b69
Use #fp curried helpers and fix jscpd clone in attribute modules
stefan-burke a3ae1b5
Load attribute filter context before type narrowing; include child li…
stefan-burke 15c65d9
Extract setupFilteredPair helper to eliminate jscpd clone in filter t…
stefan-burke 6449211
Cover attributes on package member rows and package+standalone paths
stefan-burke b3ec26c
Render child listing attributes in add-on options; copy attributes on…
stefan-burke e08af2a
Add copyLinksTx to LinkTableSide and use it for attribute copy, fixin…
stefan-burke 70ef16a
Make attributesByListing required on buildPageListingRows to kill dea…
stefan-burke 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,346 @@ | ||
| /** | ||
| * Admin routes for listing attributes. | ||
| */ | ||
|
|
||
| /* jscpd:ignore-start */ | ||
| import { | ||
| createConfirmedHandlers, | ||
| createVerifiedFormRoute, | ||
| } from "#routes/admin/confirmation.ts"; | ||
| import { OWNER_FORM, ownerPage, requireOwnerOr } from "#routes/auth.ts"; | ||
| import { ownerGetById } from "#routes/entity.ts"; | ||
| import { | ||
| errorRedirect, | ||
| htmlResponse, | ||
| notFoundResponse, | ||
| redirect, | ||
| } from "#routes/response.ts"; | ||
| import { defineRoutes } from "#routes/router.ts"; | ||
| import { | ||
| type AuthedHandlerArgs, | ||
| createAuthedFormRoute, | ||
| createAuthedHandler, | ||
| } from "#shared/app-forms.ts"; | ||
| import { logActivity } from "#shared/db/activityLog.ts"; | ||
| import { | ||
| type AttributeOption, | ||
| type AttributeWithOptions, | ||
| assignNextAttributeSortOrder, | ||
| attributeOptionsTable, | ||
| attributesTable, | ||
| deleteAttribute, | ||
| deleteAttributeOption, | ||
| getAllAttributeOptionIds, | ||
| getAllAttributesWithOptions, | ||
| getAttributeId, | ||
| getAttributeIdsOrdered, | ||
| getAttributeWithOptions, | ||
| getNextAttributeOptionSortOrder, | ||
| pruneInvalidAttributeOptionIds, | ||
| setListingAttributeOptions, | ||
| swapAttributeOptionOrder, | ||
| swapAttributeOrder, | ||
| } from "#shared/db/attributes.ts"; | ||
| import { getFlash } from "#shared/flash-context.ts"; | ||
| import { defineForm } from "#shared/forms.tsx"; | ||
| import { | ||
| adminAttributeDeletePage, | ||
| adminAttributeOptionDeletePage, | ||
| adminAttributePage, | ||
| adminAttributesPage, | ||
| attributeNameFlat, | ||
| } from "#templates/admin/attributes.tsx"; | ||
| import { createListingChoicePost } from "./listing-choice-post.ts"; | ||
| /* jscpd:ignore-end */ | ||
|
|
||
| export const attributeNameForm = defineForm({ | ||
| fields: [ | ||
| { | ||
| label: "Attribute name", | ||
| name: "name", | ||
| placeholder: "e.g. Difficulty", | ||
| required: true, | ||
| type: "text", | ||
| }, | ||
| ] as const, | ||
| id: "attributeName", | ||
| }); | ||
|
|
||
| export const attributeOptionForm = defineForm({ | ||
| fields: [ | ||
| { | ||
| label: "Option text", | ||
| name: "text", | ||
| placeholder: "e.g. Beginner", | ||
| required: true, | ||
| type: "text", | ||
| }, | ||
| ] as const, | ||
| id: "attributeOption", | ||
| }); | ||
|
|
||
| const handleAttributesGet = ownerPage(async (session) => { | ||
| const flash = getFlash(); | ||
| return adminAttributesPage( | ||
| await getAllAttributesWithOptions(), | ||
| session, | ||
| flash.error, | ||
| ); | ||
| }); | ||
|
|
||
| const handleAttributesPost = createAuthedFormRoute({ | ||
| auth: OWNER_FORM, | ||
| form: attributeNameForm, | ||
| onInvalid: ({ error }) => errorRedirect("/admin/attributes", error), | ||
| onValid: async ({ values: { name } }) => { | ||
| const attribute = await attributesTable.insert({ name }); | ||
| await assignNextAttributeSortOrder(attribute.id); | ||
| await logActivity(`Attribute '${name}' created`); | ||
| return redirect( | ||
| `/admin/attributes/${attribute.id}`, | ||
| "Attribute created", | ||
| true, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const handleAttributeGet = ownerGetById( | ||
| getAttributeWithOptions, | ||
| (attribute, session) => | ||
| htmlResponse(adminAttributePage(attribute, session, getFlash().error)), | ||
| ); | ||
|
|
||
| type AttributeParams = { id: number }; | ||
| type AttributeOptionParams = { id: number; optionId: number }; | ||
| type AttributeOptionContext = { | ||
| attribute: AttributeWithOptions; | ||
| option: AttributeOption; | ||
| }; | ||
|
|
||
| const redirectToAttribute = (args: { | ||
| error: string; | ||
| params: AttributeParams; | ||
| }): Response => | ||
| errorRedirect(`/admin/attributes/${args.params.id}`, args.error); | ||
|
|
||
| const withAttribute = async ( | ||
| id: number, | ||
| handle: (attribute: AttributeWithOptions) => Promise<Response>, | ||
| ): Promise<Response> => { | ||
| const attribute = await getAttributeWithOptions(id); | ||
| return attribute ? handle(attribute) : notFoundResponse(); | ||
| }; | ||
|
|
||
| const logAttributeOptionActivity = ( | ||
| optionText: string, | ||
| action: string, | ||
| attribute: AttributeWithOptions, | ||
| ) => | ||
| logActivity( | ||
| `Attribute option '${optionText}' ${action} ${attributeNameFlat( | ||
| attribute.name, | ||
| )}`, | ||
| ); | ||
|
|
||
| const handleAttributeEdit = createAuthedFormRoute< | ||
| { name: string }, | ||
| AttributeParams | ||
| >({ | ||
| auth: OWNER_FORM, | ||
| form: attributeNameForm, | ||
| onInvalid: redirectToAttribute, | ||
| onValid: ({ params, values: { name } }) => | ||
| withAttribute(params.id, async () => { | ||
| await attributesTable.update(params.id, { name }); | ||
| await logActivity(`Attribute '${name}' updated`); | ||
| return redirect( | ||
| `/admin/attributes/${params.id}`, | ||
| "Attribute updated", | ||
| true, | ||
| ); | ||
| }), | ||
| }); | ||
|
|
||
| const handleAddOption = createAuthedFormRoute< | ||
| { text: string }, | ||
| AttributeParams | ||
| >({ | ||
| auth: OWNER_FORM, | ||
| form: attributeOptionForm, | ||
| onInvalid: redirectToAttribute, | ||
| onValid: ({ params, values: { text } }) => | ||
| withAttribute(params.id, async (attribute) => { | ||
| await attributeOptionsTable.insert({ | ||
| attributeId: params.id, | ||
| sortOrder: await getNextAttributeOptionSortOrder(params.id), | ||
| text, | ||
| }); | ||
| await logAttributeOptionActivity(text, "added to", attribute); | ||
| return redirect(`/admin/attributes/${params.id}`, "Option added", true); | ||
| }), | ||
| }); | ||
|
|
||
| const attributeDelete = createConfirmedHandlers<AttributeWithOptions>({ | ||
| identifier: (attribute) => attributeNameFlat(attribute.name), | ||
| identifierLabel: "Attribute name", | ||
| load: (id) => getAttributeWithOptions(id), | ||
| onConfirm: async (attribute) => { | ||
| await deleteAttribute(attribute.id); | ||
| await logActivity(`Attribute '${attribute.name}' deleted`); | ||
| }, | ||
| path: "/admin/attributes/:id/delete", | ||
| render: (attribute, session, error) => | ||
| adminAttributeDeletePage(attribute, session, error), | ||
| successMessage: "Attribute deleted", | ||
| successRedirect: "/admin/attributes", | ||
| }); | ||
|
|
||
| const loadAttributeOption = async ({ | ||
| id, | ||
| optionId, | ||
| }: AttributeOptionParams): Promise<AttributeOptionContext | null> => { | ||
| const attribute = await getAttributeWithOptions(id); | ||
| const option = attribute?.options.find((item) => item.id === optionId); | ||
| return attribute && option ? { attribute, option } : null; | ||
| }; | ||
|
|
||
| const optionRoute = | ||
| ( | ||
| handler: ( | ||
| attribute: AttributeWithOptions, | ||
| option: AttributeOption, | ||
| session: Parameters<typeof adminAttributeOptionDeletePage>[2], | ||
| ) => Response, | ||
| ) => | ||
| (request: Request, params: AttributeOptionParams): Promise<Response> => | ||
| requireOwnerOr(request, async (session) => { | ||
| const context = await loadAttributeOption(params); | ||
| if (!context) return notFoundResponse(); | ||
| return handler(context.attribute, context.option, session); | ||
| }); | ||
|
|
||
| const handleDeleteOptionGet = optionRoute((attribute, option, session) => | ||
| htmlResponse( | ||
| adminAttributeOptionDeletePage( | ||
| attribute, | ||
| option, | ||
| session, | ||
| getFlash().error, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const optionDeletePath = ({ id, optionId }: AttributeOptionParams): string => | ||
| `/admin/attributes/${id}/options/${optionId}/delete`; | ||
|
|
||
| const handleDeleteOptionPost = createVerifiedFormRoute< | ||
| AttributeOptionParams, | ||
| AttributeOptionContext | ||
| >({ | ||
| actionLabel: "deletion", | ||
| auth: OWNER_FORM, | ||
| identifier: ({ option }) => option.text, | ||
| identifierLabel: "Option text", | ||
| loadContext: loadAttributeOption, | ||
| mismatchRedirect: (_context, params) => optionDeletePath(params), | ||
| onConfirm: async ({ context: { attribute, option } }) => { | ||
| await deleteAttributeOption(option.id); | ||
| await logAttributeOptionActivity(option.text, "deleted from", attribute); | ||
| return redirect( | ||
| `/admin/attributes/${attribute.id}`, | ||
| "Option deleted", | ||
| true, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const editOptionPath = ({ id }: AttributeOptionParams): string => | ||
| `/admin/attributes/${id}`; | ||
|
|
||
| const handleEditOptionPost = createAuthedFormRoute< | ||
| { text: string }, | ||
| AttributeOptionParams, | ||
| AttributeOptionContext | ||
| >({ | ||
| auth: OWNER_FORM, | ||
| form: attributeOptionForm, | ||
| loadContext: loadAttributeOption, | ||
| onInvalid: ({ error, params }) => | ||
| errorRedirect(editOptionPath(params), error), | ||
| onValid: async ({ context: { attribute, option }, values: { text } }) => { | ||
| await attributeOptionsTable.update(option.id, { text }); | ||
| await logAttributeOptionActivity(text, "updated in", attribute); | ||
| return redirect( | ||
| `/admin/attributes/${attribute.id}`, | ||
| "Option updated", | ||
| true, | ||
| ); | ||
| }, | ||
| }); | ||
|
|
||
| const optionActionHandler = ( | ||
| handle: ( | ||
| args: AuthedHandlerArgs<AttributeOptionParams, AttributeOptionContext>, | ||
| ) => Response | Promise<Response>, | ||
| ) => | ||
| createAuthedHandler<AttributeOptionParams, AttributeOptionContext>({ | ||
| auth: OWNER_FORM, | ||
| handle, | ||
| loadContext: loadAttributeOption, | ||
| }); | ||
|
|
||
| const moveOptionHandler = (direction: -1 | 1) => | ||
| optionActionHandler(async ({ context: { attribute, option } }) => { | ||
| const index = attribute.options.findIndex((item) => item.id === option.id); | ||
| const neighbor = attribute.options[index + direction]; | ||
| if (neighbor) await swapAttributeOptionOrder(option.id, neighbor.id); | ||
| return redirect(`/admin/attributes/${attribute.id}`, "Option moved", true); | ||
| }); | ||
|
|
||
| const moveAttributeHandler = (direction: -1 | 1) => | ||
| createAuthedHandler<AttributeParams, number>({ | ||
| auth: OWNER_FORM, | ||
| handle: async ({ context: attributeId }) => { | ||
| const attributeIds = await getAttributeIdsOrdered(); | ||
| const index = attributeIds.indexOf(attributeId); | ||
| const neighborId = attributeIds[index + direction]; | ||
| if (neighborId) await swapAttributeOrder(attributeId, neighborId); | ||
| return redirect("/admin/attributes", "Attribute moved", true); | ||
| }, | ||
| loadContext: ({ id }) => getAttributeId(id), | ||
| }); | ||
|
|
||
| const handleListingAttributesPost = createListingChoicePost({ | ||
| fieldName: "option_ids", | ||
| label: "Attributes", | ||
| noun: "option", | ||
| readIds: async (form) => | ||
| pruneInvalidAttributeOptionIds( | ||
| await getAllAttributeOptionIds(), | ||
| form.getNumberArray("option_ids"), | ||
| ), | ||
| saveIds: setListingAttributeOptions, | ||
| tab: "attributes", | ||
| }); | ||
|
|
||
| export const attributesRoutes = { | ||
| ...attributeDelete.routes, | ||
| ...defineRoutes({ | ||
| "GET /admin/attributes": handleAttributesGet, | ||
| "GET /admin/attributes/:id": handleAttributeGet, | ||
| "GET /admin/attributes/:id/options/:optionId/delete": handleDeleteOptionGet, | ||
| "POST /admin/attributes": handleAttributesPost, | ||
| "POST /admin/attributes/:id/edit": handleAttributeEdit, | ||
| "POST /admin/attributes/:id/move-down": moveAttributeHandler(1), | ||
| "POST /admin/attributes/:id/move-up": moveAttributeHandler(-1), | ||
| "POST /admin/attributes/:id/options": handleAddOption, | ||
| "POST /admin/attributes/:id/options/:optionId/delete": | ||
| handleDeleteOptionPost, | ||
| "POST /admin/attributes/:id/options/:optionId/edit": handleEditOptionPost, | ||
| "POST /admin/attributes/:id/options/:optionId/move-down": | ||
| moveOptionHandler(1), | ||
| "POST /admin/attributes/:id/options/:optionId/move-up": | ||
| moveOptionHandler(-1), | ||
| "POST /admin/listing/:id/attributes": handleListingAttributesPost, | ||
| }), | ||
| }; | ||
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.