change name action for api

This commit is contained in:
Nikolai Petukhov
2024-10-03 22:35:39 +03:00
parent 6bea0428f4
commit 8d0fadc906
12 changed files with 123 additions and 70 deletions

View File

@@ -10,10 +10,13 @@ export const BASE_API_URL = DEV + getConfigValue("enterfront.api");
// fetch(`${BASE_API_URL}/books/list`)
export async function post(path, body) {
const token = localStorage.getItem('token');
const res = await fetch(`${BASE_API_URL}${path}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": token ? `Bearer ${token}` : undefined
},
body: JSON.stringify(body)
});
@@ -32,8 +35,13 @@ export async function post(path, body) {
}
export async function get(path){
const token = localStorage.getItem('token');
const res = await fetch(`${BASE_API_URL}${path}`, {
method: "GET"
method: "GET",
headers: {
"Authorization": token ? `Bearer ${token}` : undefined
}
});
if (res.status === 200) {

View File

@@ -1,10 +1,11 @@
import React, {useEffect, useState} from "react";
import AccountButtons from "../components/account/AccountButtons.jsx";
import userIcon from "../../images/user.svg";
import {get} from "../backend/api";
import {get, post} from "../backend/api";
import {displayMessage} from "../backend/notifications/notifications";
import {MessageType} from "../backend/notifications/message";
import HelloItem from "../components/account/HelloItem.jsx";
import { URLs } from "../__data__/urls";
const Account = () => {
const exitHandler = () => {
@@ -14,12 +15,27 @@ const Account = () => {
localStorage.setItem("message", "Exited successfully!");
window.location.href = "/";
}
const changeNameHandler = () => {}
const changePassHandler = () => {}
const [nickname, setNickname] = useState("");
const [id, setId] = useState("");
async function changeNameHandler () {
// ...
const {ok, data} = await post('/change/nickname', {id: id, newNickname: "New Name"});
if (!ok) {
displayMessage(data.message, MessageType.ERROR);
} else {
localStorage.setItem("message", "Name was changed");
window.location.href = URLs.account.url;
}
}
async function changePassHandler (){
// ...
}
async function getUser() {
const username = localStorage.getItem("username");
if (!username) {