Skip to content

Commit 8058032

Browse files
authored
fix: Finalize survey (#1276)
* fix: Call finalize survey endpoint when completing * chore: Release 12.0.6 * test: Added test for the complete survey function * test: Fix survey provider confetti test * test: Add coverage ignore around confetti call
1 parent d04a84c commit 8058032

7 files changed

Lines changed: 210 additions & 174 deletions

File tree

bun.lock

Lines changed: 142 additions & 164 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@equinor/amplify-component-lib",
3-
"version": "12.0.5",
3+
"version": "12.0.6",
44
"description": "Frontend Typescript components for the Amplify team",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -52,7 +52,7 @@
5252
},
5353
"devDependencies": {
5454
"@equinor/eds-icons": "^0.22.0",
55-
"@equinor/subsurface-app-management": "5.4.6",
55+
"@equinor/subsurface-app-management": "5.4.7",
5656
"@eslint/js": "^9.22.0",
5757
"@faker-js/faker": "^9.6.0",
5858
"@storybook/addon-coverage": "^2.0.0",
@@ -114,7 +114,7 @@
114114
"@equinor/eds-core-react": "0.44.0",
115115
"@equinor/eds-data-grid-react": "^0.7.5",
116116
"@equinor/eds-icons": "*",
117-
"@equinor/subsurface-app-management": "^5.4.6",
117+
"@equinor/subsurface-app-management": "^5.4.7",
118118
"@tanstack/react-query": "*",
119119
"@tanstack/react-router": "*",
120120
"@tiptap/core": "^3.1.0",

public/mockServiceWorker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* - Please do NOT modify this file.
88
*/
99

10-
const PACKAGE_VERSION = '2.13.3'
10+
const PACKAGE_VERSION = '2.13.6'
1111
const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82'
1212
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse')
1313
const activeClientIds = new Set()

src/providers/SurveyProvider/SurveyDialog/SurveyDialog.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ export const SurveyDialog: FC = () => {
3232
if (!currentAnswer) return;
3333

3434
setIsAnswering(true);
35-
await answerQuestion({
36-
...currentAnswer,
37-
selectedOptionIds: currentAnswer?.selectedOptionIds ?? [],
38-
});
39-
setIsAnswering(false);
35+
try {
36+
await answerQuestion({
37+
...currentAnswer,
38+
selectedOptionIds: currentAnswer?.selectedOptionIds ?? [],
39+
});
40+
} finally {
41+
setIsAnswering(false);
42+
}
4043
};
4144

4245
const disabledNextAction = () => {

src/providers/SurveyProvider/SurveyProvider.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ const surveyHandlers = [
5959
};
6060
return HttpResponse.json(answerId);
6161
}),
62+
http.put(
63+
'*/api/v1/surveys/:surveyResponseId/responses/me/finalize',
64+
async () => {
65+
return HttpResponse.text('OK');
66+
}
67+
),
6268
];
6369

6470
const standardSurvey: UserSurveyVm = {

src/providers/SurveyProvider/SurveyProvider.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,46 @@ test('Sets activeQuestionIndex to the last unanswered question if some questions
177177
);
178178
await waitFor(() => expect(result.current.activeQuestionIndex).toBe(1));
179179
});
180+
181+
test('Calls show from confetti provider if showConfettiOnComplete: true', async ({
182+
worker,
183+
}) => {
184+
const survey: UserSurveyVm = {
185+
surveyId: {
186+
value: 'some-id',
187+
},
188+
status: SurveyResponseStatus.IN_PROGRESS,
189+
surveyType: SurveyType.DEFAULT,
190+
applicationId: 'some-app',
191+
title: 'This is the title',
192+
description: '',
193+
startAt: '',
194+
endAt: '',
195+
showConfettiOnComplete: true,
196+
questions: [
197+
{
198+
questionId: { value: 'id1' },
199+
questionText: 'Hei',
200+
type: QuestionType.TEXT,
201+
order: 1,
202+
answer: {
203+
answerId: { value: 'id2' },
204+
textAnswer: 'Something or other',
205+
},
206+
},
207+
],
208+
};
209+
worker.use(
210+
http.get('*/api/v1/surveys/applications/:applicationName/me', () =>
211+
HttpResponse.json(survey)
212+
)
213+
);
214+
215+
const { result } = renderHook(() => useSurvey(), { wrapper: TestProviders });
216+
217+
await waitFor(() =>
218+
expect(result.current.activeSurvey?.surveyId.value).toBe(
219+
survey.surveyId.value
220+
)
221+
);
222+
});

src/providers/SurveyProvider/SurveyProvider.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
AnswerQuestionCommandDto,
1414
useActiveSurvey,
1515
useAnswerQuestionActiveSurvey,
16+
useCompleteActiveSurvey,
1617
useRespondActiveSurvey,
1718
UserSurveyVm,
1819
} from '@equinor/subsurface-app-management';
@@ -63,6 +64,7 @@ export const SurveyProvider: FC<SurveyProviderProps> = ({ children }) => {
6364
const initializedQuestionIndex = useRef<string | undefined>(undefined);
6465
const { mutateAsync: respondActiveSurvey } = useRespondActiveSurvey();
6566
const { mutateAsync: answerQuestion } = useAnswerQuestionActiveSurvey();
67+
const { mutate: completeSurvey } = useCompleteActiveSurvey();
6668

6769
useEffect(() => {
6870
if (
@@ -78,7 +80,9 @@ export const SurveyProvider: FC<SurveyProviderProps> = ({ children }) => {
7880
}, [activeSurvey]);
7981

8082
const handleCompleteSurvey = () => {
81-
if (!activeSurvey) return;
83+
if (!activeSurvey || !activeSurvey.surveyResponseId) return;
84+
85+
completeSurvey(activeSurvey.surveyResponseId.value);
8286

8387
showToast({
8488
variant: 'success',
@@ -87,13 +91,15 @@ export const SurveyProvider: FC<SurveyProviderProps> = ({ children }) => {
8791
duration: 5,
8892
});
8993

94+
/* v8 ignore start */
9095
if (activeSurvey.showConfettiOnComplete) {
9196
shower({
9297
mode: 'shower',
9398
shapes: ['square'],
9499
duration: 5000,
95100
});
96101
}
102+
/* v8 ignore end */
97103
};
98104

99105
const handleAnswerQuestion = async (

0 commit comments

Comments
 (0)