I can log the data by clicking the button on the browser's console but the session data doesn't get rendered to the component. and I have added console logs inside the return statement to check whether if the state resets it seems not. and when the snapshot is received it adds the session to previous sessions rather than adding only new ones
const AssignedInterviews = () => {
const [sessions, setSessions] = useState([]);
const [loading, setLoading] = useState(true);
const { user } = useAuth();
const getSessionData = async (sessionRef) => {
const session = await sessionRef.data();
const panelRef = await db.collection("panels").doc(session.panel_id).get();
const panel = await panelRef.data();
const companyRef = await db
.collection("companies")
.doc(panel.company_id)
.get();
const company = await companyRef.data();
const interviews = await db
.collection("interviews")
.where("session_id", "==", sessionRef.id)
.get();
const panel_no = panel.panel_no;
const company_name = company.name;
const company_logo = company.photoUrl;
const interview_schedule = session.start_time;
return {
id: sessionRef.id,
panel_no,
company_name,
company_logo,
queue_length: interviews.size,
interview_schedule,
};
};
const fetchSessions = () => {
try {
db.collection("sessions")
.where("assigned_students", "array-contains", user.uuid)
.orderBy("start_time")
.onSnapshot((snapshot) => {
console.log("Snaphot Recieved");
setLoading(true);
const new_sessions = [];
snapshot.forEach((sessionRef) => {
getSessionData(sessionRef)
.then((new_session) => {
new_sessions.push(new_session);
})
.catch((e) => {
console.log(e);
});
});
setLoading(false);
setSessions(new_sessions);
});
} catch (error) {
console.log(error);
}
};
useEffect(() => {
fetchSessions();
}, []);
return (
<Flex
flexDirection='column'
mt={5}
width='100%'
p={2}
overflow='scroll'
maxHeight={500}>
<Heading size='md' mb={5}>
Your Assigned Interviews
</Heading>
{!loading ? (
sessions.map((session) => {
return <InterviewCard key={session.id} data={session} />;
})
) : (
<Center>
<Spinner />
</Center>
)}
<Button
onClick={() => {
console.log(sessions);
}}>
{JSON.stringify(sessions)}
</Button>
</Flex>
);
};