Compare commits
No commits in common. "main" and "feat/admin-needs-pricing-and-bundle-edit" have entirely different histories.
main
...
feat/admin
25
src/App.tsx
25
src/App.tsx
|
|
@ -1,10 +1,9 @@
|
||||||
import React, { useState, useEffect, useMemo } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { Database, RefreshCw, RefreshCcw, Menu, Sparkles } from 'lucide-react';
|
import { Database, RefreshCw, RefreshCcw, Menu, Sparkles } from 'lucide-react';
|
||||||
import { API_BASE } from './services/api';
|
import { API_BASE } from './services/api';
|
||||||
import type { AdminTabType } from './types';
|
import type { AdminTabType } from './types';
|
||||||
import { normalizeCategory } from './utils/category';
|
import { normalizeCategory } from './utils/category';
|
||||||
import { isAdminAccount, partitionUserLists } from './utils/roles';
|
|
||||||
|
|
||||||
// Modular Components
|
// Modular Components
|
||||||
import { AdminSidebar } from './components/layout/AdminSidebar';
|
import { AdminSidebar } from './components/layout/AdminSidebar';
|
||||||
|
|
@ -57,7 +56,9 @@ export function extractErrorMessage(err: any, fallback: string = 'An error occur
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const [userToken, setUserToken] = useState<string | null>(localStorage.getItem('access_token'));
|
const [userToken, setUserToken] = useState<string | null>(localStorage.getItem('access_token'));
|
||||||
const [loggedInUser, setLoggedInUser] = useState<any>(null);
|
const [loggedInUser, setLoggedInUser] = useState<any>(null);
|
||||||
const isAdmin = isAdminAccount(loggedInUser);
|
const isAdmin = Boolean(
|
||||||
|
loggedInUser?.roles?.some((role: string) => role === 'super_admin' || role === 'finance_admin' || role === 'admin')
|
||||||
|
);
|
||||||
|
|
||||||
// Admin Login State
|
// Admin Login State
|
||||||
const [adminAuthLoading, setAdminAuthLoading] = useState(false);
|
const [adminAuthLoading, setAdminAuthLoading] = useState(false);
|
||||||
|
|
@ -186,10 +187,6 @@ export default function App() {
|
||||||
// Admins & Practitioners
|
// Admins & Practitioners
|
||||||
const [adminsList, setAdminsList] = useState<any[]>([]);
|
const [adminsList, setAdminsList] = useState<any[]>([]);
|
||||||
const [practitionersList, setPractitionersList] = useState<any[]>([]);
|
const [practitionersList, setPractitionersList] = useState<any[]>([]);
|
||||||
const { customers: customersList, adminAccounts: resolvedAdminsList } = useMemo(
|
|
||||||
() => partitionUserLists(practitionersList, adminsList),
|
|
||||||
[practitionersList, adminsList]
|
|
||||||
);
|
|
||||||
const [createAdminEmail, setCreateAdminEmail] = useState('');
|
const [createAdminEmail, setCreateAdminEmail] = useState('');
|
||||||
const [createAdminPassword, setCreateAdminPassword] = useState('');
|
const [createAdminPassword, setCreateAdminPassword] = useState('');
|
||||||
const [createAdminName, setCreateAdminName] = useState('');
|
const [createAdminName, setCreateAdminName] = useState('');
|
||||||
|
|
@ -1094,10 +1091,10 @@ export default function App() {
|
||||||
modules: adminModulesList.length,
|
modules: adminModulesList.length,
|
||||||
plans: adminPlansList.length,
|
plans: adminPlansList.length,
|
||||||
coupons: adminCouponsList.length,
|
coupons: adminCouponsList.length,
|
||||||
customers: customersList.length,
|
customers: practitionersList.length,
|
||||||
subscriptions: usersSubscriptions.length,
|
subscriptions: usersSubscriptions.length,
|
||||||
payments: rzpPayments.length,
|
payments: rzpPayments.length,
|
||||||
admins: resolvedAdminsList.length,
|
admins: adminsList.length,
|
||||||
}}
|
}}
|
||||||
isCollapsed={isSidebarCollapsed}
|
isCollapsed={isSidebarCollapsed}
|
||||||
onToggleCollapse={() => {
|
onToggleCollapse={() => {
|
||||||
|
|
@ -1180,9 +1177,9 @@ export default function App() {
|
||||||
{adminSubTab === 'overview' && (
|
{adminSubTab === 'overview' && (
|
||||||
<SyncOverview
|
<SyncOverview
|
||||||
stats={{
|
stats={{
|
||||||
totalUsers: customersList.length,
|
totalUsers: practitionersList.length + adminsList.length,
|
||||||
subscriptions: usersSubscriptions.length,
|
subscriptions: usersSubscriptions.length,
|
||||||
superAdmins: resolvedAdminsList.length,
|
superAdmins: adminsList.length,
|
||||||
payments: rzpPayments.length,
|
payments: rzpPayments.length,
|
||||||
}}
|
}}
|
||||||
adminLogs={adminLogs}
|
adminLogs={adminLogs}
|
||||||
|
|
@ -1249,7 +1246,7 @@ export default function App() {
|
||||||
|
|
||||||
{adminSubTab === 'customers' && (
|
{adminSubTab === 'customers' && (
|
||||||
<CustomersTab
|
<CustomersTab
|
||||||
customers={customersList}
|
customers={practitionersList}
|
||||||
currentPage={customersPage}
|
currentPage={customersPage}
|
||||||
perPage={customersPerPage}
|
perPage={customersPerPage}
|
||||||
onPageChange={setCustomersPage}
|
onPageChange={setCustomersPage}
|
||||||
|
|
@ -1288,7 +1285,7 @@ export default function App() {
|
||||||
|
|
||||||
{adminSubTab === 'admins' && (
|
{adminSubTab === 'admins' && (
|
||||||
<AdminsTab
|
<AdminsTab
|
||||||
admins={resolvedAdminsList}
|
admins={adminsList}
|
||||||
name={createAdminName}
|
name={createAdminName}
|
||||||
email={createAdminEmail}
|
email={createAdminEmail}
|
||||||
password={createAdminPassword}
|
password={createAdminPassword}
|
||||||
|
|
@ -1320,7 +1317,7 @@ export default function App() {
|
||||||
downloadingReport={reportDownloading}
|
downloadingReport={reportDownloading}
|
||||||
successMessage={reportExportSuccess}
|
successMessage={reportExportSuccess}
|
||||||
errorMessage={reportExportError}
|
errorMessage={reportExportError}
|
||||||
totalUsers={customersList.length}
|
totalUsers={practitionersList.length + adminsList.length}
|
||||||
totalPayments={rzpPayments.length}
|
totalPayments={rzpPayments.length}
|
||||||
totalSubscriptions={usersSubscriptions.length}
|
totalSubscriptions={usersSubscriptions.length}
|
||||||
totalCoupons={adminCouponsList.length}
|
totalCoupons={adminCouponsList.length}
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ export const ReportsExportTab: React.FC<ReportsExportTabProps> = ({
|
||||||
<Users className="h-5 w-5" />
|
<Users className="h-5 w-5" />
|
||||||
</div>
|
</div>
|
||||||
<span className="text-[10px] font-bold px-2.5 py-1 rounded-full bg-indigo-50 text-indigo-700 border border-indigo-100">
|
<span className="text-[10px] font-bold px-2.5 py-1 rounded-full bg-indigo-50 text-indigo-700 border border-indigo-100">
|
||||||
{totalUsers} Total Customers
|
{totalUsers} Total Users
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ export const SyncOverview: React.FC<SyncOverviewProps> = ({
|
||||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||||
<div className="bg-gradient-to-br from-blue-50 to-indigo-50 border border-blue-200/60 rounded-2xl p-4 text-center">
|
<div className="bg-gradient-to-br from-blue-50 to-indigo-50 border border-blue-200/60 rounded-2xl p-4 text-center">
|
||||||
<p className="text-2xl font-black text-indigo-600">{stats.totalUsers}</p>
|
<p className="text-2xl font-black text-indigo-600">{stats.totalUsers}</p>
|
||||||
<p className="text-[10px] font-bold text-slate-500 uppercase tracking-wider mt-1">Total Customers</p>
|
<p className="text-[10px] font-bold text-slate-500 uppercase tracking-wider mt-1">Total Users</p>
|
||||||
</div>
|
</div>
|
||||||
<div className="bg-gradient-to-br from-purple-50 to-pink-50 border border-purple-200/60 rounded-2xl p-4 text-center">
|
<div className="bg-gradient-to-br from-purple-50 to-pink-50 border border-purple-200/60 rounded-2xl p-4 text-center">
|
||||||
<p className="text-2xl font-black text-purple-600">{stats.subscriptions}</p>
|
<p className="text-2xl font-black text-purple-600">{stats.subscriptions}</p>
|
||||||
|
|
|
||||||
|
|
@ -1,45 +0,0 @@
|
||||||
export const ADMIN_ROLES = new Set(['super_admin', 'admin', 'finance_admin']);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns true when a user object carries at least one administrative role.
|
|
||||||
*/
|
|
||||||
export function isAdminAccount(user?: { roles?: string[] } | null): boolean {
|
|
||||||
if (!Array.isArray(user?.roles)) return false;
|
|
||||||
return user.roles.some((role) => ADMIN_ROLES.has(String(role).trim().toLowerCase()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Separates customer accounts from admin accounts so admins never leak into customer counts.
|
|
||||||
*/
|
|
||||||
export function partitionUserLists<T extends { id?: string; email?: string; roles?: string[] }>(
|
|
||||||
practitioners: T[],
|
|
||||||
admins: T[]
|
|
||||||
): { customers: T[]; adminAccounts: T[] } {
|
|
||||||
const customers: T[] = [];
|
|
||||||
const adminAccounts: T[] = [];
|
|
||||||
const seenAdmins = new Set<string>();
|
|
||||||
|
|
||||||
for (const admin of admins) {
|
|
||||||
const key = String(admin.id || admin.email || '').toLowerCase();
|
|
||||||
if (key) {
|
|
||||||
if (seenAdmins.has(key)) continue;
|
|
||||||
seenAdmins.add(key);
|
|
||||||
}
|
|
||||||
adminAccounts.push(admin);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const user of practitioners) {
|
|
||||||
if (isAdminAccount(user)) {
|
|
||||||
const key = String(user.id || user.email || '').toLowerCase();
|
|
||||||
if (!key || !seenAdmins.has(key)) {
|
|
||||||
if (key) seenAdmins.add(key);
|
|
||||||
adminAccounts.push(user);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
customers.push(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return { customers, adminAccounts };
|
|
||||||
}
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue