UNPKG

naim-firebase-auth-wrapper

Version:

React components and hooks for Firebase Authentication and Firestore with Mantine UI

52 lines 2.13 kB
'use client'; import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useState } from 'react'; import { Select, Text, Loader } from '@mantine/core'; import { useAuth } from '../hooks/useAuth'; import { getUserOrganizations, getUserProfile, setCurrentOrganization } from '../services/firestore'; export const OrganizationSelector = ({ onSelect }) => { const { user } = useAuth(); const [organizations, setOrganizations] = useState([]); const [selectedOrg, setSelectedOrg] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { const fetchOrgs = async () => { if (!user) return; try { const orgs = await getUserOrganizations(user.uid); setOrganizations(orgs); // Set current org if available const userProfile = await getUserProfile(user.uid); if (userProfile?.currentOrganization) { setSelectedOrg(userProfile.currentOrganization); } else if (orgs.length > 0) { setSelectedOrg(orgs[0].id); } } catch (error) { console.error('Error fetching organizations:', error); } finally { setLoading(false); } }; fetchOrgs(); }, [user]); const handleOrgChange = async (orgId) => { if (!user) return; setSelectedOrg(orgId); await setCurrentOrganization(user.uid, orgId); onSelect?.(orgId); }; if (loading) { return _jsx(Loader, { size: "sm" }); } if (organizations.length === 0) { return (_jsx(Text, { c: "dimmed", children: "No organizations. Create one to get started." })); } return (_jsx(Select, { label: "Current Organization", placeholder: "Select organization", data: organizations.map(org => ({ value: org.id, label: org.name })), value: selectedOrg, onChange: (value) => value && handleOrgChange(value) })); }; //# sourceMappingURL=OrganizationSelector.js.map