Skip to content

Commit

Permalink
fix: fix empty checkboxes when selecting a bulk action
Browse files Browse the repository at this point in the history
  • Loading branch information
dziraf committed Jan 24, 2024
1 parent 7b73063 commit 88a27af
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/backend/controllers/app-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class AppController {
if (!recordIds) {
throw new Error('you have to give "recordIds" in the request parameters')
}
const arrayOfIds = recordIds.split(',')
const arrayOfIds = recordIds?.split?.(',')
const href = this.h.bulkActionUrl({ resourceId, actionName, recordIds: arrayOfIds })
return layoutTemplate(this._admin, this.currentAdmin, href)
}
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/components/actions/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ const List: React.FC<ActionProps> = ({ resource, setTag }) => {
const search = new URLSearchParams(location.search)
if (search.get(REFRESH_KEY)) {
setSelectedRecords([])
} else {
const recordIds = search.get('recordIds')?.split?.(',') ?? []
setSelectedRecords(
records.filter((r) => recordIds.includes(r.id.toString())),
)
}
}, [location.search])
}, [location.search, records])

const handleActionPerformed = (): any => fetchData()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const SelectedRecords: React.FC<SelectedRecordsProps> = (props) => {
translateFunctions,
modalFunctions,
})

const contentTag = getResourceElementCss(resource.id, 'table-caption')

return (
<TableCaption data-css={contentTag}>
<Box flex py="sm" alignItems="center">
Expand Down
12 changes: 9 additions & 3 deletions src/frontend/components/routes/bulk-action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const BulkAction: React.FC = () => {

useEffect(() => {
fetchRecords()
}, [params.resourceId, params.actionName])
}, [params.resourceId, params.actionName, location.search])

if (!resource) {
return <NoResourceError resourceId={resourceId!} />
Expand Down Expand Up @@ -121,6 +121,7 @@ const BulkAction: React.FC = () => {
action={action as ActionJSON}
resource={resource}
records={records}
setTag={setTag}
/>
</DrawerPortal>
<Wrapper width={listAction.containerWidth}>
Expand All @@ -138,8 +139,13 @@ const BulkAction: React.FC = () => {

return (
<Wrapper width={action.containerWidth}>
{!action?.showInDrawer ? <ActionHeader resource={resource} action={action} /> : ''}
<BaseActionComponent action={action as ActionJSON} resource={resource} records={records} />
{!action?.showInDrawer ? <ActionHeader resource={resource} action={action} tag={tag} /> : ''}
<BaseActionComponent
action={action as ActionJSON}
resource={resource}
records={records}
setTag={setTag}
/>
</Wrapper>
)
}
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/interfaces/action/action-href.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const actionHref = (
return null
}

if (params.recordIds?.length) {
params.recordIds = [...new Set(params.recordIds)]
}

const hrefMap = {
record: (): string => h.recordActionUrl({
...params as RecordActionParams,
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/interfaces/action/build-action-click-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ActionJSON } from './action-json.interface.js'
import { buildActionCallApiTrigger } from './build-action-api-call-trigger.js'
import { TranslateFunctions } from '../../../utils/index.js'
import { ModalData, ModalFunctions } from '../modal.interface.js'
import { REFRESH_KEY } from '../../components/actions/utils/append-force-refresh.js'

export type BuildActionClickOptions = {
action: ActionJSON
Expand Down Expand Up @@ -71,9 +72,13 @@ export const buildActionClickHandler = (
const hrefParams = new URLSearchParams(url.search)
const currentParams = new URLSearchParams(action.showInDrawer ? location?.search ?? '' : '')
Object.entries(Object.fromEntries(currentParams.entries())).forEach(([key, value]) => {
hrefParams.append(key, value)
if (!hrefParams.has(key)) hrefParams.set(key, value)
})

if (location?.pathname === url.pathname) {
hrefParams.set(REFRESH_KEY, 'true')
}

navigate({
pathname: url.pathname,
search: hrefParams.toString(),
Expand Down

0 comments on commit 88a27af

Please sign in to comment.