{id}
{name}
- {identifiedCode}
{phone}
-
- {
- roomId
- ? roomId
- : 'None'
- }
-
@@ -77,12 +39,6 @@ const UserRow = ({ user, reload, setReload }: IUserRow) => {
)}
/>
- }
- onClick={() => handleCheckOut(user)}
- >
- Check out
-
diff --git a/hotel-management/src/pages/User/UserTable.tsx b/hotel-management/src/pages/User/UserTable.tsx
index 645969e..b25d859 100644
--- a/hotel-management/src/pages/User/UserTable.tsx
+++ b/hotel-management/src/pages/User/UserTable.tsx
@@ -1,5 +1,5 @@
import { useSearchParams } from 'react-router-dom';
-import { Dispatch, SetStateAction, useEffect, useState } from 'react';
+import { Dispatch, SetStateAction, useCallback, useState } from 'react';
// Components
import Menus from '@component/Menus';
@@ -13,9 +13,6 @@ import UserRow from './UserRow';
// Types
import { IUser } from '@type/users';
-// Hooks
-import { useFetch } from '@hook/useFetch';
-
// Constants
import { ORDERBY_OPTIONS, USER_PAGE } from '@constant/commons';
@@ -24,44 +21,52 @@ import Direction from '@commonStyle/Direction';
import { StyledOperationTable } from './styled';
import Spinner from '@commonStyle/Spinner';
+import { useQuery } from '@tanstack/react-query';
+import { getAllUsers } from '@service/userServices';
+import toast from 'react-hot-toast';
+
interface IUserTable {
reload: boolean;
setReload: Dispatch>;
}
const UserTable = ({ reload, setReload }: IUserTable) => {
- const columnName = ['Id', 'Name', 'Identified Code', 'Phone', 'Room Id'];
- const [users, setUsers] = useState([]);
+ const columnName = ['Id', 'Name', 'Phone'];
+
const [phoneSearch, setPhoneSearch] = useState('');
const [searchParams] = useSearchParams();
const sortByValue = searchParams.get('sortBy')
? searchParams.get('sortBy')!
- : '';
+ : 'id';
const orderByValue = searchParams.get('orderBy')
? searchParams.get('orderBy')!
- : '';
+ : 'asc';
- const { data, isPending, errorFetchMsg } = useFetch(
- 'users',
- 'phone',
- phoneSearch,
- sortByValue,
- orderByValue,
- reload
+ const {
+ isLoading,
+ data: users,
+ error,
+ } = useQuery({
+ queryKey: ['cabins', sortByValue, orderByValue, phoneSearch],
+ queryFn: () => getAllUsers(sortByValue, orderByValue, phoneSearch),
+ });
+
+ if(error) {
+ toast.error(error.message);
+ }
+
+ const renderUserRow = useCallback(
+ (user: IUser) => (
+
+ ),
+ [reload, setReload]
);
- useEffect(() => {
- if (data) {
- setUsers(data);
- } else {
- setUsers([]);
- }
-
- if (errorFetchMsg) {
- console.error(errorFetchMsg);
- }
- }, [data, errorFetchMsg, setUsers]);
-
return (
<>
@@ -75,27 +80,17 @@ const UserTable = ({ reload, setReload }: IUserTable) => {
/>
- {isPending && }
+ {isLoading && }
- {users.length ? (
+ {users && users.length ? (
-
+
-
- data={users}
- render={(user: IUser) => (
-
- )}
- />
+ data={users} render={renderUserRow} />
) : (
- !isPending && No data to show here!
+ !isLoading && No data to show here!
)}
>
diff --git a/hotel-management/src/services/userServices.ts b/hotel-management/src/services/userServices.ts
index 94cc7b3..ab17094 100644
--- a/hotel-management/src/services/userServices.ts
+++ b/hotel-management/src/services/userServices.ts
@@ -12,6 +12,10 @@ import { IUser } from '@type/users';
// Helpers
import { sendRequest } from '@helper/sendRequest';
import { errorMsg } from '@helper/helper';
+import supabase from '@constant/supabaseConfig';
+
+const USERS_TABLE = 'users';
+const ERROR_FETCHING = "Users can't be loaded!";
/**
* Create user to the server
@@ -19,26 +23,26 @@ import { errorMsg } from '@helper/helper';
* @returns The IResponse object if success or null
*/
const createUser = async (user: IUser): Promise>> => {
- try {
- const response = await sendRequest(
- USER_PATH,
- 'POST',
- JSON.stringify(user)
- );
+ // try {
+ // const response = await sendRequest(
+ // USER_PATH,
+ // 'POST',
+ // JSON.stringify(user)
+ // );
- if (response.statusCode !== STATUS_CODE.CREATE) {
- throw new Error(errorMsg(response.statusCode, response.msg));
- }
+ // if (response.statusCode !== STATUS_CODE.CREATE) {
+ // throw new Error(errorMsg(response.statusCode, response.msg));
+ // }
- return response;
- } catch (error: unknown) {
- if (error instanceof Error) {
- toast.error(error.message);
- }
- }
+ // return response;
+ // } catch (error: unknown) {
+ // if (error instanceof Error) {
+ // toast.error(error.message);
+ // }
+ // }
return null;
-}
+};
/**
* Update the user to the server
@@ -46,23 +50,23 @@ const createUser = async (user: IUser): Promise>> => {
* @returns The IResponse object if update success or null
*/
const updateUser = async (user: IUser): Promise>> => {
- try {
- const response = await sendRequest(
- USER_PATH + '/' + user.id,
- 'PUT',
- JSON.stringify(user)
- );
+ // try {
+ // const response = await sendRequest(
+ // USER_PATH + '/' + user.id,
+ // 'PUT',
+ // JSON.stringify(user)
+ // );
- if (response.statusCode !== STATUS_CODE.OK) {
- throw new Error(errorMsg(response.statusCode, response.msg));
- }
+ // if (response.statusCode !== STATUS_CODE.OK) {
+ // throw new Error(errorMsg(response.statusCode, response.msg));
+ // }
- return response;
- } catch (error: unknown) {
- if (error instanceof Error) {
- toast.error(error.message);
- }
- }
+ // return response;
+ // } catch (error: unknown) {
+ // if (error instanceof Error) {
+ // toast.error(error.message);
+ // }
+ // }
return null;
};
@@ -72,17 +76,37 @@ const updateUser = async (user: IUser): Promise>> => {
* @param user The user need to be checkout
* @returns The IResponse object if checkout success or not
*/
-const checkOutUser = async (user: IUser): Promise>> => {
- const tempUser = user;
+const checkOutUser = async (
+ user: IUser
+): Promise>> => {
+ // const tempUser = user;
- if (tempUser) {
- tempUser.roomId = 0;
- const resUpdateUser = await updateUser(tempUser);
+ // if (tempUser) {
+ // tempUser.roomId = 0;
+ // const resUpdateUser = await updateUser(tempUser);
- return resUpdateUser;
- }
+ // return resUpdateUser;
+ // }
return null;
};
-export { updateUser, checkOutUser, createUser };
+const getAllUsers = async (
+ sortBy: string,
+ orderBy: string,
+ phoneSearch: string
+): Promise => {
+ const { data, error } = await supabase
+ .from(USERS_TABLE)
+ .select('*')
+ .order(sortBy, { ascending: orderBy === 'asc' })
+ .like('phone', `%${phoneSearch}%`);
+
+ if (error) {
+ throw new Error(ERROR_FETCHING);
+ }
+
+ return data;
+};
+
+export { updateUser, checkOutUser, createUser, getAllUsers };
diff --git a/hotel-management/src/types/supabase.ts b/hotel-management/src/types/supabase.ts
new file mode 100644
index 0000000..b5203da
--- /dev/null
+++ b/hotel-management/src/types/supabase.ts
@@ -0,0 +1,110 @@
+export type Json =
+ | string
+ | number
+ | boolean
+ | null
+ | { [key: string]: Json | undefined }
+ | Json[]
+
+export interface Database {
+ public: {
+ Tables: {
+ bookings: {
+ Row: {
+ amount: number
+ endDate: string
+ id: number
+ roomId: number
+ startDate: string
+ status: boolean
+ userId: number
+ }
+ Insert: {
+ amount: number
+ endDate: string
+ id?: number
+ roomId: number
+ startDate: string
+ status: boolean
+ userId: number
+ }
+ Update: {
+ amount?: number
+ endDate?: string
+ id?: number
+ roomId?: number
+ startDate?: string
+ status?: boolean
+ userId?: number
+ }
+ Relationships: [
+ {
+ foreignKeyName: "bookings_roomId_fkey"
+ columns: ["roomId"]
+ isOneToOne: false
+ referencedRelation: "rooms"
+ referencedColumns: ["id"]
+ },
+ {
+ foreignKeyName: "bookings_userId_fkey"
+ columns: ["userId"]
+ isOneToOne: false
+ referencedRelation: "users"
+ referencedColumns: ["id"]
+ }
+ ]
+ }
+ rooms: {
+ Row: {
+ id: number
+ name: string
+ price: number
+ status: boolean
+ }
+ Insert: {
+ id?: number
+ name: string
+ price: number
+ status: boolean
+ }
+ Update: {
+ id?: number
+ name?: string
+ price?: number
+ status?: boolean
+ }
+ Relationships: []
+ }
+ users: {
+ Row: {
+ id: number
+ name: string
+ phone: string
+ }
+ Insert: {
+ id?: number
+ name: string
+ phone: string
+ }
+ Update: {
+ id?: number
+ name?: string
+ phone?: string
+ }
+ Relationships: []
+ }
+ }
+ Views: {
+ [_ in never]: never
+ }
+ Functions: {
+ [_ in never]: never
+ }
+ Enums: {
+ [_ in never]: never
+ }
+ CompositeTypes: {
+ [_ in never]: never
+ }
+ }
+}
diff --git a/hotel-management/src/types/users.ts b/hotel-management/src/types/users.ts
index 6d4a5b2..1725420 100644
--- a/hotel-management/src/types/users.ts
+++ b/hotel-management/src/types/users.ts
@@ -1,9 +1,7 @@
interface IUser {
id: number;
name: string;
- identifiedCode: string;
phone: string;
- roomId: number;
}
export type { IUser };