ki-frontend/src/mixins/request.mixin.js

171 lines
4.3 KiB
JavaScript
Raw Normal View History

2021-09-19 12:55:33 +02:00
// SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
2021-09-19 17:30:37 +02:00
import store from '@/store'
2021-08-18 22:59:44 +02:00
export default {
methods: {
async submitLogin() {
this.showErrorMessage = false;
try {
2021-09-19 15:55:18 +02:00
const data = JSON.stringify({
username: this.username,
password: this.password,
})
const response = await fetch(`${this.apiUrl}/users/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: data
})
if (!response.ok) {
this.showErrorMessage = true
return
2021-08-18 22:59:44 +02:00
}
2021-09-19 15:55:18 +02:00
const responseData = await response.json()
2021-09-19 17:30:37 +02:00
store.commit('setCurrentUserId', parseInt(responseData.user_id, 10))
store.commit('setToken', responseData.token)
2021-09-19 15:55:18 +02:00
this.$router.push({ path: '/s/search' });
2021-08-18 22:59:44 +02:00
} catch (error) {
console.error(error);
this.showErrorMessage = true;
}
},
async search() {
try {
2021-09-19 15:55:18 +02:00
const response = await fetch(`${this.apiUrl}/${this.type}s?search=${this.searchText}`, {
2021-08-18 22:59:44 +02:00
headers: {
2021-09-19 17:30:37 +02:00
Authorization: `Bearer ${store.state.token}`,
2021-08-18 22:59:44 +02:00
},
}
);
2021-09-19 15:55:18 +02:00
if (!response.ok) {
console.error();
this.showErrorMessage = true;
return
}
const responseData = await response.json()
this.searchResults = responseData[`${this.type}s`];
if (
!this.values
.map((item) => item[this.type].name.toLowerCase())
.includes(this.searchText.toLowerCase())
) {
this.searchResults.unshift({ name: this.searchText });
2021-08-18 22:59:44 +02:00
}
} catch (error) {
console.error();
this.showErrorMessage = true;
}
},
async initEditPage() {
2021-09-19 17:30:37 +02:00
const userId = store.state.currentUserId
2021-09-19 15:55:18 +02:00
const url = `${this.apiUrl}/users/${userId}/profile`
2021-08-18 22:59:44 +02:00
try {
2021-09-19 15:55:18 +02:00
const response = await fetch(url, {
headers: {
2021-09-19 17:30:37 +02:00
Authorization: `Bearer ${store.state.token}`
2021-09-19 15:55:18 +02:00
},
2021-08-18 22:59:44 +02:00
}
);
2021-09-19 15:55:18 +02:00
if (!response.ok) {
return
}
const responseData = await response.json()
this.profile = responseData.profile;
2021-08-18 22:59:44 +02:00
} catch (error) {
console.error(error);
}
},
async submitFormEdit() {
2021-09-19 15:55:18 +02:00
this.showErrorMessage = false
this.showSuccessMessage = false
2021-09-20 16:44:44 +02:00
const userId = store.state.currentUserId
2021-09-19 15:55:18 +02:00
2021-08-18 22:59:44 +02:00
try {
2021-09-19 15:55:18 +02:00
const body = JSON.stringify(this.profile)
const response = await fetch(
2021-09-20 16:44:44 +02:00
`${this.apiUrl}/users/${userId}/profile`,
2021-08-18 22:59:44 +02:00
{
2021-09-19 15:55:18 +02:00
method: 'POST',
2021-08-18 22:59:44 +02:00
headers: {
2021-09-19 17:30:37 +02:00
Authorization: `Bearer ${store.state.token}`,
2021-09-19 15:55:18 +02:00
'Content-Type': 'application/json',
2021-08-18 22:59:44 +02:00
},
2021-09-19 15:55:18 +02:00
body
2021-08-18 22:59:44 +02:00
}
);
2021-09-19 15:55:18 +02:00
if (!response.ok) {
this.showErrorMessage = true
return
2021-08-18 22:59:44 +02:00
}
2021-09-19 15:55:18 +02:00
this.showSuccessMessage = true
2021-08-18 22:59:44 +02:00
} catch (error) {
console.error(error);
this.showErrorMessage = true;
}
},
async initViewPage() {
try {
2021-09-19 15:55:18 +02:00
const response = await fetch(`${this.apiUrl}/users/${this.$route.params.memberId}/profile`, {
2021-09-19 17:30:37 +02:00
headers: { Authorization: `Bearer ${store.state.token}` },
2021-08-18 22:59:44 +02:00
}
);
2021-09-19 15:55:18 +02:00
if (!response.ok) {
return
}
const responseData = await response.json()
2021-09-19 17:30:37 +02:00
store.commit('setCurrentProfile', responseData.profile)
2021-08-18 22:59:44 +02:00
} catch (error) {
console.error(error);
}
},
async submitSearch() {
this.showErrorMessage = false;
2021-09-19 15:55:18 +02:00
2021-08-18 22:59:44 +02:00
try {
2021-09-19 15:55:18 +02:00
const url = new URL(`${this.apiUrl}/users/profiles`)
2021-08-18 22:59:44 +02:00
if (this.searchText != "") {
2021-09-19 15:55:18 +02:00
url.searchParams.append('nickname', this.searchText)
2021-08-18 22:59:44 +02:00
}
2021-09-19 15:55:18 +02:00
const response = await fetch(url, {
2021-08-18 22:59:44 +02:00
headers: {
2021-09-19 17:30:37 +02:00
Authorization: `Bearer ${store.state.token}`,
2021-08-18 22:59:44 +02:00
},
});
2021-09-19 15:55:18 +02:00
if (!response.ok) {
this.showErrorMessage = true
return
}
const responseData = await response.json()
this.searchResults = responseData.profiles;
this.searchTotal = responseData.total;
2021-08-18 22:59:44 +02:00
} catch (error) {
console.error(error);
this.showErrorMessage = true;
}
},
},
2021-09-19 15:55:18 +02:00
}