import { Text } from "react-native"; import { useState } from "react"; import Icons from "../../UI/Icons"; import Form from "../../UI/Form"; import useLoad from "../../API/useLoad"; const defaultUser = { UserID: null, UserFirstname: null, UserLastname: null, UserEmail: null, UserRegistered: null, UserLevel: null, UserYearID: null, UserUsertypeID: null, UserImageURL: null, UserUsertypeName: null, UserYearName: null, }; const UserForm = ({ originalUser, onSubmit, onCancel }) => { // Initialisation ------------- const yearsEndpoint = 'https://softwarehub.uk/unibase/api/years'; const userEndpoint = 'https://softwarehub.uk/unibase/api/user'; const [years, , isYearsLoading] = useLoad(yearsEndpoint); const [users, , isUsersLoading] = useLoad(userEndpoint); // State ---------------------- const [user, setUser] = useState(originalUser || { ...defaultUser, UserID: Math.floor(100000 + Math.random() * 900000), UserImageURL: 'https://images.freeimages.com/images/small-preview/cf5/cellphone-1313194.jpg', UserRegistered: new Date().toISOString() }); // Handlers ------------------- const handleChange = (field, value) => { setUser({ ...user, [field]: value }); }; const handleSubmit = () => onSubmit(user); // View ----------------------- const submitLabel = originalUser ? 'Modify' : 'Add'; const submitIcon = originalUser ? : ; const levels = [ { value: 4, label: 'Level 4' }, { value: 5, label: 'Level 5' }, { value: 6, label: 'Level 6' }, { value: 7, label: 'Level 7' } ]; return (
handleChange('UserFirstname', value)} /> handleChange('UserLastname', value)} /> handleChange('UserEmail', value)} /> handleChange('UserLevel', value)} /> {isYearsLoading ? ( Loading Years... ) : ( handleChange('UserYearID', value)} prompt='Select year/cohort...' options={years} /> )} {isUsersLoading ? ( Loading User Types... ) : ( handleChange('UserUsertypeID', value)} prompt='Select user type...' options={users} /> )} handleChange('UserImageURL', value)} /> ); }; export default UserForm;