Skip to content

Commit b846a6d

Browse files
committed
Restored user load behavior, except using always_200=true
1 parent a83f8d3 commit b846a6d

File tree

6 files changed

+54
-52
lines changed

6 files changed

+54
-52
lines changed

src/app/models/accounts-model.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import settings from '~/helpers/window-settings';
22

3-
const accountsUrl = `${settings().accountHref}/api/user`;
3+
const accountsUrl = `${settings().accountHref}/api/user?always_200=true`;
44

55
function cached<T>(fn: () => T, invalidateFn: () => void) {
66
let valid = false;
@@ -20,6 +20,34 @@ function cached<T>(fn: () => T, invalidateFn: () => void) {
2020
return cachedFn;
2121
}
2222

23+
export type AccountsUserModel = {
24+
id: number;
25+
uuid: string;
26+
first_name: string;
27+
last_name: string;
28+
email: string;
29+
school_name: string;
30+
self_reported_role: string;
31+
self_reported_school: string;
32+
is_not_gdpr_location: boolean;
33+
salesforce_contact_id: string;
34+
is_instructor_verification_stale: boolean;
35+
faculty_status: string;
36+
contact_infos: {
37+
type: string;
38+
value: string;
39+
is_verified: boolean;
40+
is_guessed_preferred: boolean;
41+
}[];
42+
};
43+
44+
declare global {
45+
interface Window {
46+
_OX_USER_PROMISE?: Promise<AccountsUserModel>;
47+
dataLayer?: object[];
48+
}
49+
}
50+
2351
export default {
2452
load: cached(() => {
2553
// Uncomment ONLY to TEST
@@ -62,15 +90,17 @@ export default {
6290
// ]
6391
// });
6492

65-
window._OX_USER_PROMISE ||= fetch(accountsUrl, { credentials: 'include' }).then(
66-
(response) => {
67-
if (response.ok) {
68-
return response.json().catch((error) => {
69-
throw new Error(`Failed to parse user JSON: ${error.message}`);
70-
});
71-
} else {
72-
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
93+
// This code is shared with the CookieYes loader in index.html
94+
window._OX_USER_PROMISE ||= fetch(accountsUrl, {credentials: 'include'}).then(
95+
(response) => response.json().catch(
96+
(err: unknown) => {
97+
console.warn('No JSON in Accounts response');
98+
return {err};
7399
}
100+
),
101+
(err: unknown) => {
102+
console.warn('"Error fetching user info"');
103+
return {err};
74104
}
75105
);
76106

src/app/models/usermodel.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import {useState, useEffect} from 'react';
22
import isEqual from 'lodash/isEqual';
33
import throttle from 'lodash/throttle';
4-
import accountsModel from './accounts-model';
5-
import type {AccountsUserModel} from '../../typings/accounts';
4+
import accountsModel, { AccountsUserModel } from './accounts-model';
65

76
export type UserModelType = {
87
id: number;

src/index.html

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
<link rel="prefetch" href="<%= process.env.API_ORIGIN %>/apps/cms/api/books/?format=json">
2626
<link rel="prefetch" href="<%= process.env.API_ORIGIN %>/apps/cms/api/footer/?format=json">
2727
<script>
28-
window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', { credentials: 'include' }).then(
29-
(response) => {
30-
if (response.ok) {
31-
return response.json().catch((error) => {
32-
throw new Error(`Failed to parse user JSON: ${error.message}`);
33-
});
34-
} else {
35-
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
28+
// This code is shared with the accounts-model
29+
window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', {credentials: 'include'}).then(
30+
(response) => response.json().catch(
31+
(err) => {
32+
console.warn('No JSON in Accounts response');
33+
return {err};
34+
}
35+
),
36+
(err) => {
37+
console.warn('"Error fetching user info"');
38+
return {err};
3639
}
37-
}
3840
);
41+
3942
window._OX_USER_PROMISE.then((user) => {
4043
const updateUserConsentPreferences = (consentPreferences) => {
4144
if (user.id) {

src/typings/accounts.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/typings/global.d.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

test/src/models/accounts-model.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ describe('accounts-model', () => {
1111
accountsModel.load.invalidate();
1212
console.warn = saveWarn;
1313
});
14-
it('handles 403 response', async () => {
15-
mockFetch.mockResolvedValueOnce({
16-
status: 403
17-
});
14+
it('handles empty json response', async () => {
15+
mockFetch.mockResolvedValueOnce({json: () => Promise.resolve({})});
1816
expect(await accountsModel.load()).toEqual({});
1917
});
2018
it('catches rejection', async () => {

0 commit comments

Comments
 (0)