chore: organize documentation, fix infinite loops in hooks, and sync package.json
This commit is contained in:
parent
8c8df1b2b8
commit
38a76c2076
7 changed files with 101 additions and 52 deletions
4
frontend/package-lock.json
generated
4
frontend/package-lock.json
generated
|
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"name": "frontend",
|
"name": "marketplace-front",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "frontend",
|
"name": "marketplace-front",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@heroicons/react": "^2.2.0",
|
"@heroicons/react": "^2.2.0",
|
||||||
|
|
|
||||||
|
|
@ -1,51 +1,42 @@
|
||||||
{
|
{
|
||||||
"name": "marketplace-front",
|
"name": "marketplace-front",
|
||||||
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.0.0",
|
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "next dev --turbopack",
|
||||||
"build": "tsc -b && vite build",
|
"build": "next build",
|
||||||
"preview": "vite preview",
|
"start": "next start",
|
||||||
"test": "vitest",
|
"lint": "next lint"
|
||||||
"test:run": "vitest run",
|
|
||||||
"test:coverage": "vitest run --coverage",
|
|
||||||
"e2e": "playwright test",
|
|
||||||
"e2e:ui": "playwright test --ui",
|
|
||||||
"e2e:headed": "playwright test --headed",
|
|
||||||
"e2e:debug": "playwright test --debug"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@heroicons/react": "^2.2.0",
|
||||||
"@mercadopago/sdk-react": "^1.0.6",
|
"@mercadopago/sdk-react": "^1.0.6",
|
||||||
"@types/leaflet": "^1.9.21",
|
"@react-email/render": "^1.4.0",
|
||||||
"axios": "^1.7.4",
|
"@types/nodemailer": "^7.0.3",
|
||||||
"leaflet": "^1.9.4",
|
"appwrite": "^18.1.1",
|
||||||
"lucide-react": "^0.562.0",
|
"axios": "^1.12.2",
|
||||||
"react": "^18.3.1",
|
"chart.js": "^4.5.0",
|
||||||
"react-dom": "^18.3.1",
|
"dotenv": "^17.2.2",
|
||||||
"react-icons": "^5.5.0",
|
"lucide-react": "^0.539.0",
|
||||||
"react-leaflet": "^4.2.1",
|
"next": "15.3.4",
|
||||||
"react-router-dom": "^6.26.0",
|
"node-appwrite": "^17.2.0",
|
||||||
"react-window": "^1.8.10",
|
"nodemailer": "^7.0.10",
|
||||||
"zustand": "^4.5.5"
|
"react": "^19.0.0",
|
||||||
|
"react-chartjs-2": "^5.3.0",
|
||||||
|
"react-dom": "^19.0.0",
|
||||||
|
"react-hot-toast": "^2.6.0",
|
||||||
|
"resend": "^6.4.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/jest-dom": "^6.9.1",
|
"@eslint/eslintrc": "^3",
|
||||||
"@testing-library/react": "^16.3.1",
|
"@tailwindcss/postcss": "^4",
|
||||||
"@testing-library/user-event": "^14.6.1",
|
"@types/node": "^20",
|
||||||
"@types/node": "^22.0.0",
|
"@types/react": "^19",
|
||||||
"@types/react": "^18.3.7",
|
"@types/react-chartjs-2": "^2.0.2",
|
||||||
"@types/react-dom": "^18.3.0",
|
"@types/react-dom": "^19",
|
||||||
"@types/react-window": "^1.8.8",
|
"eslint": "^9",
|
||||||
"@vitejs/plugin-react": "^4.7.0",
|
"eslint-config-next": "15.3.4",
|
||||||
"@vitest/coverage-v8": "^4.0.16",
|
"tailwindcss": "^4",
|
||||||
"autoprefixer": "^10.4.20",
|
"typescript": "^5"
|
||||||
"jsdom": "^27.3.0",
|
|
||||||
"postcss": "^8.4.47",
|
|
||||||
"tailwindcss": "^3.4.10",
|
|
||||||
"typescript": "^5.6.2",
|
|
||||||
"vite": "^5.4.3",
|
|
||||||
"vitest": "^4.0.16",
|
|
||||||
"@playwright/test": "^1.49.0"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -7,11 +7,27 @@ interface ProtectedRouteProps {
|
||||||
allowedRoles?: UserRole[]
|
allowedRoles?: UserRole[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getFallbackRoute = (role?: UserRole) => {
|
||||||
|
switch (role) {
|
||||||
|
case 'admin':
|
||||||
|
return '/dashboard'
|
||||||
|
case 'owner':
|
||||||
|
case 'seller':
|
||||||
|
return '/seller'
|
||||||
|
case 'employee':
|
||||||
|
return '/colaborador'
|
||||||
|
case 'delivery':
|
||||||
|
return '/entregas'
|
||||||
|
default:
|
||||||
|
return '/login'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function ProtectedRoute({ children, allowedRoles }: ProtectedRouteProps) {
|
export function ProtectedRoute({ children, allowedRoles }: ProtectedRouteProps) {
|
||||||
const { user, loading } = useAuth()
|
const { user, loading } = useAuth()
|
||||||
|
|
||||||
if (loading) {
|
if (loading) {
|
||||||
return <div className="p-6 text-center">Carregando sessão...</div>
|
return <div className="p-6 text-center">Carregando sessao...</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
|
|
@ -19,7 +35,7 @@ export function ProtectedRoute({ children, allowedRoles }: ProtectedRouteProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (allowedRoles && !allowedRoles.includes(user.role)) {
|
if (allowedRoles && !allowedRoles.includes(user.role)) {
|
||||||
return <Navigate to="/dashboard" replace />
|
return <Navigate to={getFallbackRoute(user.role)} replace />
|
||||||
}
|
}
|
||||||
|
|
||||||
return <>{children}</>
|
return <>{children}</>
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { createContext, ReactNode, useContext, useEffect, useMemo, useState } fr
|
||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
import { apiClient } from '../services/apiClient'
|
import { apiClient } from '../services/apiClient'
|
||||||
import { authService } from '../services/auth'
|
import { authService } from '../services/auth'
|
||||||
|
import { decodeJwtPayload } from '../utils/jwt'
|
||||||
|
|
||||||
export type UserRole = 'admin' | 'owner' | 'employee' | 'delivery' | 'seller' | 'customer'
|
export type UserRole = 'admin' | 'owner' | 'employee' | 'delivery' | 'seller' | 'customer'
|
||||||
|
|
||||||
|
|
@ -27,14 +28,49 @@ const AuthContext = createContext<AuthContextValue | undefined>(undefined)
|
||||||
|
|
||||||
const AUTH_KEY = 'mp-auth-user'
|
const AUTH_KEY = 'mp-auth-user'
|
||||||
|
|
||||||
|
const normalizeRole = (role?: string): UserRole => {
|
||||||
|
const normalizedRole = role?.toLowerCase()
|
||||||
|
|
||||||
|
if (normalizedRole?.includes('admin')) {
|
||||||
|
return 'admin'
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (normalizedRole) {
|
||||||
|
case 'admin':
|
||||||
|
return 'admin'
|
||||||
|
case 'owner':
|
||||||
|
case 'dono':
|
||||||
|
return 'owner'
|
||||||
|
case 'employee':
|
||||||
|
case 'colaborador':
|
||||||
|
return 'employee'
|
||||||
|
case 'delivery':
|
||||||
|
case 'entregador':
|
||||||
|
return 'delivery'
|
||||||
|
case 'customer':
|
||||||
|
return 'customer'
|
||||||
|
case 'seller':
|
||||||
|
default:
|
||||||
|
return 'seller'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
const [user, setUser] = useState<AuthUser | null>(() => {
|
const [user, setUser] = useState<AuthUser | null>(() => {
|
||||||
const persisted = localStorage.getItem(AUTH_KEY)
|
const persisted = localStorage.getItem(AUTH_KEY)
|
||||||
const parsed = persisted ? (JSON.parse(persisted) as AuthUser) : null
|
const parsed = persisted ? (JSON.parse(persisted) as AuthUser) : null
|
||||||
if (parsed?.token) {
|
const payload = decodeJwtPayload<{ role?: string; company_id?: string }>(parsed?.token)
|
||||||
apiClient.setToken(parsed.token)
|
const normalized = parsed
|
||||||
|
? {
|
||||||
|
...parsed,
|
||||||
|
role: normalizeRole(payload?.role ?? parsed.role),
|
||||||
|
companyId: parsed.companyId ?? payload?.company_id,
|
||||||
|
}
|
||||||
|
: null
|
||||||
|
if (normalized?.token) {
|
||||||
|
apiClient.setToken(normalized.token)
|
||||||
}
|
}
|
||||||
return parsed
|
return normalized
|
||||||
})
|
})
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const navigate = useNavigate()
|
const navigate = useNavigate()
|
||||||
|
|
@ -54,10 +90,12 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
}, [user])
|
}, [user])
|
||||||
|
|
||||||
const login = (token: string, role: UserRole, name: string, id: string, companyId?: string, email?: string, username?: string) => {
|
const login = (token: string, role: UserRole, name: string, id: string, companyId?: string, email?: string, username?: string) => {
|
||||||
setUser({ token, role, name, id, companyId, email, username })
|
const payload = decodeJwtPayload<{ role?: string; company_id?: string }>(token)
|
||||||
|
const normalizedRole = normalizeRole(payload?.role ?? role)
|
||||||
|
setUser({ token, role: normalizedRole, name, id, companyId: companyId ?? payload?.company_id, email, username })
|
||||||
|
|
||||||
// Redirect based on role
|
// Redirect based on role
|
||||||
switch (role) {
|
switch (normalizedRole) {
|
||||||
case 'admin':
|
case 'admin':
|
||||||
navigate('/dashboard', { replace: true })
|
navigate('/dashboard', { replace: true })
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ export const useUsuariosApi = (): UseUsuariosApiReturn => {
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
}, [currentPage]);
|
}, []);
|
||||||
|
|
||||||
const buscarUsuarioPorId = useCallback(async (id: string): Promise<UsuarioBff | null> => {
|
const buscarUsuarioPorId = useCallback(async (id: string): Promise<UsuarioBff | null> => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,11 @@ export function LoginPage() {
|
||||||
|
|
||||||
const resolveRole = (role?: string): UserRole => {
|
const resolveRole = (role?: string): UserRole => {
|
||||||
logger.info('🔐 [Login] Resolving role:', role)
|
logger.info('🔐 [Login] Resolving role:', role)
|
||||||
switch (role?.toLowerCase()) {
|
const normalizedRole = role?.toLowerCase()
|
||||||
|
|
||||||
|
if (normalizedRole?.includes('admin')) return 'admin'
|
||||||
|
|
||||||
|
switch (normalizedRole) {
|
||||||
case 'admin': return 'admin'
|
case 'admin': return 'admin'
|
||||||
case 'dono': return 'owner'
|
case 'dono': return 'owner'
|
||||||
case 'colaborador': return 'employee'
|
case 'colaborador': return 'employee'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue