Skip to content

Commit

Permalink
Put types in separate files.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrezz-b committed Jan 11, 2024
1 parent 9dfbdaa commit 02cec4b
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 85 deletions.
22 changes: 2 additions & 20 deletions frontend/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import type { User } from "@/types/User";
import type { LoginData, RegisterData, TokenResponse } from "@/types/Auth";
import { axiosPublic } from "./config/axios";
import { UseMutationOptions, useMutation } from "@tanstack/react-query";
import { User } from "./users";

interface TokenResponse {
token: string;
active: boolean;
}

interface RegisterData {
email: string;
password: string;
firstName: string;
lastName: string;
gender: string;
age: number;
}

interface LoginData {
email: string;
password: string;
}

const AuthService = {
useLogin: <T = TokenResponse>(
Expand Down
46 changes: 2 additions & 44 deletions frontend/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,14 @@
import useAuth from "@/hooks/useAuth";
import useAxiosPrivate from "@/hooks/useAxiosPrivate";
import type { Match, MatchData } from "@/types/Match";
import type { PreferencesInitData, User } from "@/types/User";
import {
UseMutationOptions,
useMutation,
useQuery,
useQueryClient,
} from "@tanstack/react-query";

export interface User {
id: string;
email: string;
firstName: string;
lastName: string;
hashedPassword: string;
gender: string;
age: number;
active: boolean;
description: string;
profileImageUrl: string;
phoneNumber: string;
location: string;
preferences: UserPreferences;
lastSeen: string;
createdAt: string;
updatedAt: string;
}

interface UserPreferences {
ageGroupMin: number;
ageGroupMax: number;
partnerGender: string;
}

export interface PreferencesInitData {
description: string;
profileImageUrl: string;
phoneNumber: string;
location: string;
preferences: UserPreferences;
}

interface Match {
id: string;
firstUserId: string;
secondUserId: string;
accepted?: boolean;
finished?: boolean;
}
interface MatchData {
userId: string;
}

export const UsersService = {
useGetCurrentUser() {
const { auth } = useAuth();
Expand Down
24 changes: 6 additions & 18 deletions frontend/src/context/AuthProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
import {
AuthContextData,
AuthContextProps,
AuthContextType,
} from "@/types/Auth";
import { createContext, useState } from "react";

interface StateContextType<T> {
auth: T;
setAuth: React.Dispatch<React.SetStateAction<T>>;
}

interface AuthObject {
user: Record<string, unknown>;
accessToken: string;
active: boolean;
}

type AuthContextType = StateContextType<AuthObject | null>;

const AuthContext = createContext<AuthContextType>({} as AuthContextType);

interface AuthContextProps {
children: React.ReactNode;
}

export const AuthProvider = ({ children }: AuthContextProps) => {
const [auth, setAuth] = useState<AuthObject | null>(null);
const [auth, setAuth] = useState<AuthContextData | null>(null);
return (
<AuthContext.Provider value={{ auth, setAuth }}>
{children}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/context/CurrentUserProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { User, UsersService } from "@/api/users";
import { UsersService } from "@/api/users";
import { createContext } from "react";

import type { User } from "@/types/User";
export const CurrentUserContext = createContext<User | null>(null);

const CurrentUserContextProvider = ({
Expand Down
33 changes: 33 additions & 0 deletions frontend/src/types/Auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
interface StateContextType<T> {
auth: T;
setAuth: React.Dispatch<React.SetStateAction<T>>;
}
export interface AuthContextData {
user: Record<string, unknown>;
accessToken: string;
active: boolean;
}
export type AuthContextType = StateContextType<AuthContextData | null>;

export interface AuthContextProps {
children: React.ReactNode;
}

export interface TokenResponse {
token: string;
active: boolean;
}

export interface RegisterData {
email: string;
password: string;
firstName: string;
lastName: string;
gender: string;
age: number;
}

export interface LoginData {
email: string;
password: string;
}
10 changes: 10 additions & 0 deletions frontend/src/types/Match.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Match {
id: string;
firstUserId: string;
secondUserId: string;
accepted: boolean;
finished: boolean;
}
export interface MatchData {
userId: string;
}
32 changes: 32 additions & 0 deletions frontend/src/types/User.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export interface User {
id: string;
email: string;
firstName: string;
lastName: string;
hashedPassword: string;
gender: string;
age: number;
active: boolean;
description: string;
profileImageUrl: string;
phoneNumber: string;
location: string;
preferences: UserPreferences;
lastSeen: string;
createdAt: string;
updatedAt: string;
}

interface UserPreferences {
ageGroupMin: number;
ageGroupMax: number;
partnerGender: string;
}

export interface PreferencesInitData {
description: string;
profileImageUrl: string;
phoneNumber: string;
location: string;
preferences: UserPreferences;
}
3 changes: 2 additions & 1 deletion frontend/src/views/Preferences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import "aos/dist/aos.css";
import MovmentButtons from "../components/MovmentButtons";
import { useNavigate } from "react-router-dom";
import useAuth from "@/hooks/useAuth";
import { PreferencesInitData, UsersService } from "@/api/users";
import { UsersService } from "@/api/users";
import type { PreferencesInitData } from "@/types/User";
import ScrollToFieldError from "@/components/ScrollToFieldError";

const preferenceSchema = Yup.object({
Expand Down

0 comments on commit 02cec4b

Please sign in to comment.