mirror of
https://github.com/mudabbir-ahmad/Project-MAD-crudler.git
synced 2026-10-08 04:40:20 +00:00
Added post and put and delete functionality both to user and modules, also updated users so that it by default has the user set to registered as not registered, but gives a dropdown allowing a person to select whether they are registered or not
This commit is contained in:
1 parent
63287d4a46
commit
80efb0be54
3 files changed
+60
-39
No files matched your search
@@ -13,6 +13,7 @@ const defaultUser = {
|
||||
UserYearID: null,
|
||||
UserLevel: null,
|
||||
UserImageURL: null,
|
||||
UserRegistered: null,
|
||||
};
|
||||
|
||||
const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
||||
@@ -20,6 +21,7 @@ const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
||||
|
||||
defaultUser.UserID = Math.floor(100000 + Math.random() * 900000);
|
||||
defaultUser.UserImageURL = 'https://images.generated.photos/tZmcC6WnzFmgAwRMsBnGYcmaj_NSktxhYzBfTqej_Lo/rs:fit:256:256/czM6Ly9pY29uczgu/Z3Bob3Rvcy1wcm9k/LnBob3Rvcy92M18w/MDE3MjYzLmpwZw.jpg';
|
||||
defaultUser.UserRegistered = 0;
|
||||
|
||||
const userTypesEndpoint = 'https://softwarehub.uk/unibase/api/usertypes';
|
||||
const yearsEndpoint = 'https://softwarehub.uk/unibase/api/years';
|
||||
@@ -118,6 +120,14 @@ const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
||||
value={user.UserImageURL}
|
||||
onChange={(value) => handleChange('UserImageURL', value)}
|
||||
/>
|
||||
|
||||
<Form.InputSelect
|
||||
label='Registered'
|
||||
value={user.UserRegistered}
|
||||
onChange={(value) => handleChange('UserRegistered', value)}
|
||||
prompt={'Select registered status...'}
|
||||
options={registeredOptions}
|
||||
/>
|
||||
</Form>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
// import {useState, useEffect} from "react";
|
||||
import {LogBox, Text} from "react-native";
|
||||
import {Alert, LogBox, Text} from "react-native";
|
||||
import Screen from "../layout/Screen";
|
||||
import ModuleList from "../entity/modules/ModuleList";
|
||||
import {Button, ButtonTray} from "../UI/Button";
|
||||
import Icons from "../UI/Icons";
|
||||
import useLoad from "../API/useLoad";
|
||||
import API from "../API/API";
|
||||
|
||||
|
||||
const ModuleListScreen = ({navigation}) => {
|
||||
@@ -16,40 +17,49 @@ const ModuleListScreen = ({navigation}) => {
|
||||
|
||||
// State ----------------------
|
||||
|
||||
const [modules, setModules, isLoading, loadModules] = useLoad(modulesEndpoint);
|
||||
const [modules, isLoading, loadModules] = useLoad(modulesEndpoint);
|
||||
|
||||
// Handlers -------------------
|
||||
|
||||
const handleDelete = (module) =>
|
||||
setModules(modules.filter((item) => item.ModuleID !== module.ModuleID));
|
||||
|
||||
|
||||
const onDelete = (module) => {
|
||||
handleDelete(module);
|
||||
navigation.goBack();
|
||||
const onDelete = async (module) => {
|
||||
const deleteEndpoint = `${modulesEndpoint}/${module.ModuleID}`;
|
||||
const result = await API.delete(deleteEndpoint, module);
|
||||
if (result.isSuccess) {
|
||||
loadModules(modulesEndpoint)
|
||||
navigation.goBack();
|
||||
} else
|
||||
Alert.alert(result.message);
|
||||
}
|
||||
|
||||
const handleAdd = (module) => setModules([...modules, module]);
|
||||
// This code is the old handleAdd which added to local state.
|
||||
// const handleAdd = (module) => setModules([...modules, module]);
|
||||
|
||||
const handleModify = (updatedModule) => setModules(
|
||||
modules.map((module) => (module.ModuleID === updatedModule.ModuleID) ? updatedModule : module),
|
||||
);
|
||||
|
||||
const onAdd = (module) => {
|
||||
handleAdd(module);
|
||||
navigation.goBack();
|
||||
//new handleAdd which posts to the API
|
||||
const handleAdd = async (module) => {
|
||||
const result = await API.post(modulesEndpoint, module);
|
||||
if (result.isSuccess) {
|
||||
loadModules(modulesEndpoint)
|
||||
navigation.goBack();
|
||||
} else
|
||||
Alert.alert(result.message);
|
||||
};
|
||||
|
||||
const onModify = (module) => {
|
||||
handleModify(module);
|
||||
navigation.replace('ModuleViewScreen', {module, onDelete, onModify});
|
||||
const onModify = async (module) => {
|
||||
const putEndpoint = `${modulesEndpoint}/${module.ModuleID}`;
|
||||
const result = await API.put(putEndpoint, module);
|
||||
if ( result.isSuccess ) {
|
||||
loadModules(modulesEndpoint)
|
||||
navigation.navigate('ModuleListScreen');
|
||||
} else
|
||||
Alert.alert(result.message);
|
||||
};
|
||||
|
||||
|
||||
const goToViewScreen = (module) =>
|
||||
navigation.navigate('ModuleViewScreen', {module, onDelete, onModify});
|
||||
navigation.navigate('ModuleViewScreen', {module, onDelete, onModify });
|
||||
|
||||
const goToAddScreen = () =>
|
||||
navigation.navigate('ModuleAddScreen', {onAdd});
|
||||
navigation.navigate('ModuleAddScreen', {onAdd: handleAdd});
|
||||
|
||||
|
||||
// View -----------------------
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import {LogBox, Text} from "react-native";
|
||||
import {Alert, LogBox, Text} from "react-native";
|
||||
import Screen from "../layout/Screen";
|
||||
import UserList from "../entity/users/UserList";
|
||||
import {Button, ButtonTray} from "../UI/Button";
|
||||
import Icons from "../UI/Icons";
|
||||
import useLoad from "../API/useLoad";
|
||||
import API from "../API/API";
|
||||
|
||||
const UserListScreen = ({navigation}) => {
|
||||
// Initialisation -------------
|
||||
@@ -23,31 +24,31 @@ const UserListScreen = ({navigation}) => {
|
||||
navigation.goBack();
|
||||
};
|
||||
|
||||
const handleAdd = (user) => setUsers([...users, user]);
|
||||
|
||||
const handleModify = (updatedUser) => setUsers(
|
||||
users.map((user) => (user.UserID === updatedUser.UserID) ? updatedUser : user),
|
||||
);
|
||||
|
||||
const onAdd = (user) => {
|
||||
handleAdd(user);
|
||||
navigation.goBack();
|
||||
const handleAdd = async (user) => {
|
||||
const result = await API.post(usersEndpoint, user);
|
||||
if (result.isSuccess) {
|
||||
await loadUsers(usersEndpoint)
|
||||
navigation.goBack();
|
||||
} else
|
||||
Alert.alert(result.message);
|
||||
};
|
||||
|
||||
const onModify = (user) => {
|
||||
handleModify(user);
|
||||
navigation.replace('UserViewScreen', {user, onDelete, onModify});
|
||||
const onModify = async (user) => {
|
||||
const putEndpoint = `${usersEndpoint}/${user.UserID}`;
|
||||
const result = await API.put(putEndpoint, user);
|
||||
if ( result.isSuccess ) {
|
||||
await loadUsers(usersEndpoint)
|
||||
navigation.navigate('UserListScreen');
|
||||
} else
|
||||
Alert.alert(result.message);
|
||||
};
|
||||
|
||||
const goToViewScreen = (user) =>
|
||||
navigation.navigate('UserViewScreen', {user, onDelete, onModify});
|
||||
|
||||
const goToAddScreen = () =>
|
||||
navigation.navigate('UserAddScreen', {onAdd});
|
||||
navigation.navigate('UserAddScreen', {onAdd: handleAdd});
|
||||
|
||||
const handleSwitchToModules = () => {
|
||||
navigation.navigate("ModuleListScreen");
|
||||
};
|
||||
|
||||
// View -----------------------
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user