Skip to content

Commit ed6f5c5

Browse files
authored
Restore loading state for server action in custom button (#3759)
* restore loading state for server action in custom button * try/catch * add loading check to test case * add delay to action
1 parent 72c8ad1 commit ed6f5c5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/layout/CustomButton/CustomButtonComponent.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ export const CustomButtonComponent = ({ baseComponentId }: PropsFromGenericCompo
202202
const acquireLock = FD.useLocking(id);
203203
const isAuthorized = useIsAuthorized();
204204
const { handleClientActions } = useHandleClientActions();
205-
const { mutate: handleServerAction, error } = useMutation({
205+
const { mutateAsync: handleServerAction, error } = useMutation({
206206
mutationFn: useHandleServerActionMutationFn(acquireLock),
207207
});
208208

@@ -263,7 +263,11 @@ export const CustomButtonComponent = ({ baseComponentId }: PropsFromGenericCompo
263263
if (isClientAction(action)) {
264264
await handleClientActions([action]);
265265
} else if (isServerAction(action)) {
266-
handleServerAction({ action, buttonId: id });
266+
try {
267+
await handleServerAction({ action, buttonId: id });
268+
} catch {
269+
// Error is handled elsewhere
270+
}
267271
}
268272
}
269273
});

test/e2e/integration/frontend-test/custom-button.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@ const appFrontend = new AppFrontend();
44

55
describe('Custom Button', () => {
66
it('Should perform action and update the frontend with the updated datamodel', () => {
7+
cy.intercept({ url: '**/instances/**/actions*' }, (req) => {
8+
req.reply((res) => {
9+
res.setDelay(500);
10+
});
11+
}).as('actionWithDelay');
12+
713
cy.goto('changename');
814

915
cy.findByRole('button', { name: 'Fyll ut skjema' }).click();
16+
cy.findByRole('button', { name: 'Fyll ut skjema' }).should('be.disabled'); // Disabled while loading
17+
cy.wait('@actionWithDelay');
18+
cy.findByRole('button', { name: 'Fyll ut skjema' }).should('not.be.disabled'); // Enabled when finished
1019
cy.findByRole('textbox', { name: 'Denne oppdateres av custom button' }).should(
1120
'have.value',
1221
'Her kommer det data fra backend',

0 commit comments

Comments
 (0)