Skip to content

Commit

Permalink
Refresh user name cache after set username
Browse files Browse the repository at this point in the history
  • Loading branch information
hanydd committed Oct 27, 2024
1 parent dd05f0d commit ae2368e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
11 changes: 3 additions & 8 deletions src/popup/UserWork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Spin } from "antd";
import { MessageInstance } from "antd/es/message/interface";
import * as React from "react";
import Config from "../config";
import { asyncRequestToServer } from "../requests/requests";
import { getUserInfo, setUsername } from "../requests/user";
import { getErrorMessage, getFormattedHours } from "../utils/formating";
import { getHash } from "../utils/hash";

Expand Down Expand Up @@ -40,12 +40,7 @@ class UserWork extends React.Component<UserWorkProps, UserWorkState> {
this.userNameInputRef = React.createRef<HTMLInputElement>();

getHash(Config.config.userID)
.then((hash) =>
asyncRequestToServer("GET", "/api/userInfo", {
publicUserID: hash,
values: ["userName", "viewCount", "minutesSaved", "vip", "permissions", "segmentCount"],
})
)
.then((hash) => getUserInfo(hash))
.then((res) => {
if (res.status === 200) {
const userInfo = JSON.parse(res.responseText);
Expand Down Expand Up @@ -84,7 +79,7 @@ class UserWork extends React.Component<UserWorkProps, UserWorkState> {

this.setState({ usernameLoading: true });

asyncRequestToServer("POST", "/api/setUsername?userID=" + Config.config.userID + "&username=" + inputUserName)
setUsername(inputUserName)
.then((response) => {
if (response.status == 200) {
this.setState({ editingUsername: false, userName: inputUserName });
Expand Down
30 changes: 30 additions & 0 deletions src/requests/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Config from "../config";
import { getHash } from "../utils/hash";
import { FetchResponse } from "./background-request-proxy";
import { asyncRequestToServer } from "./requests";

export async function setUsername(inputUserName: string): Promise<FetchResponse> {
const response = asyncRequestToServer(
"POST",
"/api/setUsername?userID=" + Config.config.userID + "&username=" + inputUserName
);

getUserInfo("", true);

return response;
}

export async function getUserInfo(userID: string, ignoreServerCache: boolean = false): Promise<FetchResponse> {
if (!userID) {
userID = await getHash(Config.config.userID);
}
return asyncRequestToServer(
"GET",
"/api/userInfo",
{
publicUserID: userID,
values: ["userName", "viewCount", "minutesSaved", "vip", "permissions", "segmentCount"],
},
ignoreServerCache
);
}

0 comments on commit ae2368e

Please sign in to comment.