Skip to content

Commit

Permalink
Use accountsUrl, store function to clear _OX_USER_PROMISE as part of …
Browse files Browse the repository at this point in the history
…the cached load function
  • Loading branch information
Dantemss committed Feb 12, 2025
1 parent b57f8cd commit c8c1b4a
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/app/models/usermodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import throttle from 'lodash/throttle';
const settings = window.SETTINGS;
const accountsUrl = `${settings.accountHref}/api/user?always_200=true`;

function cached(fn) {
function cached(fn, invalidateFn) {
let valid = false;
let cachedResult = null;
const cachedFn = function () {
Expand All @@ -17,6 +17,7 @@ function cached(fn) {
};

cachedFn.invalidate = () => {
invalidateFn();
valid = false;
};
return cachedFn;
Expand Down Expand Up @@ -64,26 +65,26 @@ const accountsModel = {
// ]
// });

window._OX_USER_PROMISE ||= fetch('/accounts/api/user?always_200=true', { credentials: 'include' }).then(
(response) => {
if (response.status === 200) {
return response.json().catch((error) => {
throw new Error(`Failed to parse user JSON: ${error.message}`);
});
} else {
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
window._OX_USER_PROMISE ||= fetch(accountsUrl, { credentials: 'include' }).then(
(response) => {
if (response.status === 200) {
return response.json().catch((error) => {
throw new Error(`Failed to parse user JSON: ${error.message}`);
});
} else {
throw new Error(`Failed to load user: HTTP ${response.status} status code`);
}
}
}
);
return window._OX_USER_PROMISE.then((user) => {
if (window.dataLayer) {
window.dataLayer.push({
faculty_status: user.faculty_status
});
}
return user;
if (window.dataLayer) {
window.dataLayer.push({
faculty_status: user.faculty_status
});
}
return user;
});
})
}, () => { window._OX_USER_PROMISE = undefined; })
};

// eslint-disable-next-line complexity
Expand Down Expand Up @@ -157,7 +158,6 @@ const userModel = {

const throttledLoginCheck = throttle((setData) => {
accountsModel.load().then((oldUser) => {
window._OX_USER_PROMISE = undefined;
accountsModel.load.invalidate();
accountsModel.load().then((newUser) => {
if (!isEqual(oldUser, newUser)) {
Expand Down

0 comments on commit c8c1b4a

Please sign in to comment.