richie-education 2.33.1-dev14 → 2.33.1-dev19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/api/joanie.ts CHANGED
@@ -83,6 +83,7 @@ export const getRoutes = () => {
83
83
  update: `${baseUrl}/credit-cards/:id/`,
84
84
  delete: `${baseUrl}/credit-cards/:id/`,
85
85
  tokenize: `${baseUrl}/credit-cards/tokenize-card/`,
86
+ promote: `${baseUrl}/credit-cards/:id/promote/`,
86
87
  },
87
88
  addresses: {
88
89
  get: `${baseUrl}/addresses/:id/`,
@@ -242,6 +243,10 @@ const API = (): Joanie.API => {
242
243
  }).then(checkStatus),
243
244
  tokenize: async () =>
244
245
  fetchWithJWT(ROUTES.user.creditCards.tokenize, { method: 'POST' }).then(checkStatus),
246
+ promote: async (id) =>
247
+ fetchWithJWT(ROUTES.user.creditCards.promote.replace(':id', id), {
248
+ method: 'PATCH',
249
+ }).then(checkStatus),
245
250
  },
246
251
  addresses: {
247
252
  get: (id?: string) => {
@@ -25,24 +25,26 @@ type JWTPayload = {
25
25
  username: string;
26
26
  };
27
27
 
28
- /* All JWT tokens will expire the 12 Dec 2024 ! */
28
+ /* All JWT tokens will expire the 02 Feb 2026 ! */
29
29
  const JOANIE_DEV_DEMO_USER_JWT_TOKENS = {
30
- admin:
31
- 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzMzOTI4MjE0LCJpYXQiOjE3MDIzNjU1MjMsImp0aSI6IjRhMzQxZWVmMmVhOTRkNGFiMzQ5OThkOWE4ZDM5MTI0IiwiZW1haWwiOiJhZG1pbkBleGFtcGxlLmNvbSIsImxhbmd1YWdlIjoiZW4tdXMiLCJ1c2VybmFtZSI6ImFkbWluIiwiZnVsbF9uYW1lIjoiIn0.rT8nymp8f4T7tIIXO-M5-ahXBwxoDNVqtaZIrb_GHuk',
32
30
  user0:
33
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I6Ijg5ZDIyNDJjODRkODRiNThiZWVkYjg1NmU2MGNiM2FiIiwiZW1haWwiOiJkZXZlbG9wZXIrc2Vjb25kX3N0dWRlbnRfdXNlckBleGFtcGxlLmNvbSIsImxhbmd1YWdlIjoiZnItZnIiLCJ1c2VybmFtZSI6InNlY29uZF9zdHVkZW50X3VzZXIiLCJmdWxsX25hbWUiOiJcdTAwYzl0dWRpYW50IDAwMiJ9.p5p4Ku0w8mHortWW9TYHTJgORF9wnfCpq-6pvBRjU0Y',
46
+ admin:
47
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzcwMjIzOTY5LCJpYXQiOjE3Mzg2ODc5NjksImp0aSI6Ijk4M2UzNmI5MTUzODQ2Mjg4ZGMxNWNjOTAwNDgwMDA4IiwiZW1haWwiOiJhZG1pbkBleGFtcGxlLmNvbSIsImxhbmd1YWdlIjoiZW4tdXMiLCJ1c2VybmFtZSI6ImFkbWluIiwiZnVsbF9uYW1lIjoiIn0.VuSqfh4l0vtIDSdkEgCNyciiOhlFlMAsf5u5snm2Avw',
46
48
  };
47
49
 
48
50
  export type DevDemoUser = keyof typeof JOANIE_DEV_DEMO_USER_JWT_TOKENS;
@@ -175,4 +175,87 @@ describe('useCreditCards', () => {
175
175
  expect(result.current.states.isPending).toBe(false);
176
176
  expect(result.current.states.tokenizing).toBe(false);
177
177
  });
178
+
179
+ it('promotes a credit card', async () => {
180
+ const responseDeferred = new Deferred();
181
+ fetchMock.patch(
182
+ 'https://joanie.endpoint/api/v1.0/credit-cards/1/promote/',
183
+ responseDeferred.promise,
184
+ );
185
+ const { result } = renderHook(() => useCreditCards(undefined, { enabled: false }), {
186
+ wrapper: Wrapper,
187
+ });
188
+
189
+ await waitFor(() => {
190
+ expect(result.current).not.toBeNull();
191
+ });
192
+
193
+ await act(async () => {
194
+ result.current.methods.promote('1');
195
+ });
196
+
197
+ await waitFor(() => {
198
+ expect(result.current.states.updating).toBe(true);
199
+ });
200
+
201
+ expect(result.current.states.deleting).toBe(false);
202
+ expect(result.current.states.tokenizing).toBe(false);
203
+ expect(result.current.states.fetching).toBe(false);
204
+ expect(result.current.states.isFetched).toBe(true);
205
+ expect(result.current.states.isPending).toBe(true);
206
+ expect(result.current.states.error).toBe(undefined);
207
+
208
+ await act(async () => {
209
+ responseDeferred.resolve({});
210
+ });
211
+
212
+ expect(result.current.states.updating).toBe(false);
213
+ expect(result.current.states.isPending).toBe(false);
214
+ expect(result.current.states.error).toBe(undefined);
215
+ });
216
+
217
+ it('manages error during credit card promotion', async () => {
218
+ fetchMock.patch(
219
+ 'https://joanie.endpoint/api/v1.0/credit-cards/1/promote/',
220
+ HttpStatusCode.INTERNAL_SERVER_ERROR,
221
+ );
222
+ const { result } = renderHook(() => useCreditCards(undefined, { enabled: true }), {
223
+ wrapper: Wrapper,
224
+ });
225
+
226
+ await waitFor(() => {
227
+ expect(result.current).not.toBeNull();
228
+ });
229
+
230
+ await act(async () => {
231
+ await expect(result.current.methods.promote('1')).rejects.toThrow('Internal Server Error');
232
+ });
233
+
234
+ expect(result.current.states.error).toBe('Cannot set the credit card as default');
235
+ expect(result.current.states.isPending).toBe(false);
236
+ expect(result.current.states.updating).toBe(false);
237
+ });
238
+
239
+ it('has a specific error when credit card deletion fails because it is used', async () => {
240
+ const creditCard = CreditCardFactory({
241
+ id: '1',
242
+ last_numbers: '1337',
243
+ }).one();
244
+ fetchMock.delete('https://joanie.endpoint/api/v1.0/credit-cards/1/', HttpStatusCode.CONFLICT);
245
+ const { result } = renderHook(() => useCreditCards(undefined, { enabled: true }), {
246
+ wrapper: Wrapper,
247
+ });
248
+
249
+ await waitFor(() => {
250
+ expect(result.current).not.toBeNull();
251
+ });
252
+
253
+ await act(async () => {
254
+ result.current.methods.delete(creditCard);
255
+ });
256
+
257
+ expect(result.current.states.error).toBe(
258
+ 'Cannot delete the credit card •••• •••• •••• 1337 because it is used to pay at least one of your order.',
259
+ );
260
+ });
178
261
  });
@@ -1,8 +1,10 @@
1
1
  import { defineMessages, useIntl } from 'react-intl';
2
2
  import { useMutation, useQueryClient } from '@tanstack/react-query';
3
+ import { MutateOptions } from '@tanstack/query-core';
3
4
  import { API, CreditCard } from 'types/Joanie';
4
5
  import { useJoanieApi } from 'contexts/JoanieApiContext';
5
6
  import { useSessionMutation } from 'utils/react-query/useSessionMutation';
7
+ import { HttpError, HttpStatusCode } from 'utils/errors/HttpError';
6
8
  import {
7
9
  QueryOptions,
8
10
  ResourcesQuery,
@@ -27,6 +29,13 @@ const messages = defineMessages({
27
29
  description: 'Error message shown to the user when credit card deletion request fails.',
28
30
  defaultMessage: 'An error occurred while deleting the credit card. Please retry later.',
29
31
  },
32
+ errorCannotDelete: {
33
+ id: 'hooks.useCreditCards.errorCannotDelete',
34
+ description:
35
+ 'Error message shown to the user when trying to delete a credit card that is used to pay at least order.',
36
+ defaultMessage:
37
+ 'Cannot delete the credit card •••• •••• •••• {last_numbers} because it is used to pay at least one of your order.',
38
+ },
30
39
  errorTokenize: {
31
40
  id: 'hooks.useCreditCards.errorTokenize',
32
41
  description: 'Error message shown to the user when credit card tokenize request fails.',
@@ -37,6 +46,11 @@ const messages = defineMessages({
37
46
  description: 'Error message shown to the user when no credit cards matches.',
38
47
  defaultMessage: 'Cannot find the credit card',
39
48
  },
49
+ errorPromote: {
50
+ id: 'hooks.useCreditCards.errorPromote',
51
+ description: 'Error message shown to the user when promoting a credit card fails.',
52
+ defaultMessage: 'Cannot set the credit card as default',
53
+ },
40
54
  });
41
55
 
42
56
  const useCreditCardResources =
@@ -55,17 +69,55 @@ const useCreditCardResources =
55
69
  },
56
70
  onError: () => custom.methods.setError(intl.formatMessage(messages.errorTokenize)),
57
71
  });
72
+ const promoteHandler = mutation({
73
+ mutationFn: api.promote,
74
+ onSuccess: async () => {
75
+ custom.methods.setError(undefined);
76
+ custom.methods.invalidate();
77
+ props.onMutationSuccess?.(queryClient);
78
+ },
79
+ onError: () => custom.methods.setError(intl.formatMessage(messages.errorPromote)),
80
+ });
81
+
82
+ /**
83
+ * Override the default delete mutation to handle error more specifically.
84
+ * If the error is a 409, it means the credit card is used to pay at least one order
85
+ * and the user should be informed about that.
86
+ */
87
+ const deleteMutateAsync = async (creditCard: CreditCard, options?: MutateOptions) => {
88
+ return custom.methods.delete(creditCard.id, {
89
+ ...options,
90
+ onError: (error: HttpError, variables, context) => {
91
+ if (error.code === HttpStatusCode.CONFLICT) {
92
+ custom.methods.setError(
93
+ intl.formatMessage(messages.errorCannotDelete, {
94
+ last_numbers: creditCard.last_numbers,
95
+ }),
96
+ );
97
+ } else {
98
+ custom.methods.setError(intl.formatMessage(messages.errorDelete));
99
+ }
100
+ options?.onError?.(error, variables, context);
101
+ },
102
+ });
103
+ };
58
104
 
59
105
  return {
60
106
  ...custom,
61
107
  methods: {
62
108
  ...custom.methods,
109
+ delete: deleteMutateAsync,
63
110
  tokenize: tokenizeHandler.mutateAsync,
111
+ promote: promoteHandler.mutateAsync,
64
112
  },
65
113
  states: {
66
114
  ...custom.states,
67
- isPending: [tokenizeHandler, custom.states].some((value) => value?.isPending),
115
+ isPending: [tokenizeHandler, promoteHandler, custom.states].some(
116
+ (value) => value?.isPending,
117
+ ),
118
+ updating: custom.states.updating || promoteHandler.isPending,
68
119
  tokenizing: tokenizeHandler.isPending,
120
+ promoting: promoteHandler.isPending,
69
121
  },
70
122
  };
71
123
  };
@@ -5,11 +5,6 @@ import { CreditCard } from 'types/Joanie';
5
5
  import { confirm } from 'utils/indirection/window';
6
6
 
7
7
  const messages = defineMessages({
8
- errorCannotRemoveMain: {
9
- id: 'hooks.useCreditCardsManagement.errorCannotRemoveMain',
10
- description: 'Error shown if a user tries to delete a main credit card',
11
- defaultMessage: 'Cannot remove main credit card.',
12
- },
13
8
  deletionConfirmation: {
14
9
  id: 'hooks.useCreditCardsManagement.deletionConfirmation',
15
10
  description: 'Confirmation message shown to the user when he wants to delete a credit card',
@@ -23,15 +18,11 @@ export const useCreditCardsManagement = () => {
23
18
  const creditCards = useCreditCards();
24
19
 
25
20
  const safeDelete = (creditCard: CreditCard, options?: MutateOptions) => {
26
- if (creditCard.is_main) {
27
- creditCards.methods.setError(intl.formatMessage(messages.errorCannotRemoveMain));
28
- return;
29
- }
30
21
  const sure = confirm(intl.formatMessage(messages.deletionConfirmation));
31
22
  if (!sure) {
32
23
  return;
33
24
  }
34
- creditCards.methods.delete(creditCard.id, options);
25
+ creditCards.methods.delete(creditCard, options);
35
26
  };
36
27
 
37
28
  return {
@@ -76,11 +76,9 @@ export const DashboardCreditCardBox = ({ creditCard, promote, edit, remove }: Pr
76
76
  <FormattedMessage {...messages.edit} />
77
77
  </Button>
78
78
  </div>
79
- {!creditCard.is_main && (
80
- <Button color="primary" onClick={() => remove(creditCard)}>
81
- <FormattedMessage {...messages.delete} />
82
- </Button>
83
- )}
79
+ <Button color="primary" onClick={() => remove(creditCard)}>
80
+ <FormattedMessage {...messages.delete} />
81
+ </Button>
84
82
  </>
85
83
  }
86
84
  >
@@ -2,12 +2,23 @@
2
2
  display: flex;
3
3
  flex-direction: column;
4
4
  gap: 2rem;
5
+ position: relative;
5
6
 
6
7
  &__empty {
7
8
  text-align: center;
8
9
  color: r-theme-val(dashboard-credit-cards, empty-color);
9
10
  margin: 0;
10
11
  }
12
+
13
+ &__loading-overlay {
14
+ position: absolute;
15
+ pointer-events: none;
16
+ inset: 0;
17
+ display: grid;
18
+ place-items: center;
19
+ background-color: rgba(255, 255, 255, 0.8);
20
+ z-index: 9999;
21
+ }
11
22
  }
12
23
 
13
24
  .dashboard-credit-card {
@@ -34,9 +45,6 @@
34
45
  }
35
46
  }
36
47
 
37
- &__data {
38
- }
39
-
40
48
  &__brand {
41
49
  font-weight: bold;
42
50
 
@@ -191,7 +191,7 @@ describe('<DashboardCreditCardsManagement/>', () => {
191
191
  });
192
192
 
193
193
  it('deletes a credit card', async () => {
194
- const creditCards = CreditCardFactory().many(5);
194
+ const creditCards = CreditCardFactory().many(2);
195
195
  fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', creditCards);
196
196
  render(<DashboardPreferences />, {
197
197
  queryOptions: { client: createTestQueryClient({ user: richieUser }) },
@@ -231,6 +231,46 @@ describe('<DashboardCreditCardsManagement/>', () => {
231
231
  expect(screen.queryByText(creditCard.title!)).toBeNull();
232
232
  });
233
233
 
234
+ it('deletes a main credit card', async () => {
235
+ const creditCard = CreditCardFactory({ is_main: true }).one();
236
+ fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', [creditCard]);
237
+ render(<DashboardPreferences />, {
238
+ queryOptions: { client: createTestQueryClient({ user: richieUser }) },
239
+ });
240
+ // No error is shown.
241
+ expect(screen.queryByText('An error occurred', { exact: false })).toBeNull();
242
+
243
+ // Find the delete button of the credit card.
244
+ await screen.findByText(creditCard.title!);
245
+ const creditCardContainer = screen.getByTestId('dashboard-credit-card__' + creditCard.id);
246
+ const deleteButton = getByRole(creditCardContainer, 'button', {
247
+ name: 'Delete',
248
+ });
249
+
250
+ // Mock delete route and the refresh route to returns `creditCards` without the first one.
251
+ const deleteUrl = 'https://joanie.endpoint/api/v1.0/credit-cards/' + creditCard.id + '/';
252
+ fetchMock.delete(deleteUrl, []);
253
+ fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', [], {
254
+ overwriteRoutes: true,
255
+ });
256
+
257
+ // Clicking on the delete button calls delete API route.
258
+ expect(fetchMock.called(deleteUrl)).toBe(false);
259
+ // Clicking on the delete button calls window.confirm function.
260
+ expect(confirm).not.toHaveBeenCalled();
261
+ await act(async () => {
262
+ fireEvent.click(deleteButton);
263
+ });
264
+ expect(fetchMock.called(deleteUrl)).toBe(true);
265
+ expect(confirm).toHaveBeenCalled();
266
+
267
+ // No error is shown.
268
+ expect(screen.queryByText('An error occurred', { exact: false })).toBeNull();
269
+
270
+ // The address does not appear anymore in the list.
271
+ expect(screen.queryByText(creditCard.title!)).toBeNull();
272
+ });
273
+
234
274
  it('promotes a credit card', async () => {
235
275
  const creditCards = CreditCardFactory().many(5);
236
276
  fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', creditCards);
@@ -251,8 +291,9 @@ describe('<DashboardCreditCardsManagement/>', () => {
251
291
  expect(queryByText(creditCardContainer, 'Default credit card')).toBeNull();
252
292
 
253
293
  // Mock the update url and the refresh URL to return the first credit card as main.
254
- const updateUrl = 'https://joanie.endpoint/api/v1.0/credit-cards/' + creditCard.id + '/';
255
- fetchMock.put(updateUrl, []);
294
+ const promoteUrl =
295
+ 'https://joanie.endpoint/api/v1.0/credit-cards/' + creditCard.id + '/promote/';
296
+ fetchMock.patch(promoteUrl, {});
256
297
  fetchMock.get(
257
298
  'https://joanie.endpoint/api/v1.0/credit-cards/',
258
299
  [{ ...creditCard, is_main: true }, ...creditCards.splice(1)],
@@ -260,11 +301,11 @@ describe('<DashboardCreditCardsManagement/>', () => {
260
301
  );
261
302
 
262
303
  // Clicking on the promote button calls the update API route.
263
- expect(fetchMock.called(updateUrl)).toBe(false);
304
+ expect(fetchMock.called(promoteUrl)).toBe(false);
264
305
  await act(async () => {
265
306
  fireEvent.click(promoteButton);
266
307
  });
267
- expect(fetchMock.called(updateUrl)).toBe(true);
308
+ expect(fetchMock.called(promoteUrl)).toBe(true);
268
309
 
269
310
  // Assert that "Default credit card" is displayed on the credit card's box.
270
311
  creditCardContainer = screen.getByTestId('dashboard-credit-card__' + creditCard.id);
@@ -298,26 +339,6 @@ describe('<DashboardCreditCardsManagement/>', () => {
298
339
  );
299
340
  });
300
341
 
301
- it('cannot delete a main credit card', async () => {
302
- const creditCards = CreditCardFactory().many(5);
303
- const mainCreditCard = creditCards[3];
304
- mainCreditCard.is_main = true;
305
- fetchMock.get('https://joanie.endpoint/api/v1.0/credit-cards/', creditCards);
306
- render(<DashboardPreferences />, {
307
- queryOptions: { client: createTestQueryClient({ user: richieUser }) },
308
- });
309
-
310
- // The delete button is not displayed.
311
- const creditCardContainer = await screen.findByTestId(
312
- 'dashboard-credit-card__' + mainCreditCard.id,
313
- );
314
- expect(
315
- queryByRole(creditCardContainer, 'button', {
316
- name: 'Delete',
317
- }),
318
- ).toBeNull();
319
- });
320
-
321
342
  it('cannot promote a main credit card', async () => {
322
343
  const creditCards = CreditCardFactory().many(5);
323
344
  const mainCreditCard = creditCards[3];
@@ -13,11 +13,6 @@ const messages = defineMessages({
13
13
  description: 'Title of the dashboard credit cards management block',
14
14
  defaultMessage: 'Credit cards',
15
15
  },
16
- errorCannotPromoteMain: {
17
- id: 'components.DashboardCreditCardsManagement.errorCannotPromoteMain',
18
- description: 'Error shown if a user tries to promote a main credit card',
19
- defaultMessage: 'Cannot promote main credit card.',
20
- },
21
16
  emptyList: {
22
17
  id: 'components.DashboardCreditCardsManagement.emptyList',
23
18
  description: 'Empty placeholder of the dashboard credit cards management block',
@@ -33,7 +28,7 @@ export const DashboardCreditCardsManagement = ({ onClickEdit }: Props) => {
33
28
  const intl = useIntl();
34
29
  const {
35
30
  states: { error, isPending },
36
- methods: { setError, update, safeDelete },
31
+ methods: { promote, safeDelete },
37
32
  ...creditCards
38
33
  } = useCreditCardsManagement();
39
34
 
@@ -54,40 +49,29 @@ export const DashboardCreditCardsManagement = ({ onClickEdit }: Props) => {
54
49
  return creditCards.items.sort(sortByMainFirstThenByTitle);
55
50
  }, [creditCards.items]);
56
51
 
57
- const promote = (creditCard: CreditCard) => {
58
- if (creditCard.is_main) {
59
- setError(intl.formatMessage(messages.errorCannotPromoteMain));
60
- return;
61
- }
62
- update({
63
- ...creditCard,
64
- is_main: true,
65
- });
66
- };
67
-
68
52
  return (
69
53
  <DashboardCard header={<FormattedMessage {...messages.header} />}>
70
- <div className="dashboard-credit-cards">
71
- {isPending && <Spinner />}
72
- {!isPending && (
73
- <>
74
- {error && <Banner message={error} type={BannerType.ERROR} rounded />}
75
- {!error && creditCardsList.length === 0 && (
76
- <p className="dashboard-credit-cards__empty">
77
- <FormattedMessage {...messages.emptyList} />
78
- </p>
79
- )}
80
- {creditCardsList.map((creditCard) => (
81
- <DashboardCreditCardBox
82
- creditCard={creditCard}
83
- key={creditCard.id}
84
- edit={(_creditCard) => onClickEdit?.(_creditCard)}
85
- promote={promote}
86
- remove={safeDelete}
87
- />
88
- ))}
89
- </>
54
+ <div className="dashboard-credit-cards" aria-busy={isPending}>
55
+ {isPending && (
56
+ <div className="dashboard-credit-cards__loading-overlay">
57
+ <Spinner />
58
+ </div>
59
+ )}
60
+ {error && <Banner message={error} type={BannerType.ERROR} rounded />}
61
+ {!error && creditCardsList.length === 0 && (
62
+ <p className="dashboard-credit-cards__empty">
63
+ <FormattedMessage {...messages.emptyList} />
64
+ </p>
90
65
  )}
66
+ {creditCardsList.map((creditCard) => (
67
+ <DashboardCreditCardBox
68
+ key={creditCard.id}
69
+ creditCard={creditCard}
70
+ edit={(instance) => onClickEdit?.(instance)}
71
+ promote={({ id }) => promote(id)}
72
+ remove={safeDelete}
73
+ />
74
+ ))}
91
75
  </div>
92
76
  </DashboardCard>
93
77
  );
@@ -591,6 +591,7 @@ interface APIUser {
591
591
  get(): Promise<CreditCard[]>;
592
592
  update(payload: CreditCard): Promise<CreditCard>;
593
593
  tokenize(): Promise<Payment>;
594
+ promote(id: CreditCard['id']): Promise<void>;
594
595
  };
595
596
  orders: {
596
597
  cancel(id: Order['id']): Promise<void>;
@@ -30,5 +30,6 @@ export enum HttpStatusCode {
30
30
  BAD_REQUEST = 400,
31
31
  FORBIDDEN = 403,
32
32
  NOT_FOUND = 404,
33
+ CONFLICT = 409,
33
34
  INTERNAL_SERVER_ERROR = 500,
34
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "richie-education",
3
- "version": "2.33.1-dev14",
3
+ "version": "2.33.1-dev19",
4
4
  "description": "A CMS to build learning portals for Open Education",
5
5
  "main": "sandbox/manage.py",
6
6
  "scripts": {