Refactored user form to fetch data as appropriate. including user types and years for cohorts and populating the screen appropriately. Changed User add screen to use the user form.

This commit is contained in:
bobbert committed 2025-12-07 18:13:41 +00:00
1 parent fe6c389564
commit a64793ffb5
2 files changed
+36 -50

No files matched your search

+34 -48
View File
@@ -9,33 +9,29 @@ const defaultUser = {
UserFirstname: null,
UserLastname: null,
UserEmail: null,
UserRegistered: null,
UserLevel: null,
UserYearID: null,
UserUsertypeID: null,
UserYearID: 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';
defaultUser.UserID = Math.floor(100000 + Math.random() * 900000);
defaultUser.UserImageURL = 'https://images.freeimages.com/images/small-preview/cf5/cellphone-1313194.jpg';
const [years, , isYearsLoading] = useLoad(yearsEndpoint);
const [users, , isUsersLoading] = useLoad(userEndpoint);
const usertypesEndpoint = 'https://softwarehub.uk/unibase/api/usertypes';
const yearsEndpoint = 'https://softwarehub.uk/unibase/api/years';
// 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()
});
const [user, setUser] = useState(originalUser || defaultUser);
const [usertypes, , isUsertypesLoading] = useLoad(usertypesEndpoint);
const [years, , isYearsLoading] = useLoad(yearsEndpoint);
// Handlers -------------------
const handleChange = (field, value) => {
setUser({ ...user, [field]: value });
};
@@ -43,15 +39,19 @@ const UserForm = ({ originalUser, onSubmit, onCancel }) => {
const handleSubmit = () => onSubmit(user);
// View -----------------------
const submitLabel = originalUser ? 'Modify' : 'Add';
const submitIcon = originalUser ? <Icons.Edit /> : <Icons.Add />;
const levels = [
{ value: 4, label: 'Level 4' },
{ value: 5, label: 'Level 5' },
{ value: 6, label: 'Level 6' },
{ value: 7, label: 'Level 7' }
];
const usertypeOptions = usertypes.map((usertype) => ({
value: usertype.UsertypeID,
label: usertype.UsertypeName
}));
const yearOptions = years.map((year) => ({
value: year.YearID,
label: year.YearName
}));
return (
<Form
@@ -78,40 +78,26 @@ const UserForm = ({ originalUser, onSubmit, onCancel }) => {
onChange={(value) => handleChange('UserEmail', value)}
/>
<Form.InputSelect
label='User Level'
prompt='Select user level...'
options={levels}
value={user.UserLevel}
onChange={(value) => handleChange('UserLevel', value)}
/>
{isYearsLoading ? (
<Text>Loading Years...</Text>
) : (
<Form.InputSelect
label='Year/Cohort'
value={user.UserYearID}
onChange={(value) => handleChange('UserYearID', value)}
prompt='Select year/cohort...'
options={years}
/>
)}
{isUsersLoading ? (
<Text>Loading User Types...</Text>
) : (
<Form.InputSelect
label='User Type'
value={user.UserUsertypeID}
onChange={(value) => handleChange('UserUsertypeID', value)}
prompt='Select user type...'
options={users}
prompt={'Select user type...'}
options={usertypeOptions}
isLoading={isUsertypesLoading}
/>
<Form.InputSelect
label='Cohort Year'
value={user.UserYearID}
onChange={(value) => handleChange('UserYearID', value)}
prompt={'Select cohort year...'}
options={yearOptions}
isLoading={isYearsLoading}
/>
)}
<Form.InputText
label='Profile Image URL'
label='User Image URL'
value={user.UserImageURL}
onChange={(value) => handleChange('UserImageURL', value)}
/>
+2 -2
View File
@@ -1,6 +1,6 @@
import {StyleSheet, Text, TextInput, View} from "react-native";
import Screen from "../layout/Screen";
import ModuleForm from "../entity/modules/moduleForm";
import UserForm from "../entity/users/UserForm";
export const UserAddScreen = ({navigation, route}) => {
@@ -14,7 +14,7 @@ export const UserAddScreen = ({navigation, route}) => {
// View -----------------------
return (
<Screen style={styles.container}>
<ModuleForm onSubmit={onAdd} onCancel={handleCancel}/>
<UserForm onSubmit={onAdd} onCancel={handleCancel}/>
</Screen>
);
};