i want to display component once i click the view icon. Somehow i have to double click to display the component. Whenever I click the view icon it sets the isViewing to be true and the component gets displayed under the Table container. I declaired viewId and setViewId on App.js and set up useContext file so that i can use the state in different components
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';
import RemoveRedEyeOutlinedIcon from '@mui/icons-material/RemoveRedEyeOutlined';
import ClearOutlinedIcon from '@mui/icons-material/ClearOutlined';
import { useState, useEffect, useContext } from 'react';
import { useNavigate } from 'react-router-dom';
import { collection, deleteDoc, doc, onSnapshot } from 'firebase/firestore';
import { db } from '../../firebase';
import { Link } from 'react-router-dom';
import ViewTicket from '../viewTicket/ViewTicket';
import { UserContext } from '../../Contexts/UserContext';
const Tables = () => {
const { viewId, setViewId } = useContext(UserContext)
const [data, setData] = useState([]);
const [isViewing, setIsViewing] = useState(false);
useEffect(() => {
const unsub = onSnapshot(collection(db, "invoices"), (snapshot) => {
let list = [];
snapshot.docs.forEach((doc) => {
list.push({ id: doc.id, ...doc.data() });
})
setData(list)
}, (err) => {
console.log(err)
});
return () => {
unsub();
}
}, [])
const deleteData = async (id) => {
try {
await deleteDoc(doc(db, "invoices", id));
setData(data.filter((item) => item.id !== id));
} catch (err) {
console.log(err)
}
}
return (
<div className="conatainer">
<TableContainer component={Paper} className="table">
<Table sx={{ minWidth: 650 }} aria-label="simple table">
<TableHead>
<TableRow>
<TableCell className="tableCell">Subject</TableCell>
<TableCell className="tableCell">Fullname</TableCell>
<TableCell className="tableCell">Email</TableCell>
<TableCell className="tableCell">Date</TableCell>
<TableCell className="tableCell">Action</TableCell>
</TableRow>
</TableHead>
<TableBody>
{data.map((item) => (
<TableRow key={item.id}>
<TableCell className="tableCell">{item.subject}</TableCell>
<TableCell className="tableCell">{item.fullname}</TableCell>
<TableCell className="tableCell">{item.email}</TableCell>
<TableCell className="tableCell">
<RemoveRedEyeOutlinedIcon onClick={() => {
setViewId(item.id)
setIsViewing(true);
}} className="viewButton" />
<ClearOutlinedIcon className="deleteButton" onClick={() => deleteData(item.id)} />
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<div>
{!isViewing ? null :
<ViewTicket/>
}
</div>
</div>
)
}
export default Tables
this is what i have for component. It takes the viewId to get the document details from the firebase. Then it displays the subject and email.
import { useState, useEffect, useContext } from 'react';
import { doc, getDoc } from 'firebase/firestore';
import { db } from '../../firebase';
import { UserContext } from '../../Contexts/UserContext';
const ViewTicket = () => {
const { viewId, setViewId } = useContext(UserContext)
const [tempData, setTempData] = useState("");
useEffect(() => {
if (viewId) {
const getUserData = async () => {
const dataRef = doc(db, "invoices", viewId);
const docSnap = await getDoc(dataRef);
if (docSnap.exists()) {
setTempData(docSnap.data());
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}
viewId && getUserData();
} else {
console.log('id does not exist')
}
}, [])
return (
<div className="single">
<div className="singleContainer">
<div className="details">
<h1>{tempData.subject}</h1>
<p>{tempData.email}</p>
</div>
</div>
</div>
)
}
export default ViewTicket