import React, { useContext, useEffect, useState } from 'react'; import { Dropdown } from 'react-bootstrap'; import { fetchData } from '../../fetching/Fetch.js'; import 'bootstrap/dist/css/bootstrap.min.css'; import { SERVER_URL } from '../../constants.js'; import { useNavigate } from 'react-router-dom'; import { UserContext } from '../../context/UserContext.jsx'; import { saveActiveStation } from '../../utils/activeStation.js'; const StationIntroPage = () => { const navigate = useNavigate(); const { user } = useContext(UserContext); const [branches, setBranches] = useState([]); const [selectedBranch, setSelectedBranch] = useState(null); const [stations, setStations] = useState([]); const [selectedStation, setSelectedStation] = useState(null); const [error, setError] = useState(''); const [loadingBranches, setLoadingBranches] = useState(false); const [loadingStations, setLoadingStations] = useState(false); const tenantCode = user?.tenantCode; const url = `${SERVER_URL}/api/v1/`; useEffect(() => { if (!tenantCode) { setBranches([]); setError('Missing tenant on your account.'); return; } setLoadingBranches(true); setError(''); fetchData(`${url}branches/${encodeURIComponent(tenantCode)}`, 'GET') .then(({ data, success }) => { if (!success) { setBranches([]); setError('Could not load branches for your tenant.'); return; } setBranches(Array.isArray(data) ? data : []); if (!data?.length) { setError(`No branches found for tenant ${tenantCode}.`); } }) .catch((err) => { console.error(err); setError('Could not load branches.'); }) .finally(() => setLoadingBranches(false)); }, [tenantCode]); function handleBranchSelect(branch) { setSelectedBranch(branch); setSelectedStation(null); setStations([]); setLoadingStations(true); setError(''); fetchData(`${url}stations/${encodeURIComponent(tenantCode)}/${branch.id}`, 'GET') .then(({ data, success }) => { if (!success) { setStations([]); setError('Could not load stations for this branch.'); return; } setStations(Array.isArray(data) ? data : []); if (!data?.length) { setError('No stations found for this branch.'); } }) .catch((err) => { console.error(err); setError('Could not load stations.'); }) .finally(() => setLoadingStations(false)); } function handleStationSelect(station) { setSelectedStation(station); saveActiveStation({ stationId: station.id, stationName: station.name, branchId: selectedBranch?.id, branchName: selectedBranch?.name, }); navigate(`/teller-queue/${station.id}`, { state: { station, branch: selectedBranch, }, }); } return (
Syarikat: {tenantCode}
) : null} {error ?{error}
: null}