|
| 1 | +import { API_URL } from "@/resources.js"; |
1 | 2 | import { createStore } from "vuex"; |
2 | 3 | // import { createLogger } from "vuex"; |
3 | 4 | // import { set } from 'vue' |
@@ -30,6 +31,8 @@ export default createStore({ |
30 | 31 | currentUserDisplayName: null, |
31 | 32 | currentUserID: null, |
32 | 33 | currentUserInfoLoading: false, |
| 34 | + currentUserInfoLoaded: false, |
| 35 | + currentUserInfoPromise: null, |
33 | 36 | serverInfo: null, |
34 | 37 | blocksInfos: {}, |
35 | 38 | currentUserIsUnverified: false, |
@@ -345,6 +348,9 @@ export default createStore({ |
345 | 348 | }, |
346 | 349 | setCurrentUserInfoLoading(state, isLoading) { |
347 | 350 | state.currentUserInfoLoading = isLoading; |
| 351 | + if (!isLoading && !state.currentUserID) { |
| 352 | + state.currentUserInfoLoaded = true; |
| 353 | + } |
348 | 354 | }, |
349 | 355 | setItemGraphIsLoading(state, isLoading) { |
350 | 356 | state.itemGraphIsLoading = isLoading; |
@@ -423,7 +429,48 @@ export default createStore({ |
423 | 429 | return state.userActivityCache[cacheKey]; |
424 | 430 | }, |
425 | 431 | }, |
426 | | - actions: {}, |
| 432 | + actions: { |
| 433 | + async fetchCurrentUser({ state, commit }, { fullInfo = false } = {}) { |
| 434 | + if (state.currentUserInfoPromise) { |
| 435 | + return state.currentUserInfoPromise; |
| 436 | + } |
| 437 | + |
| 438 | + if (!fullInfo && state.currentUserInfoLoaded) { |
| 439 | + return state.currentUserID !== null; |
| 440 | + } |
| 441 | + |
| 442 | + commit("setCurrentUserInfoLoading", true); |
| 443 | + |
| 444 | + const promise = fetch(`${API_URL}/get-current-user/`, { |
| 445 | + method: "GET", |
| 446 | + credentials: "include", |
| 447 | + }) |
| 448 | + .then((response) => { |
| 449 | + if (!response.ok) { |
| 450 | + throw new Error("UNAUTHORIZED"); |
| 451 | + } |
| 452 | + return response.json(); |
| 453 | + }) |
| 454 | + .then((response_json) => { |
| 455 | + commit("setDisplayName", response_json.display_name); |
| 456 | + commit("setCurrentUserID", response_json.immutable_id); |
| 457 | + commit("setIsUnverified", response_json.account_status == "unverified" ? true : false); |
| 458 | + commit("setCurrentUserInfoLoading", false); |
| 459 | + state.currentUserInfoLoaded = true; |
| 460 | + state.currentUserInfoPromise = null; |
| 461 | + return response_json; |
| 462 | + }) |
| 463 | + .catch(() => { |
| 464 | + commit("setCurrentUserInfoLoading", false); |
| 465 | + state.currentUserInfoLoaded = true; |
| 466 | + state.currentUserInfoPromise = null; |
| 467 | + return null; |
| 468 | + }); |
| 469 | + |
| 470 | + state.currentUserInfoPromise = promise; |
| 471 | + return promise; |
| 472 | + }, |
| 473 | + }, |
427 | 474 | modules: {}, |
428 | 475 | // plugins: [createLogger()], |
429 | 476 | }); |
0 commit comments