Is it possible to update one of the records in a Table?

One of the actions in my table consists of making an API call and changing the status of a record from active to inactive. I want to update the record in the table to reflect the action performed. How can I do this? Here's a snippet of my current code:
export default function StoresList() {
const { tableProps, } = useTable<StoreList>({
syncWithLocation: true,

});

const toggleStoreStatus = ({ status, id }: StoreList) => {
// API Call
// How to update the record or refresh the table?
};

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column
dataIndex="status"
title={translate("fields.status.title", "Status")}
render={(value) => <RenderActiveStatus value={value} />}
/>
<Table.Column<StoreList>
width="240px"
title={translate("actions.actions", "Ações")}
dataIndex="actions"
render={(_, record) => (
<Button
onClick={() => toggleStoreStatus(record)}
size="small"
type="link"
>
{record.status === "approved"
? translate("buttons.deactivate", "Desativar")
: translate("buttons.activate", "Ativar")}
</Button>
)}
/>
</Table>
</List>
);
}
export default function StoresList() {
const { tableProps, } = useTable<StoreList>({
syncWithLocation: true,

});

const toggleStoreStatus = ({ status, id }: StoreList) => {
// API Call
// How to update the record or refresh the table?
};

return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column
dataIndex="status"
title={translate("fields.status.title", "Status")}
render={(value) => <RenderActiveStatus value={value} />}
/>
<Table.Column<StoreList>
width="240px"
title={translate("actions.actions", "Ações")}
dataIndex="actions"
render={(_, record) => (
<Button
onClick={() => toggleStoreStatus(record)}
size="small"
type="link"
>
{record.status === "approved"
? translate("buttons.deactivate", "Desativar")
: translate("buttons.activate", "Ativar")}
</Button>
)}
/>
</Table>
</List>
);
}
2 Replies
kapa.ai
kapa.ai5mo ago
Hi @Chrystian Ferreira. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To update a record in a table or refresh the table after performing an action, such as toggling a store's status from active to inactive, you can utilize the queryResult.refetch() method provided by useTable or useEditableTable hooks in refine. This method allows you to refetch the data, thus updating the table to reflect any changes made. Here's how you can integrate the refetch method into your existing code: 1. First, ensure you're capturing the queryResult from the useTable hook alongside tableProps. 2. Then, after performing your API call within the toggleStoreStatus function, call the queryResult.refetch() method to refresh the table data. Below is an adapted version of your code snippet incorporating these steps:
export default function StoresList() {
// Capture queryResult from useTable
const { tableProps, queryResult } = useTable<StoreList>({
syncWithLocation: true,
});

const toggleStoreStatus = async ({ status, id }: StoreList) => {
// API Call
await yourApiCallToUpdateStatus(id, status); // Replace with your actual API call

// Refresh the table data
queryResult.refetch();
};
return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column
dataIndex="status"
title={translate("fields.status.title", "Status")}
render={(value) => <RenderActiveStatus value={value} />}
/>
<Table.Column<StoreList>
width="240px"
title={translate("actions.actions", "Ações")}
dataIndex="actions"
render={(_, record) => (
<Button
onClick={() => toggleStoreStatus(record)}
size="small"
type="link"
>
{record.status === "approved"
? translate("buttons.deactivate", "Desativar")
: translate("buttons.activate", "Ativar")}
</Button>
)}
/>
</Table>
</List>
);
}
export default function StoresList() {
// Capture queryResult from useTable
const { tableProps, queryResult } = useTable<StoreList>({
syncWithLocation: true,
});

const toggleStoreStatus = async ({ status, id }: StoreList) => {
// API Call
await yourApiCallToUpdateStatus(id, status); // Replace with your actual API call

// Refresh the table data
queryResult.refetch();
};
return (
<List>
<Table {...tableProps} rowKey="id">
<Table.Column
dataIndex="status"
title={translate("fields.status.title", "Status")}
render={(value) => <RenderActiveStatus value={value} />}
/>
<Table.Column<StoreList>
width="240px"
title={translate("actions.actions", "Ações")}
dataIndex="actions"
render={(_, record) => (
<Button
onClick={() => toggleStoreStatus(record)}
size="small"
type="link"
>
{record.status === "approved"
? translate("buttons.deactivate", "Desativar")
: translate("buttons.activate", "Ativar")}
</Button>
)}
/>
</Table>
</List>
);
}
Please note, the yourApiCallToUpdateStatus function is a placeholder for your actual API call function. Replace it with the appropriate function call to update the store's status. For more information on using useTable and handling data refetching, you can refer to the refine documentation on useEditableTable or useTable (https://refine.dev/docs/api-references/hooks/table/useEditableTable/#api). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@1072217596058939412> kapa.ai is still learning and improving, please let us know how it did by reacting below