mirror of
https://github.com/docmost/docmost.git
synced 2025-11-23 05:31:11 +10:00
switch to nx monorepo
This commit is contained in:
5
apps/client/src/features/user/atoms/current-user-atom.ts
Normal file
5
apps/client/src/features/user/atoms/current-user-atom.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { atomWithStorage } from "jotai/utils";
|
||||
|
||||
import { ICurrentUserResponse } from "@/features/user/types/user.types";
|
||||
|
||||
export const currentUserAtom = atomWithStorage<ICurrentUserResponse | null>("currentUser", null);
|
||||
12
apps/client/src/features/user/hooks/use-current-user.ts
Normal file
12
apps/client/src/features/user/hooks/use-current-user.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { useQuery, UseQueryResult } from "@tanstack/react-query";
|
||||
import { getUserInfo } from "@/features/user/services/user-service";
|
||||
import { ICurrentUserResponse } from "@/features/user/types/user.types";
|
||||
|
||||
export default function useCurrentUser(): UseQueryResult<ICurrentUserResponse> {
|
||||
return useQuery({
|
||||
queryKey: ["currentUser"],
|
||||
queryFn: async () => {
|
||||
return await getUserInfo();
|
||||
},
|
||||
});
|
||||
}
|
||||
30
apps/client/src/features/user/services/user-service.ts
Normal file
30
apps/client/src/features/user/services/user-service.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import api from '@/lib/api-client';
|
||||
import { ICurrentUserResponse, IUser } from '@/features/user/types/user.types';
|
||||
|
||||
export async function getMe(): Promise<IUser> {
|
||||
const req = await api.get<IUser>('/user/me');
|
||||
return req.data as IUser;
|
||||
}
|
||||
|
||||
export async function getUserInfo(): Promise<ICurrentUserResponse> {
|
||||
const req = await api.get<ICurrentUserResponse>('/user/info');
|
||||
return req.data as ICurrentUserResponse;
|
||||
}
|
||||
|
||||
export async function updateUser(data: Partial<IUser>): Promise<IUser> {
|
||||
const req = await api.post<IUser>('/user/update', data);
|
||||
|
||||
return req.data as IUser;
|
||||
}
|
||||
|
||||
export async function uploadAvatar(file: File) {
|
||||
const formData = new FormData();
|
||||
formData.append('avatar', file);
|
||||
const req = await api.post('/attachments/upload/avatar', formData, {
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
}
|
||||
});
|
||||
return req.data;
|
||||
}
|
||||
|
||||
21
apps/client/src/features/user/types/user.types.ts
Normal file
21
apps/client/src/features/user/types/user.types.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { IWorkspace } from "@/features/workspace/types/workspace.types";
|
||||
|
||||
export interface IUser {
|
||||
id: string;
|
||||
name: string;
|
||||
email: string;
|
||||
emailVerifiedAt: Date;
|
||||
avatarUrl: string;
|
||||
timezone: string;
|
||||
settings: any;
|
||||
lastLoginAt: string;
|
||||
lastLoginIp: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
workspaceRole?: string;
|
||||
}
|
||||
|
||||
export interface ICurrentUserResponse {
|
||||
user: IUser,
|
||||
workspace: IWorkspace
|
||||
}
|
||||
23
apps/client/src/features/user/user-provider.tsx
Normal file
23
apps/client/src/features/user/user-provider.tsx
Normal file
@ -0,0 +1,23 @@
|
||||
import { useAtom } from 'jotai';
|
||||
import { currentUserAtom } from '@/features/user/atoms/current-user-atom';
|
||||
import React, { useEffect } from 'react';
|
||||
import useCurrentUser from '@/features/user/hooks/use-current-user';
|
||||
|
||||
export function UserProvider({ children }: React.PropsWithChildren) {
|
||||
const [, setCurrentUser] = useAtom(currentUserAtom);
|
||||
const { data, isLoading, error } = useCurrentUser();
|
||||
|
||||
useEffect(() => {
|
||||
if (data && data.user) {
|
||||
setCurrentUser(data);
|
||||
}
|
||||
}, [data, isLoading, setCurrentUser]);
|
||||
|
||||
if (isLoading) return <></>;
|
||||
|
||||
if (error) {
|
||||
return <>an error occurred</>;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
Reference in New Issue
Block a user