|
| 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, |
@@ -344,6 +347,9 @@ export default createStore({ |
344 | 347 | }, |
345 | 348 | setCurrentUserInfoLoading(state, isLoading) { |
346 | 349 | state.currentUserInfoLoading = isLoading; |
| 350 | + if (!isLoading && !state.currentUserID) { |
| 351 | + state.currentUserInfoLoaded = true; |
| 352 | + } |
347 | 353 | }, |
348 | 354 | setItemGraphIsLoading(state, isLoading) { |
349 | 355 | state.itemGraphIsLoading = isLoading; |
@@ -409,7 +415,48 @@ export default createStore({ |
409 | 415 | return Object.values(state.blocksInfos); |
410 | 416 | }, |
411 | 417 | }, |
412 | | - actions: {}, |
| 418 | + actions: { |
| 419 | + async fetchCurrentUser({ state, commit }, { fullInfo = false } = {}) { |
| 420 | + if (state.currentUserInfoPromise) { |
| 421 | + return state.currentUserInfoPromise; |
| 422 | + } |
| 423 | + |
| 424 | + if (!fullInfo && state.currentUserInfoLoaded) { |
| 425 | + return state.currentUserID !== null; |
| 426 | + } |
| 427 | + |
| 428 | + commit("setCurrentUserInfoLoading", true); |
| 429 | + |
| 430 | + const promise = fetch(`${API_URL}/get-current-user/`, { |
| 431 | + method: "GET", |
| 432 | + credentials: "include", |
| 433 | + }) |
| 434 | + .then((response) => { |
| 435 | + if (!response.ok) { |
| 436 | + throw new Error("UNAUTHORIZED"); |
| 437 | + } |
| 438 | + return response.json(); |
| 439 | + }) |
| 440 | + .then((response_json) => { |
| 441 | + commit("setDisplayName", response_json.display_name); |
| 442 | + commit("setCurrentUserID", response_json.immutable_id); |
| 443 | + commit("setIsUnverified", response_json.account_status == "unverified" ? true : false); |
| 444 | + commit("setCurrentUserInfoLoading", false); |
| 445 | + state.currentUserInfoLoaded = true; |
| 446 | + state.currentUserInfoPromise = null; |
| 447 | + return response_json; |
| 448 | + }) |
| 449 | + .catch(() => { |
| 450 | + commit("setCurrentUserInfoLoading", false); |
| 451 | + state.currentUserInfoLoaded = true; |
| 452 | + state.currentUserInfoPromise = null; |
| 453 | + return null; |
| 454 | + }); |
| 455 | + |
| 456 | + state.currentUserInfoPromise = promise; |
| 457 | + return promise; |
| 458 | + }, |
| 459 | + }, |
413 | 460 | modules: {}, |
414 | 461 | // plugins: [createLogger()], |
415 | 462 | }); |
0 commit comments