Skip to content

Commit 00a21f5

Browse files
committed
#210 update settings request
1 parent 3aec416 commit 00a21f5

File tree

7 files changed

+62
-60
lines changed

7 files changed

+62
-60
lines changed

app.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<script lang="ts" setup>
66
import "./src/assets/index.css";
77
import "./src/assets/vendor";
8-
import { useEventsStore, useSettingsStore } from "~/src/shared/stores";
8+
import { useEventsStore } from "~/src/shared/stores";
99
10-
await useSettingsStore().initialize();
1110
useEventsStore().initialize();
1211
</script>

middleware/auth.global.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import { navigateTo, defineNuxtRouteMiddleware } from "#app";
2-
import { useSettings } from "~/src/shared/lib/use-settings";
32
import {useSettingsStore, useProfileStore} from "~/src/shared/stores";
43

54
export default defineNuxtRouteMiddleware(async (to) => {
6-
const { auth} = storeToRefs(useSettingsStore())
5+
const settingsStore = useSettingsStore()
6+
const {isFetched, isAuthEnabled } = storeToRefs(settingsStore)
77

8-
if (!auth.value.isEnabled) {
8+
if (!isFetched.value) {
9+
await settingsStore.fetchSettings()
10+
}
11+
12+
if (!isAuthEnabled.value) {
913
return undefined
1014
}
1115

12-
const store = useProfileStore()
13-
const { isAuthenticated} = storeToRefs(store)
14-
store.fetchToken()
16+
const profileStore = useProfileStore()
17+
const { isAuthenticated} = storeToRefs(profileStore)
18+
await profileStore.getStoredToken()
1519

1620
if (isAuthenticated.value) {
17-
const {api: {getProfile}} = useSettings();
18-
const profile = await getProfile();
19-
store.setProfile(profile)
20-
return undefined
21+
await profileStore.getProfile();
2122
}
2223

2324
if (to.name !== 'login' && !isAuthenticated.value) {
2425
return navigateTo('/login')
2526
}
2627

2728
if (to.name === 'login' && to?.query?.token) {
28-
store.setToken(String(to.query.token))
29+
profileStore.setToken(String(to.query.token))
2930
return navigateTo('/')
3031
}
3132

pages/login.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import { IconSvg } from "~/src/shared/ui";
99
setPageLayout("blank");
1010
1111
const store = useProfileStore();
12-
const { auth } = storeToRefs(useSettingsStore());
12+
const { authLogicUrl } = storeToRefs(useSettingsStore());
1313
1414
if (store.isAuthenticated) {
1515
await navigateTo("/");
1616
}
1717
18-
const loginUrl = computed(() => `${REST_API_URL}/${auth.value.loginUrl}`);
18+
const loginUrl = computed(() => `${REST_API_URL}/${authLogicUrl}`);
1919
2020
const redirect = async () => {
2121
await navigateTo(loginUrl.value, {
@@ -43,7 +43,7 @@ const redirect = async () => {
4343
<div
4444
class="login-form-right-block"
4545
style="
46-
background: url('/bg.jpg');
46+
background: url(&quot;/bg.jpg&quot;);
4747
background-size: cover;
4848
background-position: center center;
4949
"
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,17 @@
11
import {useProfileStore} from "../../stores/profile/profile-store";
2-
import type {TProfile, TSettings, TProjects} from "../../types";
2+
import type { TProjects} from "../../types";
33
import { REST_API_URL } from "../io/constants";
44

55

66
type TUseSettings = {
77
api: {
8-
getProfile: () => Promise<TProfile>
9-
getSettings: () => Promise<TSettings>
108
getProjects: () => Promise<TProjects>
119
}
1210
}
1311

1412
export const useSettings = (): TUseSettings => {
1513
const { token } = storeToRefs(useProfileStore())
1614

17-
const getAppSettings = () => fetch(`${REST_API_URL}/api/settings`)
18-
.then((response) => response.json())
19-
.catch((e) => {
20-
console.error(e);
21-
22-
return null
23-
});
24-
25-
const getProfile = () => fetch(`${REST_API_URL}/api/me`, {
26-
headers: {"X-Auth-Token": token.value || ""}
27-
})
28-
.then((response) => response.json())
29-
.catch((e) => {
30-
console.error(e);
31-
32-
return null
33-
});
3415
const getProjects = () => fetch(`${REST_API_URL}/api/projects`, {
3516
headers: {"X-Auth-Token": token.value || ""}
3617
})
@@ -43,9 +24,7 @@ export const useSettings = (): TUseSettings => {
4324

4425
return {
4526
api: {
46-
getProfile,
4727
getProjects,
48-
getSettings: getAppSettings
4928
}
5029
}
5130
}

src/shared/stores/profile/profile-store.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineStore } from "pinia";
2+
import {REST_API_URL} from "../../lib/io/constants";
23
import type {TProfile} from "../../types";
34
import {getStoredToken, removeStoredToken, setStoredToken} from "./local-storage-actions";
45

@@ -10,19 +11,38 @@ export const useProfileStore = defineStore("profileStore", {
1011
}),
1112
getters: {
1213
isAuthenticated(): boolean {
13-
return this.token !== undefined && this.token !== null && this.token !== "null";
14+
return !!this.token && this.token !== "null";
1415
},
1516
},
1617
actions: {
1718
setToken(token: string): void {
1819
this.token = token;
1920
setStoredToken(token);
2021
},
22+
async getProfile(): Promise<TProfile> {
23+
// TODO: need to remove fetch out of the store
24+
const profile = await fetch(`${REST_API_URL}/api/me`, {
25+
headers: {"X-Auth-Token": this.token || ""}
26+
})
27+
.then((response) => response.json())
28+
.catch((e) => {
29+
console.error(e);
30+
31+
return null
32+
});
33+
34+
this.setProfile(profile)
35+
36+
return profile
37+
},
2138
setProfile(profile: TProfile): void {
2239
this.profile = profile;
2340
},
24-
fetchToken(): void {
25-
this.setToken(getStoredToken() || '');
41+
getStoredToken(): string {
42+
const token = getStoredToken()
43+
this.setToken(token);
44+
45+
return token
2646
},
2747
removeToken(): void {
2848
this.token = '';

src/shared/stores/settings/settings-store.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {defineStore} from "pinia";
2-
import {useSettings} from "../../lib/use-settings";
2+
import {REST_API_URL} from "../../lib/io/constants";
33
import type { TSettings } from "../../types";
44
import {THEME_MODES} from "./constants";
55
import {
@@ -16,32 +16,37 @@ import {
1616
export const useSettingsStore = defineStore("settingsStore", {
1717
state: () => ({
1818
apiVersion: '',
19-
isSettingsLoading: false,
20-
auth: {
21-
isEnabled: false,
22-
loginUrl: '/login',
23-
},
19+
isFetched: false,
20+
isAuthEnabled: false,
21+
authLogicUrl: '/login',
2422
codeEditor: getStoredPrimaryCodeEditor() || 'phpstorm',
2523
themeType: getStoredActiveTheme(),
2624
isFixedHeader: getStoredFixedHeader(),
2725
isVisibleEventCounts: getStoredEventsCountVisibility(),
2826
}),
2927
actions: {
30-
async initialize() {
31-
this.isSettingsLoading = true
32-
const {api: { getSettings }} = useSettings();
28+
async fetchSettings() {
29+
// TODO: need to remove fetch out of the store
30+
const settings: TSettings = await fetch(`${REST_API_URL}/api/settings`)
31+
.then((response) => response.json())
32+
.catch((e) => {
33+
console.error(e);
34+
35+
return null
36+
});
3337

34-
const { version, auth }: TSettings = await getSettings()
35-
if (version) {
36-
this.apiVersion = version
38+
if (settings.version) {
39+
this.apiVersion = settings.version
3740
}
3841

39-
if (auth) {
40-
this.auth.isEnabled = auth.enabled;
41-
this.auth.loginUrl = auth.login_url;
42+
if (settings.auth) {
43+
this.isAuthEnabled = settings.auth.enabled;
44+
this.authLogicUrl = settings.auth.login_url;
4245
}
4346

44-
this.isSettingsLoading = false
47+
this.isFetched = true
48+
49+
return settings
4550
},
4651
changeTheme() {
4752
this.themeType = this.themeType === THEME_MODES.DARK

src/widgets/ui/layout-sidebar/layout-sidebar.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { version } from "../../../../package.json";
1717
import { EVENTS_LINKS_MAP, EVENTS_NAV_ORDER } from "./constants";
1818
1919
const { isConnectedWS } = storeToRefs(useConnectionStore());
20-
const { isVisibleEventCounts, auth } = storeToRefs(useSettingsStore());
20+
const { isVisibleEventCounts, isAuthEnabled } = storeToRefs(useSettingsStore());
2121
const eventsStore = useEventsStore();
2222
const { availableProjects, isMultipleProjects, activeProject } =
2323
storeToRefs(eventsStore);
@@ -104,8 +104,6 @@ const logout = () => {
104104
105105
const path = computed(() => useRoute().path);
106106
107-
const isAuthEnabled = computed(() => auth.value.isEnabled);
108-
109107
const { apiVersion } = storeToRefs(useSettingsStore());
110108
111109
const clientVersion = ref(

0 commit comments

Comments
 (0)