@@ -41,8 +41,10 @@ import { EmptyState } from '../../../../admin-ui/components/EmptyState'
4141import { GraphQLErrorNotice } from '../../../../admin-ui/components/GraphQLErrorNotice'
4242import { PageContainer } from '../../../../admin-ui/components/PageContainer'
4343import { useList } from '../../../../admin-ui/context'
44+ import { serializeActionData } from '../../../../admin-ui/utils/actionData'
4445import { useSearchFilter } from '../../../../fields/types/relationship/views/useFilter'
4546import type { ActionMeta , FieldMeta , JSONValue , ListMeta } from '../../../../types'
47+ import { deserializeItemToValue } from '../../../../admin-ui/utils'
4648import { FilterAdd } from './FilterAdd'
4749import { PaginationControls , snapValueToClosest } from './PaginationControls'
4850import { Tag } from './Tag'
@@ -332,7 +334,23 @@ function ListPage({ listKey }: ListPageProps) {
332334 label : f . label ,
333335 isDisabled : f . listView . fieldMode === 'read' ,
334336 } ) )
335- const shownFields = columns . map ( fieldKey => list . fields [ fieldKey ] ) . filter ( Boolean )
337+ const shownFields = useMemo (
338+ ( ) => columns . map ( fieldKey => list . fields [ fieldKey ] ) . filter ( Boolean ) ,
339+ [ columns , list . fields ]
340+ )
341+ const queriedFields = useMemo ( ( ) => {
342+ return [
343+ ...new Set ( [
344+ ...columns ,
345+ ...list . actions
346+ . filter ( action => action . listView . actionMode === 'enabled' )
347+ . flatMap ( action => action . graphql . fields ) ,
348+ ] ) ,
349+ ]
350+ . map ( fieldKey => list . fields [ fieldKey ] )
351+ . filter ( Boolean )
352+ } , [ columns , list ] )
353+
336354 const where = useMemo (
337355 ( ) =>
338356 filters . map ( filter => {
@@ -350,7 +368,7 @@ function ListPage({ listKey }: ListPageProps) {
350368 items : Record < string , unknown > [ ] | null
351369 count : number | null
352370 } > => {
353- const selectedGqlFields = shownFields
371+ const selectedGqlFields = queriedFields
354372 . filter ( field => field . key !== 'id' ) // id is always included
355373 . map ( field => field . controller . graphqlSelection )
356374 . join ( '\n' )
@@ -375,7 +393,7 @@ function ListPage({ listKey }: ListPageProps) {
375393 count: ${ list . graphql . names . listQueryCountName } (where: $where)
376394 }
377395 `
378- } , [ list , shownFields ] ) ,
396+ } , [ list , queriedFields ] ) ,
379397 {
380398 fetchPolicy : 'cache-and-network' ,
381399 errorPolicy : 'all' ,
@@ -431,16 +449,16 @@ function ListPage({ listKey }: ListPageProps) {
431449 setSort ( defaultSort )
432450 }
433451
434- const actions = list . actions . filter ( action => action . listView . actionMode === 'enabled' )
435452 const actionsForList = list . hideDelete
436- ? actions
453+ ? list . actions
437454 : [
438- ...actions ,
455+ ...list . actions ,
439456 {
440457 key : 'delete' ,
441458 label : 'Delete' ,
442459 icon : 'trash2Icon' ,
443460 graphql : {
461+ fields : [ ] ,
444462 names : {
445463 one : list . graphql . names . deleteMutationName ,
446464 many : list . graphql . names . deleteManyMutationName ,
@@ -661,6 +679,7 @@ function ListPage({ listKey }: ListPageProps) {
661679 return (
662680 < ActionItemsDialog
663681 itemIds = { selectedItemIds }
682+ items = { data ?. items ?? [ ] }
664683 action = { action }
665684 list = { list }
666685 onSuccess = { remaining => {
@@ -788,27 +807,47 @@ type ActionErrorResult = {
788807function ActionItemsDialog ( {
789808 list,
790809 itemIds,
810+ items,
791811 onSuccess,
792812 onErrors,
793813 action,
794814} : {
795815 list : ListMeta
796816 itemIds : string [ ]
817+ items : Record < string , unknown > [ ]
797818 onSuccess : ( remaining : Set < string > ) => void
798819 onErrors : ( result : ActionErrorResult ) => void
799820 action : ActionMeta
800821} ) {
801- const [ actionOnItems ] = useMutation < { results ?: ( { id : string } | null ) [ ] } > (
802- gql `mutation($where: [${ list . graphql . names . whereUniqueInputName } !]!) {
822+ const actionMutation =
823+ action . key === 'delete'
824+ ? gql `mutation($where: [${ list . graphql . names . whereUniqueInputName } !]!) {
803825 results: ${ action . graphql . names . many } (where: $where) {
804826 id
805827 }
806- }` ,
807- {
808- variables : { where : itemIds . map ( id => ( { id } ) ) } ,
809- errorPolicy : 'all' ,
810- }
811- )
828+ }`
829+ : gql `mutation($data: [${ action . graphql . names . one [ 0 ] . toUpperCase ( ) } ${ action . graphql . names . one . slice ( 1 ) } Args!]!) {
830+ results: ${ action . graphql . names . many } (data: $data) {
831+ id
832+ }
833+ }`
834+ const [ actionOnItems ] = useMutation < { results ?: ( { id : string } | null ) [ ] } > ( actionMutation , {
835+ variables :
836+ action . key === 'delete'
837+ ? { where : itemIds . map ( id => ( { id } ) ) }
838+ : {
839+ data : itemIds . flatMap ( id => {
840+ const row = items . find ( item => String ( item . id ) === id )
841+ if ( ! row ) {
842+ return [ ]
843+ }
844+ const deserialized = deserializeItemToValue ( list . fields , row )
845+ const data = serializeActionData ( list , action , deserialized , deserialized )
846+ return Object . keys ( data ) . length ? { where : { id } , data } : { where : { id } }
847+ } ) ,
848+ } ,
849+ errorPolicy : 'all' ,
850+ } )
812851 const { messages : m } = action
813852
814853 async function onTryAction ( ) {
0 commit comments