From 8c15fb7f2bf83e5d2b9dbf4f35e5ea3fc0958503 Mon Sep 17 00:00:00 2001 From: Torsten Schulz Date: Wed, 4 Dec 2024 22:51:04 +0100 Subject: [PATCH] Fixed websockets in navigation --- backend/controllers/navigationController.js | 21 +++++++++++++------ frontend/src/App.vue | 2 -- frontend/src/components/AppNavigation.vue | 15 +++++++++++-- frontend/src/i18n/locales/de/falukant.json | 5 +++++ frontend/src/i18n/locales/de/navigation.json | 12 +++++++++++ frontend/src/i18n/locales/en/falukant.json | 5 +++++ frontend/src/main.js | 2 -- frontend/src/store/index.js | 22 ++------------------ frontend/src/views/home/NoLoginView.vue | 11 ++++++++-- 9 files changed, 61 insertions(+), 34 deletions(-) create mode 100644 frontend/src/i18n/locales/de/falukant.json create mode 100644 frontend/src/i18n/locales/en/falukant.json diff --git a/backend/controllers/navigationController.js b/backend/controllers/navigationController.js index fde4886..5b0b919 100644 --- a/backend/controllers/navigationController.js +++ b/backend/controllers/navigationController.js @@ -3,6 +3,7 @@ import UserParam from '../models/community/user_param.js'; import UserRight from '../models/community/user_right.js'; import UserRightType from '../models/type/user_right.js'; import UserParamType from '../models/type/user_param.js'; +import FalukantUser from '../models/falukant/data/user.js'; const menuStructure = { home: { @@ -236,17 +237,20 @@ class NavigationController { return age; } - filterMenu(menu, rights, age) { + async filterMenu(menu, rights, age, userId) { + console.log(userId); const filteredMenu = {}; + const hasFalukantAccount = await this.hasFalukantAccount(userId); for (const [key, value] of Object.entries(menu)) { if (value.visible.includes("all") - || value.visible.some(v => rights.includes(v) - || (value.visible.includes("anyadmin") && rights.length > 0)) - || (value.visible.includes("over14") && age >= 14)) { + || value.visible.some(v => rights.includes(v) || (value.visible.includes("anyadmin") && rights.length > 0)) + || (value.visible.includes("over14") && age >= 14) + || (value.visible.includes('nofalukantaccount') && !hasFalukantAccount) + || (value.visible.includes('hasfalukantaccount') && hasFalukantAccount)) { const { visible, ...itemWithoutVisible } = value; filteredMenu[key] = { ...itemWithoutVisible }; if (value.children) { - filteredMenu[key].children = this.filterMenu(value.children, rights, age); + filteredMenu[key].children = await this.filterMenu(value.children, rights, age, userId); } } } @@ -281,13 +285,18 @@ class NavigationController { const birthDate = userBirthdateParams.length > 0 ? userBirthdateParams[0].value : (new Date()).toDateString(); const age = this.calculateAge(birthDate); const rights = userRights.map(ur => ur.rightType.title); - const filteredMenu = this.filterMenu(menuStructure, rights, age); + const filteredMenu = await this.filterMenu(menuStructure, rights, age, user.id); res.status(200).json(filteredMenu); } catch (error) { console.error('Error fetching menu:', error); res.status(500).json({ error: 'An error occurred while fetching the menu' }); } } + + async hasFalukantAccount(userId) { + const falukantUser = await FalukantUser.findOne({ where: { userId: userId } }); + return falukantUser !== null; + } } export default NavigationController; diff --git a/frontend/src/App.vue b/frontend/src/App.vue index fab6ec0..1b2a850 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -65,8 +65,6 @@ export default { MessageDialog, }, created() { - this.$store.dispatch('loadLoginState'); - this.$store.dispatch('loadMenu'); this.$i18n.locale = this.$store.getters.language; }, }; diff --git a/frontend/src/components/AppNavigation.vue b/frontend/src/components/AppNavigation.vue index 455fd1c..5e58ccb 100644 --- a/frontend/src/components/AppNavigation.vue +++ b/frontend/src/components/AppNavigation.vue @@ -54,15 +54,24 @@ export default { } }, computed: { - ...mapGetters(['menu', 'user', 'menuNeedsUpdate']), + ...mapGetters(['menu', 'user', 'menuNeedsUpdate', 'socket']), }, watch: { menuNeedsUpdate(newValue) { if (newValue) { - console.log('Menu needs update, loading menu...'); this.loadMenu(); } }, + socket(newValue) { + if (newValue) { + newValue.on('forumschanged', (data) => { + this.fetchForums(); + }); + newValue.on('friendloginchanged', () => { + this.fetchFriends(); + }); + } + } }, created() { if (this.user && this.user.id) { @@ -75,10 +84,12 @@ export default { }, mounted() { if (this.$store.getters.socket) { + console.log('connect sockets in navigation') this.$store.getters.socket.on('forumschanged', (data) => { this.fetchForums(); }); this.$store.getters.socket.on('friendloginchanged', () => { + console.log('update friends'); this.fetchFriends(); }); } diff --git a/frontend/src/i18n/locales/de/falukant.json b/frontend/src/i18n/locales/de/falukant.json new file mode 100644 index 0000000..43fb193 --- /dev/null +++ b/frontend/src/i18n/locales/de/falukant.json @@ -0,0 +1,5 @@ +{ + "falukant": { + + } +} \ No newline at end of file diff --git a/frontend/src/i18n/locales/de/navigation.json b/frontend/src/i18n/locales/de/navigation.json index c112347..5f3a8b3 100644 --- a/frontend/src/i18n/locales/de/navigation.json +++ b/frontend/src/i18n/locales/de/navigation.json @@ -44,6 +44,18 @@ "manageFriends": "Freunde verwalten", "chat": "Chatten", "profile": "Profil" + }, + "m-falukant": { + "create": "Erstellen", + "overview": "Übersicht", + "towns": "Niederlassungen", + "directors": "Direktoren", + "factory": "Produktion", + "family": "Familie", + "house": "Haus", + "darknet": "Untergrund", + "reputation": "Reputation", + "moneyhistory": "Geldfluss" } } } diff --git a/frontend/src/i18n/locales/en/falukant.json b/frontend/src/i18n/locales/en/falukant.json new file mode 100644 index 0000000..43fb193 --- /dev/null +++ b/frontend/src/i18n/locales/en/falukant.json @@ -0,0 +1,5 @@ +{ + "falukant": { + + } +} \ No newline at end of file diff --git a/frontend/src/main.js b/frontend/src/main.js index 42d2641..1e9159e 100644 --- a/frontend/src/main.js +++ b/frontend/src/main.js @@ -31,6 +31,4 @@ app.use(router); app.use(i18n); app.use(vuetify); -store.dispatch('loadLoginState'); - app.mount('#app'); diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js index dc2f0cc..f243085 100644 --- a/frontend/src/store/index.js +++ b/frontend/src/store/index.js @@ -50,19 +50,13 @@ const store = createStore({ }, actions: { async login({ commit, dispatch }, user) { - console.log('login', user); await commit('dologin', user); await dispatch('initializeSocket'); const socket = this.getters.socket; if (socket) { - console.log('Emitting setUserId:', user.id); socket.emit('setUserId', user.id); - console.log('setUserId emitted successfully'); - } else { - console.error('Socket not initialized'); } await dispatch('loadMenu'); - dispatch('startMenuReload'); }, logout({ commit, state }) { if (state.socket) { @@ -74,23 +68,10 @@ const store = createStore({ }, initializeSocket({ commit, state }) { if (state.isLoggedIn && state.user) { - const socket = io(import.meta.env.VITE_API_BASE_URL); // oder Ihre URL - console.log('Socket initialized:', socket); - - socket.on('connect', () => { - console.log('Socket connected:', socket.id); - }); - - socket.on('disconnect', (reason) => { - console.log('Socket disconnected:', reason); - }); - + const socket = io(import.meta.env.VITE_API_BASE_URL); commit('setSocket', socket); } }, - loadLoginState({ commit }) { - commit('loadLoginState'); - }, setLanguage({ commit }, language) { commit('setLanguage', language); }, @@ -110,6 +91,7 @@ const store = createStore({ language: state => state.language, menu: state => state.menu, socket: state => state.socket, + menuNeedsUpdate: state => state.menuNeedsUpdate }, modules: { dialogs, diff --git a/frontend/src/views/home/NoLoginView.vue b/frontend/src/views/home/NoLoginView.vue index 5ea6967..70b7c2d 100644 --- a/frontend/src/views/home/NoLoginView.vue +++ b/frontend/src/views/home/NoLoginView.vue @@ -13,12 +13,16 @@
+ :title="$t('home.nologin.login.namedescription')" + @keydown.enter="focusPassword">
+ :title="$t('home.nologin.login.passworddescription')" + @keydown.enter="doLogin" + ref="passwordInput" + >
@@ -72,6 +76,9 @@ export default { openPasswordResetDialog() { this.$refs.passwordResetDialog.open(); }, + focusPassword() { + this.$refs.passwordInput.focus(); + }, async doLogin() { try { const response = await apiClient.post('/api/auth/login', { username: this.username, password: this.password });