feat: add subscription plans. Add admin panel. Add password recovery
This commit is contained in:
60
apps/web/src/features/admin/AdminAccountDetailProvider.tsx
Normal file
60
apps/web/src/features/admin/AdminAccountDetailProvider.tsx
Normal file
@@ -0,0 +1,60 @@
|
||||
import type { AdminAccountDetail } from '@gruperly/shared'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import type { ReactNode } from 'react'
|
||||
import { createContext, useContext } from 'react'
|
||||
import { ApiError, getAdminAccount } from '../../lib/api'
|
||||
|
||||
type AdminAccountDetailContextValue = {
|
||||
account: AdminAccountDetail | null
|
||||
isPending: boolean
|
||||
isError: boolean
|
||||
isNotFound: boolean
|
||||
errorMessage: string | null
|
||||
retry: () => void
|
||||
}
|
||||
|
||||
const AdminAccountDetailContext = createContext<AdminAccountDetailContextValue | null>(null)
|
||||
|
||||
export function AdminAccountDetailProvider({
|
||||
accountId,
|
||||
children,
|
||||
}: {
|
||||
accountId: string
|
||||
children: ReactNode
|
||||
}) {
|
||||
const accountQuery = useQuery({
|
||||
queryKey: ['admin', 'accounts', 'detail', accountId],
|
||||
queryFn: () => getAdminAccount(accountId),
|
||||
})
|
||||
|
||||
const error = accountQuery.error
|
||||
const isNotFound = error instanceof ApiError && error.status === 404
|
||||
const errorMessage = accountQuery.isError
|
||||
? error instanceof ApiError
|
||||
? (error.problem?.detail ?? 'No pudimos cargar la cuenta.')
|
||||
: 'No pudimos cargar la cuenta.'
|
||||
: null
|
||||
|
||||
return (
|
||||
<AdminAccountDetailContext.Provider
|
||||
value={{
|
||||
account: accountQuery.data ?? null,
|
||||
isPending: accountQuery.isPending,
|
||||
isError: accountQuery.isError,
|
||||
isNotFound,
|
||||
errorMessage,
|
||||
retry: () => void accountQuery.refetch(),
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AdminAccountDetailContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export function useAdminAccountDetail() {
|
||||
const context = useContext(AdminAccountDetailContext)
|
||||
if (!context) {
|
||||
throw new Error('useAdminAccountDetail debe usarse dentro de <AdminAccountDetailProvider>')
|
||||
}
|
||||
return context
|
||||
}
|
||||
Reference in New Issue
Block a user