import { Box, Stack, Table, TableBody, TableCell, TableHead, TableRow, TextField, Typography } from '@mui/material'; import { nanoid } from 'nanoid'; import { Dispatch, useEffect, useState } from 'react'; import { useFormState } from 'react-dom'; import { Profiles } from '../../../../data/types'; import { CreateProfileAction } from './actions/CreateProfileAction'; import { ProfilesAction } from './actions/ProfilesAction'; import ProfilesTableRow from './ProfilesTableRow'; interface ProfilesTableProps { selectedProfile: string | undefined; setSelectedProfile: Dispatch; } export default function ProfilesTable({ selectedProfile, setSelectedProfile }: ProfilesTableProps) { const [profiles, setProfiles] = useState([]); const [formKey, setFormKey] = useState(() => nanoid()); const [refreshKey, setRefreshKey] = useState(0); const [formState, formAction] = useFormState(CreateProfileAction, { clear: false }); useEffect(() => { const fetchProfiles = async () => { try { const profiles = await ProfilesAction(); setProfiles(profiles); } catch (error) { console.error("Couldn't fetch profiles."); } }; if (formState.clear) { setFormKey(nanoid()); } fetchProfiles(); }, [formState, refreshKey]); return ( Profiles
Name Delete {profiles && profiles.map((profile) => ( setRefreshKey((prevKey) => prevKey + 1)} /> ))}
); }