feat: enhance login redirection and add loading state to tables
This commit is contained in:
parent
02cd9f91a4
commit
db20a23f6a
@ -4,10 +4,11 @@ import Form from "#/components/form/form"
|
|||||||
import { loginSchema } from "#/schema"
|
import { loginSchema } from "#/schema"
|
||||||
import { useMutation } from "@tanstack/react-query"
|
import { useMutation } from "@tanstack/react-query"
|
||||||
import { signIn } from "next-auth/react"
|
import { signIn } from "next-auth/react"
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
|
|
||||||
export default function LoginPage() {
|
export default function LoginPage() {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
const params = useSearchParams().get("redirect_to");
|
||||||
|
|
||||||
const mutation = useMutation({
|
const mutation = useMutation({
|
||||||
mutationKey: ['login'],
|
mutationKey: ['login'],
|
||||||
@ -26,7 +27,11 @@ export default function LoginPage() {
|
|||||||
console.error(errorMessage)
|
console.error(errorMessage)
|
||||||
throw new Error(result.error)
|
throw new Error(result.error)
|
||||||
} else {
|
} else {
|
||||||
router.push('/admin/home')
|
if (params) {
|
||||||
|
router.push(params);
|
||||||
|
} else {
|
||||||
|
router.push('/admin/home')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
@ -47,7 +52,7 @@ export default function LoginPage() {
|
|||||||
<div>
|
<div>
|
||||||
<Form
|
<Form
|
||||||
title="Connexion"
|
title="Connexion"
|
||||||
className="bg-white p-10 shadow-2xl w-3/4 lg:w-lg"
|
formClassName="bg-white p-10 shadow-2xl w-3/4 lg:w-lg"
|
||||||
fields={[
|
fields={[
|
||||||
{
|
{
|
||||||
label: "Email",
|
label: "Email",
|
||||||
@ -65,7 +70,7 @@ export default function LoginPage() {
|
|||||||
]}
|
]}
|
||||||
submit={mutation.mutate}
|
submit={mutation.mutate}
|
||||||
schema={loginSchema}
|
schema={loginSchema}
|
||||||
child={<button type="submit" className="btn-auth">Connexion</button>}
|
child={<button type="submit" className="btn-auth mt-4">Connexion</button>}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@ -298,6 +298,7 @@ export default function Admins() {
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
data={users || []}
|
data={users || []}
|
||||||
pageSize={5}
|
pageSize={5}
|
||||||
|
isDataLoading={isLoading}
|
||||||
header={(table) => {
|
header={(table) => {
|
||||||
const selectedIds = table
|
const selectedIds = table
|
||||||
.getRowModel()
|
.getRowModel()
|
||||||
|
|||||||
@ -187,6 +187,7 @@ export default function HomePage () {
|
|||||||
|
|
||||||
<Table
|
<Table
|
||||||
columns={columns}
|
columns={columns}
|
||||||
|
isDataLoading={isLoading}
|
||||||
data={companies || []}
|
data={companies || []}
|
||||||
pageSize={5}
|
pageSize={5}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -1,9 +1,24 @@
|
|||||||
import { ReactNode } from "react";
|
"use client"
|
||||||
|
|
||||||
|
import { ReactNode, useEffect } from "react";
|
||||||
import "../../assets/css/admin.css"
|
import "../../assets/css/admin.css"
|
||||||
import Sidebar from "../../components/admin/sidebar";
|
import Sidebar from "../../components/admin/sidebar";
|
||||||
import Header from "../../components/admin/adminHeader";
|
import Header from "../../components/admin/adminHeader";
|
||||||
|
import { signOutFunc } from "#/lib/function";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
import { useSession } from "next-auth/react";
|
||||||
|
|
||||||
export default function Dashboard({ children }: { children: ReactNode }) {
|
export default function Dashboard({ children }: { children: ReactNode }) {
|
||||||
|
const { status, data } = useSession();
|
||||||
|
const path = usePathname();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (status !== "loading") {
|
||||||
|
if (status === "unauthenticated" || (data && !data.user.access_token)) {
|
||||||
|
signOutFunc();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [data, status, path]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
||||||
|
|||||||
@ -309,6 +309,7 @@ export default function Organizations() {
|
|||||||
columns={columns}
|
columns={columns}
|
||||||
data={companies || []}
|
data={companies || []}
|
||||||
pageSize={5}
|
pageSize={5}
|
||||||
|
isDataLoading={isLoading}
|
||||||
header={(table) => {
|
header={(table) => {
|
||||||
const selectedIds = table
|
const selectedIds = table
|
||||||
.getRowModel()
|
.getRowModel()
|
||||||
|
|||||||
@ -1,17 +1,18 @@
|
|||||||
"use client"
|
"use client"
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { icons } from "#/assets/icons"
|
import { icons } from "#/assets/icons"
|
||||||
|
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
import { DropdownMenu } from "radix-ui";
|
import { DropdownMenu } from "radix-ui";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import Theme from "./theme";
|
import Theme from "./theme";
|
||||||
|
import { signOutFunc } from "#/lib/function";
|
||||||
|
|
||||||
import ProfilePicture from "../../assets/icons/profile.svg"
|
import ProfilePicture from "../../assets/icons/profile.svg"
|
||||||
|
|
||||||
export default function AdminHeader() {
|
export default function AdminHeader() {
|
||||||
const [open, setOpen] = React.useState(false);
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<nav className="header r-flex-between px-[44px] py-[20px] ">
|
<nav className="header r-flex-between px-[44px] py-[20px] ">
|
||||||
@ -35,15 +36,15 @@ export default function AdminHeader() {
|
|||||||
</DropdownMenu.Trigger>
|
</DropdownMenu.Trigger>
|
||||||
|
|
||||||
<DropdownMenu.Portal>
|
<DropdownMenu.Portal>
|
||||||
<DropdownMenu.Content className="DropdownMenuContent dropdown-menu r-flex-column r-secondary" sideOffset={0} side="bottom" align="end" >
|
<DropdownMenu.Content className="DropdownMenuContent dropdown-menu r-flex-column r-secondary p-2" sideOffset={0} side="bottom" align="end" >
|
||||||
<DropdownMenu.Item className="DropdownMenuItem dropdown-item text-[14px]">
|
<DropdownMenu.Item className="DropdownMenuItem dropdown-item text-[14px] outline-none hover:bg-blue-100 rounded-md">
|
||||||
<Link href="#" className="d-flex items-start ">
|
<Link href="#" className="d-flex items-start ">
|
||||||
<Image src={icons.userIcon} alt="Profil" />
|
<Image src={icons.userIcon} alt="Profil" />
|
||||||
Profil
|
Profil
|
||||||
</Link>
|
</Link>
|
||||||
</DropdownMenu.Item>
|
</DropdownMenu.Item>
|
||||||
<DropdownMenu.Item className="DropdownMenuItem dropdown-item text-[14px]">
|
<DropdownMenu.Item className="DropdownMenuItem dropdown-item text-[14px] outline-none hover:bg-blue-100 rounded-md">
|
||||||
<Link href="#" className="d-flex items-start r-danger ">
|
<Link href="#" onClick={() => signOutFunc()} className="d-flex items-start r-danger ">
|
||||||
<Image src={icons.logoutRed} alt="Deconnexion" />
|
<Image src={icons.logoutRed} alt="Deconnexion" />
|
||||||
Déconnexion
|
Déconnexion
|
||||||
</Link>
|
</Link>
|
||||||
|
|||||||
@ -10,7 +10,8 @@ export default function Form({
|
|||||||
className,
|
className,
|
||||||
child,
|
child,
|
||||||
title,
|
title,
|
||||||
schema
|
schema,
|
||||||
|
formClassName
|
||||||
} : FormProps) {
|
} : FormProps) {
|
||||||
const [errors, setErrors] = useState<Record<string, string>>({});
|
const [errors, setErrors] = useState<Record<string, string>>({});
|
||||||
|
|
||||||
@ -42,7 +43,7 @@ export default function Form({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form onSubmit={handleSubmit}>
|
<form onSubmit={handleSubmit} className={formClassName}>
|
||||||
<div className="flex justify-center text-black">
|
<div className="flex justify-center text-black">
|
||||||
<p className="text-3xl font-bold">{title}</p>
|
<p className="text-3xl font-bold">{title}</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import {
|
|||||||
getFilteredRowModel,
|
getFilteredRowModel,
|
||||||
Table as TableType,
|
Table as TableType,
|
||||||
} from "@tanstack/react-table"
|
} from "@tanstack/react-table"
|
||||||
import { cloneElement, isValidElement, ReactNode, useEffect, useRef, useState } from "react";
|
import { ReactNode, useEffect, useRef, useState } from "react";
|
||||||
import { clsx, type ClassValue } from "clsx"
|
import { clsx, type ClassValue } from "clsx"
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import { icons } from "#/assets/icons";
|
import { icons } from "#/assets/icons";
|
||||||
@ -18,14 +18,16 @@ import { icons } from "#/assets/icons";
|
|||||||
columns: ColumnDef<TData, TValue>[]
|
columns: ColumnDef<TData, TValue>[]
|
||||||
data: TData[],
|
data: TData[],
|
||||||
pageSize?: number,
|
pageSize?: number,
|
||||||
header?: ReactNode | ((table: TableType<TData>) => ReactNode)
|
header?: ReactNode | ((table: TableType<TData>) => ReactNode),
|
||||||
|
isDataLoading?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Table<TData, TValue>({
|
export default function Table<TData, TValue>({
|
||||||
columns,
|
columns,
|
||||||
data,
|
data,
|
||||||
pageSize = 10,
|
pageSize = 10,
|
||||||
header
|
header,
|
||||||
|
isDataLoading = true
|
||||||
}: DataTableProps<TData, TValue>) {
|
}: DataTableProps<TData, TValue>) {
|
||||||
const [rowSelection, setRowSelection] = useState({})
|
const [rowSelection, setRowSelection] = useState({})
|
||||||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(
|
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>(
|
||||||
@ -101,6 +103,12 @@ export default function Table<TData, TValue>({
|
|||||||
<thead className="h-10">
|
<thead className="h-10">
|
||||||
{table.getHeaderGroups().map((headerGroup) => (
|
{table.getHeaderGroups().map((headerGroup) => (
|
||||||
<tr key={headerGroup.id} className="rounded-lg">
|
<tr key={headerGroup.id} className="rounded-lg">
|
||||||
|
{header
|
||||||
|
?
|
||||||
|
<th className="bg-[#E9F0FF] p-3 text-start first:rounded-tl-lg">
|
||||||
|
|
||||||
|
</th>
|
||||||
|
:
|
||||||
<th className="bg-[#E9F0FF] p-3 text-start first:rounded-tl-lg">
|
<th className="bg-[#E9F0FF] p-3 text-start first:rounded-tl-lg">
|
||||||
<input
|
<input
|
||||||
ref={headerCheckboxRef}
|
ref={headerCheckboxRef}
|
||||||
@ -109,6 +117,7 @@ export default function Table<TData, TValue>({
|
|||||||
type="checkbox" name="" id=""
|
type="checkbox" name="" id=""
|
||||||
/>
|
/>
|
||||||
</th>
|
</th>
|
||||||
|
}
|
||||||
{headerGroup.headers.map((header) => {
|
{headerGroup.headers.map((header) => {
|
||||||
return(
|
return(
|
||||||
<th key={header.id} className="bg-[#E9F0FF] p-3 text-start last:rounded-tr-lg">
|
<th key={header.id} className="bg-[#E9F0FF] p-3 text-start last:rounded-tr-lg">
|
||||||
@ -141,9 +150,17 @@ export default function Table<TData, TValue>({
|
|||||||
</tr>
|
</tr>
|
||||||
))
|
))
|
||||||
)
|
)
|
||||||
|
: isDataLoading ?
|
||||||
|
(
|
||||||
|
<tr>
|
||||||
|
<td colSpan={columns.length} className="h-20 text-center">
|
||||||
|
Chargement...
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
)
|
||||||
: (
|
: (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={columns.length}>
|
<td colSpan={columns.length} className="h-20 text-center">
|
||||||
Aucun résultats
|
Aucun résultats
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|||||||
11
src/lib/function.ts
Normal file
11
src/lib/function.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { signOut } from "next-auth/react";
|
||||||
|
|
||||||
|
export const signOutFunc = () => {
|
||||||
|
signOut({
|
||||||
|
callbackUrl: `/login?redirect_to=${
|
||||||
|
window.location.pathname === "/logout"
|
||||||
|
? "/home"
|
||||||
|
: window.location.pathname
|
||||||
|
}`,
|
||||||
|
});
|
||||||
|
};
|
||||||
@ -23,7 +23,8 @@ export interface FormProps {
|
|||||||
submit: (param: any) => unknown,
|
submit: (param: any) => unknown,
|
||||||
className?: string,
|
className?: string,
|
||||||
child: ReactNode,
|
child: ReactNode,
|
||||||
schema: ZodSchema
|
schema: ZodSchema,
|
||||||
|
formClassName?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface StatsType {
|
export interface StatsType {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user