feat: add auth and data providers with mock data hooks

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-16 14:34:23 +05:30
parent 15483c5542
commit dc68577477
5 changed files with 255 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';
export type Role = 'executive' | 'admin';
type User = {
name: string;
initials: string;
role: Role;
email: string;
};
type AuthContextType = {
user: User;
setRole: (role: Role) => void;
isAdmin: boolean;
isAuthenticated: boolean;
login: () => void;
logout: () => void;
};
const USERS: Record<Role, User> = {
admin: {
name: 'Admin User',
initials: 'AU',
role: 'admin',
email: 'admin@ramaiah.com',
},
executive: {
name: 'Sanjay M.',
initials: 'SM',
role: 'executive',
email: 'sanjay@ramaiah.com',
},
};
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export const useAuth = (): AuthContextType => {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
};
interface AuthProviderProps {
children: ReactNode;
}
export const AuthProvider = ({ children }: AuthProviderProps) => {
const [role, setRole] = useState<Role>('executive');
const [isAuthenticated, setIsAuthenticated] = useState(false);
const user = USERS[role];
const isAdmin = role === 'admin';
const login = () => setIsAuthenticated(true);
const logout = () => setIsAuthenticated(false);
return (
<AuthContext.Provider value={{ user, setRole, isAdmin, isAuthenticated, login, logout }}>
{children}
</AuthContext.Provider>
);
};

View File

@@ -0,0 +1,52 @@
import type { ReactNode } from 'react';
import { createContext, useContext, useState } from 'react';
import type { Lead, Campaign, Ad, LeadActivity, FollowUp, WhatsAppTemplate, Agent } from '@/types/entities';
import { mockLeads, mockCampaigns, mockAds, mockFollowUps, mockLeadActivities, mockTemplates, mockAgents } from '@/mocks';
type DataContextType = {
leads: Lead[];
campaigns: Campaign[];
ads: Ad[];
followUps: FollowUp[];
leadActivities: LeadActivity[];
templates: WhatsAppTemplate[];
agents: Agent[];
updateLead: (id: string, updates: Partial<Lead>) => void;
};
const DataContext = createContext<DataContextType | undefined>(undefined);
export const useData = (): DataContextType => {
const context = useContext(DataContext);
if (context === undefined) {
throw new Error('useData must be used within a DataProvider');
}
return context;
};
interface DataProviderProps {
children: ReactNode;
}
export const DataProvider = ({ children }: DataProviderProps) => {
const [leads, setLeads] = useState<Lead[]>(mockLeads);
const [campaigns] = useState<Campaign[]>(mockCampaigns);
const [ads] = useState<Ad[]>(mockAds);
const [followUps] = useState<FollowUp[]>(mockFollowUps);
const [leadActivities] = useState<LeadActivity[]>(mockLeadActivities);
const [templates] = useState<WhatsAppTemplate[]>(mockTemplates);
const [agents] = useState<Agent[]>(mockAgents);
const updateLead = (id: string, updates: Partial<Lead>) => {
setLeads((prev) => prev.map((lead) => (lead.id === id ? { ...lead, ...updates } : lead)));
};
return (
<DataContext.Provider value={{ leads, campaigns, ads, followUps, leadActivities, templates, agents, updateLead }}>
{children}
</DataContext.Provider>
);
};