Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the client to make use of schema models for the users API #16608

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions client/src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ import {
removeFavoriteToolQuery,
setCurrentThemeQuery,
} from "@/stores/users/queries";
import { expand } from "rxjs";

type QuotaUsageResponse = components["schemas"]["UserQuotaUsage"];

interface User extends QuotaUsageResponse {
id: string;
email: string;
tags_used: string[];
isAnonymous: false;
}
type DetailedAnonymousUser = components["schemas"]["AnonUserModel"]
type DetailedUserModelUser = components["schemas"]["DetailedUserModel"]

interface AnonymousUser {
isAnonymous: true;
interface AnonymousUser extends DetailedAnonymousUser {
isAnonymous: true
}

interface User extends DetailedUserModelUser {
isAnonymous: false;
}
interface Preferences {
theme: string;
favorites: { tools: string[] };
Expand Down Expand Up @@ -61,10 +60,16 @@ export const useUserStore = defineStore("userStore", () => {
if (!loadPromise) {
loadPromise = getCurrentUser()
.then(async (user) => {
currentUser.value = { ...user, isAnonymous: !user.email };
currentPreferences.value = user?.preferences ?? null;
if ('email' in user) {
currentUser.value = { ...user, isAnonymous: false };
currentPreferences.value = user.preferences as unknown as Preferences;
}
else {
currentUser.value = { ...user, isAnonymous: true };
currentPreferences.value = null;
}
// TODO: This is a hack to get around the fact that the API returns a string
if (currentPreferences.value?.favorites) {
if (currentPreferences.value?.favorites && isRegisteredUser(user)) {
currentPreferences.value.favorites = JSON.parse(user?.preferences?.favorites ?? { tools: [] });
}
if (includeHistories) {
Expand All @@ -82,7 +87,11 @@ export const useUserStore = defineStore("userStore", () => {
});
}
}

function isRegisteredUser(user?: any): user is User {
return (
user !== undefined && "email" in user
);
}
async function setCurrentTheme(theme: string) {
if (!currentUser.value || currentUser.value.isAnonymous) {
return;
Expand Down
41 changes: 18 additions & 23 deletions client/src/stores/users/queries.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
import axios from "axios";
import { fetcher } from "@/schema"

import { prependPath } from "@/utils/redirect";
import { rethrowSimple } from "@/utils/simple-error";
const getUser = fetcher.path("/api/users/{user_id}").method("get").create();
const setTheme = fetcher.path("/api/users/{user_id}/theme/{theme}").method("put").create();
const addFavoriteTool = fetcher.path("/api/users/{user_id}/favorites/{object_type}").method("put").create();
const removeFavoriteTool = fetcher.path("/api/users/{user_id}/favorites/{object_type}/{object_id}").method("delete").create();

export async function getCurrentUser() {
const url = prependPath("/api/users/current");
const response = await axios.get(url);
const response = await getUser({ user_id: "current" })
if (response.status != 200) {
throw new Error("Failed to get current user");
}
return response.data;
}

export async function addFavoriteToolQuery(userId: string, toolId: string) {
const url = prependPath(`/api/users/${userId}/favorites/tools`);
try {
const { data } = await axios.put(url, { object_id: toolId });
return data["tools"];
} catch (e) {
rethrowSimple(e);
const response = await addFavoriteTool({ user_id: userId, object_type: "tools", object_id: toolId })
if (response.status != 200) {
throw new Error("Failed to add tool to favorites");
}
return response.data["tools"];
}

export async function removeFavoriteToolQuery(userId: string, toolId: string) {
const url = prependPath(`/api/users/${userId}/favorites/tools/${encodeURIComponent(toolId)}`);
try {
const { data } = await axios.delete(url);
return data["tools"];
} catch (e) {
rethrowSimple(e);
const response = await removeFavoriteTool({ user_id: userId, object_type: "tools", object_id: toolId })
if (response.status != 200) {
throw new Error("Failed to remove tool from favorites");
}
return response.data["tools"];
}

export async function setCurrentThemeQuery(userId: string, theme: string) {
const url = prependPath(`/api/users/${userId}/theme/${theme}`);
try {
const { data } = await axios.put(url, { theme: theme });
return data;
} catch (e) {
rethrowSimple(e);
const response = await setTheme({ user_id: userId, theme: theme })
if (response.status != 200) {
throw new Error("Failed to set theme");
}
return response.data;
}