introduce vuex

This commit is contained in:
2021-09-19 17:30:37 +02:00
parent 94d2a98b96
commit a408cae686
10 changed files with 129 additions and 46 deletions

View File

@ -2,6 +2,8 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
import store from '@/store'
export default {
methods: {
async submitLogin() {
@ -26,8 +28,8 @@ export default {
}
const responseData = await response.json()
localStorage.setItem('token', responseData.token);
localStorage.setItem('user_id', responseData.user_id);
store.commit('setCurrentUserId', parseInt(responseData.user_id, 10))
store.commit('setToken', responseData.token)
this.$router.push({ path: '/s/search' });
} catch (error) {
console.error(error);
@ -38,7 +40,7 @@ export default {
try {
const response = await fetch(`${this.apiUrl}/${this.type}s?search=${this.searchText}`, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
Authorization: `Bearer ${store.state.token}`,
},
}
);
@ -65,13 +67,13 @@ export default {
}
},
async initEditPage() {
const userId = localStorage.getItem('user_id')
const userId = store.state.currentUserId
const url = `${this.apiUrl}/users/${userId}/profile`
try {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`
Authorization: `Bearer ${store.state.token}`
},
}
);
@ -94,11 +96,11 @@ export default {
try {
const body = JSON.stringify(this.profile)
const response = await fetch(
`${this.apiUrl}/users/${localStorage.getItem("user_id")}/profile`,
`${this.apiUrl}/users/${store.currentUserId}/profile`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
Authorization: `Bearer ${store.state.token}`,
'Content-Type': 'application/json',
},
body
@ -119,7 +121,7 @@ export default {
async initViewPage() {
try {
const response = await fetch(`${this.apiUrl}/users/${this.$route.params.memberId}/profile`, {
headers: { Authorization: `Bearer ${localStorage.getItem("token")}` },
headers: { Authorization: `Bearer ${store.state.token}` },
}
);
@ -128,7 +130,7 @@ export default {
}
const responseData = await response.json()
this.profile = responseData.profile;
store.commit('setCurrentProfile', responseData.profile)
} catch (error) {
console.error(error);
}
@ -145,7 +147,7 @@ export default {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${localStorage.getItem("token")}`,
Authorization: `Bearer ${store.state.token}`,
},
});