import axios from "axios"; export default { methods: { async submitLogin() { this.showErrorMessage = false; try { const loginResult = await axios.post( `${this.apiUrl}/users/login`, { username: this.username, password: this.password, } ); if (loginResult.status === 200) { this.showErrorMessage = false; //success login localStorage.setItem("token", loginResult.data.token); localStorage.setItem("user_id", loginResult.data.user_id); this.$router.push({ path: "/s/search" }); } else { this.showErrorMessage = true; } } catch (error) { console.error(error); this.showErrorMessage = true; } }, async search() { try { const request = await axios.get( `${this.apiUrl}/${this.type}s?search=${this.searchText}`, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, }, } ); if (request.status === 200) { this.searchResults = request.data[`${this.type}s`]; if ( !this.values .map((item) => item[this.type].name.toLowerCase()) .includes(this.searchText.toLowerCase()) ) { this.searchResults.unshift({ name: this.searchText }); } } } catch (error) { console.error(); this.showErrorMessage = true; } }, async initEditPage() { try { const userProfile = await axios.get( `${this.apiUrl}/users/${localStorage.getItem("user_id")}/profile`, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}` }, } ); this.profile = userProfile.data.profile; } catch (error) { console.error(error); } }, async submitFormEdit() { try { const formSubmitResult = await axios.post( `${this.apiUrl}/users/${localStorage.getItem("user_id")}/profile`, this.profile, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, }, } ); if (formSubmitResult.status === 200) { // success this.showSuccessMessage = true; } else { // failure this.showErrorMessage = true; } } catch (error) { console.error(error); this.showErrorMessage = true; } }, async initViewPage() { try { const userProfile = await axios.get( `${this.apiUrl}/users/${this.$route.params.memberId}/profile`, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}` }, } ); this.profile = userProfile.data.profile; } catch (error) { console.error(error); } }, async submitSearch() { this.showErrorMessage = false; try { let url = `${this.apiUrl}/users/profiles`; if (this.searchText != "") { url += `?nickname=${this.searchText}`; } const result = await axios.get(url, { headers: { Authorization: `Bearer ${localStorage.getItem("token")}`, }, }); this.searchResults = result.data.profiles; this.searchTotal = result.data.total; } catch (error) { console.error(error); this.showErrorMessage = true; } }, }, }