Skip to content

Commit c8c1b4a

Browse files
committed
Use accountsUrl, store function to clear _OX_USER_PROMISE as part of the cached load function
1 parent b57f8cd commit c8c1b4a

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/app/models/usermodel.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import throttle from 'lodash/throttle';
55
const settings = window.SETTINGS;
66
const accountsUrl = `${settings.accountHref}/api/user?always_200=true`;
77

8-
function cached(fn) {
8+
function cached(fn, invalidateFn) {
99
let valid = false;
1010
let cachedResult = null;
1111
const cachedFn = function () {
@@ -17,6 +17,7 @@ function cached(fn) {
1717
};
1818

1919
cachedFn.invalidate = () => {
20+
invalidateFn();
2021
valid = false;
2122
};
2223
return cachedFn;
@@ -64,26 +65,26 @@ const accountsModel = {
6465
// ]
6566
// });
6667

67-
window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', { credentials: 'include' }).then(
68-
(response) => {
69-
if (response.status === 200) {
70-
return response.json().catch((error) => {
71-
throw new Error(`Failed to parse user JSON: ${error.message}`);
72-
});
73-
} else {
74-
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
68+
window._OX_USER_PROMISE ||= fetch(accountsUrl, { credentials: 'include' }).then(
69+
(response) => {
70+
if (response.status === 200) {
71+
return response.json().catch((error) => {
72+
throw new Error(`Failed to parse user JSON: ${error.message}`);
73+
});
74+
} else {
75+
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
76+
}
7577
}
76-
}
7778
);
7879
return window._OX_USER_PROMISE.then((user) => {
79-
if (window.dataLayer) {
80-
window.dataLayer.push({
81-
faculty_status: user.faculty_status
82-
});
83-
}
84-
return user;
80+
if (window.dataLayer) {
81+
window.dataLayer.push({
82+
faculty_status: user.faculty_status
83+
});
84+
}
85+
return user;
8586
});
86-
})
87+
}, () => { window._OX_USER_PROMISE = undefined; })
8788
};
8889

8990
// eslint-disable-next-line complexity
@@ -157,7 +158,6 @@ const userModel = {
157158

158159
const throttledLoginCheck = throttle((setData) => {
159160
accountsModel.load().then((oldUser) => {
160-
window._OX_USER_PROMISE = undefined;
161161
accountsModel.load.invalidate();
162162
accountsModel.load().then((newUser) => {
163163
if (!isEqual(oldUser, newUser)) {

0 commit comments

Comments
 (0)