Skip to content

Commit

Permalink
Adjust pinia store definition to fix reactivity bug
Browse files Browse the repository at this point in the history
+ code cleanup
  • Loading branch information
SamuelWei committed Sep 24, 2024
1 parent 1b00955 commit 9c6b84f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
2 changes: 1 addition & 1 deletion resources/js/composables/useApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function useApi () {
if (responseStatus === env.HTTP_UNAUTHORIZED) { // 401 => unauthorized, redirect and show error messages as flash!
if (auth.isAuthenticated) {
toast.info(t('app.flash.unauthenticated'));
auth.setCurrentUser(null, false);
auth.setCurrentUser(null);
router.replace({ name: 'login', query: { redirect: router.currentRoute.value.path } });
}
} else if (responseStatus === env.HTTP_FORBIDDEN && errorMessage === 'This action is unauthorized.') { // 403 => unauthorized, show error messages as flash!
Expand Down
12 changes: 5 additions & 7 deletions resources/js/stores/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import { useLocaleStore } from './locale';
import { useApi } from '../composables/useApi';

export const useAuthStore = defineStore('auth', {
state: () => {
return {
currentUser: null
};
},
state: () => ({
currentUser: null
}),
getters: {
isAuthenticated: (state) => !_.isEmpty(state.currentUser)
},
Expand Down Expand Up @@ -53,15 +51,15 @@ export const useAuthStore = defineStore('auth', {
});

// logout successfull, clear current user
this.setCurrentUser(null, false);
this.setCurrentUser(null);
// reset timezone of i18n to use local system timezone
const locale = useLocaleStore();
locale.setTimezone(undefined);

return response;
},

setCurrentUser (currentUser, emit = true) {
setCurrentUser (currentUser) {
this.currentUser = currentUser;
}
}
Expand Down
34 changes: 16 additions & 18 deletions resources/js/stores/loading.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ import { useAuthStore } from './auth';
import { useSettingsStore } from './settings';

export const useLoadingStore = defineStore('loading', {
state: () => {
return {
initialized: false,
/**
* Counter of running data loading processes for the entire application.
*
* This counter can be used for a global overlay over the whole page and unmounts the whole app until finished
*/
loadingCounter: 0,

/**
* Counter of running data loading processes for the entire application.
*
* This counter can be used for a global overlay over the whole page without unmounting
*/
overlayLoadingCounter: 0
};
},
state: () => ({
initialized: false,
/**
* Counter of running data loading processes for the entire application.
*
* This counter can be used for a global overlay over the whole page and unmounts the whole app until finished
*/
loadingCounter: 0,

/**
* Counter of running data loading processes for the entire application.
*
* This counter can be used for a global overlay over the whole page without unmounting
*/
overlayLoadingCounter: 0
}),
actions: {
async initialize () {
const auth = useAuthStore();
Expand Down
12 changes: 5 additions & 7 deletions resources/js/stores/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { defineStore } from 'pinia';
import i18n, { setTimeZone, setLocale } from '../i18n';

export const useLocaleStore = defineStore('locale', {
state: () => {
return {
currentLocale: null,
timezone: null,
i18n: i18n.global
};
},
state: () => ({
currentLocale: null,
timezone: null,
i18n: i18n.global
}),
actions: {
async setLocale (currentLocale) {
this.currentLocale = currentLocale;
Expand Down
8 changes: 3 additions & 5 deletions resources/js/stores/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import { setToastLifetime } from '../composables/useToast';
import { updateTheme } from '../composables/useTheme';

export const useSettingsStore = defineStore('settings', {
state: () => {
return {
settings: null
};
},
state: () => ({
settings: null
}),
getters: {
getSetting: (state) => {
return (setting) => _.isEmpty(state.settings) ? undefined : _.get(state.settings, setting);
Expand Down

0 comments on commit 9c6b84f

Please sign in to comment.