mirror of
https://github.com/mudabbir-ahmad/Project-MAD-crudler.git
synced 2026-10-07 20: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,
|
UserYearID: null,
|
||||||
UserLevel: null,
|
UserLevel: null,
|
||||||
UserImageURL: null,
|
UserImageURL: null,
|
||||||
|
UserRegistered: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
||||||
@@ -20,6 +21,7 @@ const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
|||||||
|
|
||||||
defaultUser.UserID = Math.floor(100000 + Math.random() * 900000);
|
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.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 userTypesEndpoint = 'https://softwarehub.uk/unibase/api/usertypes';
|
||||||
const yearsEndpoint = 'https://softwarehub.uk/unibase/api/years';
|
const yearsEndpoint = 'https://softwarehub.uk/unibase/api/years';
|
||||||
@@ -118,6 +120,14 @@ const UserForm = ({originalUser, onSubmit, onCancel}) => {
|
|||||||
value={user.UserImageURL}
|
value={user.UserImageURL}
|
||||||
onChange={(value) => handleChange('UserImageURL', value)}
|
onChange={(value) => handleChange('UserImageURL', value)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<Form.InputSelect
|
||||||
|
label='Registered'
|
||||||
|
value={user.UserRegistered}
|
||||||
|
onChange={(value) => handleChange('UserRegistered', value)}
|
||||||
|
prompt={'Select registered status...'}
|
||||||
|
options={registeredOptions}
|
||||||
|
/>
|
||||||
</Form>
|
</Form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
// import {useState, useEffect} from "react";
|
// import {useState, useEffect} from "react";
|
||||||
import {LogBox, Text} from "react-native";
|
import {Alert, LogBox, Text} from "react-native";
|
||||||
import Screen from "../layout/Screen";
|
import Screen from "../layout/Screen";
|
||||||
import ModuleList from "../entity/modules/ModuleList";
|
import ModuleList from "../entity/modules/ModuleList";
|
||||||
import {Button, ButtonTray} from "../UI/Button";
|
import {Button, ButtonTray} from "../UI/Button";
|
||||||
import Icons from "../UI/Icons";
|
import Icons from "../UI/Icons";
|
||||||
import useLoad from "../API/useLoad";
|
import useLoad from "../API/useLoad";
|
||||||
|
import API from "../API/API";
|
||||||
|
|
||||||
|
|
||||||
const ModuleListScreen = ({navigation}) => {
|
const ModuleListScreen = ({navigation}) => {
|
||||||
@@ -16,40 +17,49 @@ const ModuleListScreen = ({navigation}) => {
|
|||||||
|
|
||||||
// State ----------------------
|
// State ----------------------
|
||||||
|
|
||||||
const [modules, setModules, isLoading, loadModules] = useLoad(modulesEndpoint);
|
const [modules, isLoading, loadModules] = useLoad(modulesEndpoint);
|
||||||
|
|
||||||
// Handlers -------------------
|
// Handlers -------------------
|
||||||
|
|
||||||
const handleDelete = (module) =>
|
const onDelete = async (module) => {
|
||||||
setModules(modules.filter((item) => item.ModuleID !== module.ModuleID));
|
const deleteEndpoint = `${modulesEndpoint}/${module.ModuleID}`;
|
||||||
|
const result = await API.delete(deleteEndpoint, module);
|
||||||
|
if (result.isSuccess) {
|
||||||
const onDelete = (module) => {
|
loadModules(modulesEndpoint)
|
||||||
handleDelete(module);
|
navigation.goBack();
|
||||||
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(
|
//new handleAdd which posts to the API
|
||||||
modules.map((module) => (module.ModuleID === updatedModule.ModuleID) ? updatedModule : module),
|
const handleAdd = async (module) => {
|
||||||
);
|
const result = await API.post(modulesEndpoint, module);
|
||||||
|
if (result.isSuccess) {
|
||||||
const onAdd = (module) => {
|
loadModules(modulesEndpoint)
|
||||||
handleAdd(module);
|
navigation.goBack();
|
||||||
navigation.goBack();
|
} else
|
||||||
|
Alert.alert(result.message);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onModify = (module) => {
|
const onModify = async (module) => {
|
||||||
handleModify(module);
|
const putEndpoint = `${modulesEndpoint}/${module.ModuleID}`;
|
||||||
navigation.replace('ModuleViewScreen', {module, onDelete, onModify});
|
const result = await API.put(putEndpoint, module);
|
||||||
|
if ( result.isSuccess ) {
|
||||||
|
loadModules(modulesEndpoint)
|
||||||
|
navigation.navigate('ModuleListScreen');
|
||||||
|
} else
|
||||||
|
Alert.alert(result.message);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const goToViewScreen = (module) =>
|
const goToViewScreen = (module) =>
|
||||||
navigation.navigate('ModuleViewScreen', {module, onDelete, onModify});
|
navigation.navigate('ModuleViewScreen', {module, onDelete, onModify });
|
||||||
|
|
||||||
const goToAddScreen = () =>
|
const goToAddScreen = () =>
|
||||||
navigation.navigate('ModuleAddScreen', {onAdd});
|
navigation.navigate('ModuleAddScreen', {onAdd: handleAdd});
|
||||||
|
|
||||||
|
|
||||||
// View -----------------------
|
// View -----------------------
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import {LogBox, Text} from "react-native";
|
import {Alert, LogBox, Text} from "react-native";
|
||||||
import Screen from "../layout/Screen";
|
import Screen from "../layout/Screen";
|
||||||
import UserList from "../entity/users/UserList";
|
import UserList from "../entity/users/UserList";
|
||||||
import {Button, ButtonTray} from "../UI/Button";
|
import {Button, ButtonTray} from "../UI/Button";
|
||||||
import Icons from "../UI/Icons";
|
import Icons from "../UI/Icons";
|
||||||
import useLoad from "../API/useLoad";
|
import useLoad from "../API/useLoad";
|
||||||
|
import API from "../API/API";
|
||||||
|
|
||||||
const UserListScreen = ({navigation}) => {
|
const UserListScreen = ({navigation}) => {
|
||||||
// Initialisation -------------
|
// Initialisation -------------
|
||||||
@@ -23,31 +24,31 @@ const UserListScreen = ({navigation}) => {
|
|||||||
navigation.goBack();
|
navigation.goBack();
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAdd = (user) => setUsers([...users, user]);
|
const handleAdd = async (user) => {
|
||||||
|
const result = await API.post(usersEndpoint, user);
|
||||||
const handleModify = (updatedUser) => setUsers(
|
if (result.isSuccess) {
|
||||||
users.map((user) => (user.UserID === updatedUser.UserID) ? updatedUser : user),
|
await loadUsers(usersEndpoint)
|
||||||
);
|
navigation.goBack();
|
||||||
|
} else
|
||||||
const onAdd = (user) => {
|
Alert.alert(result.message);
|
||||||
handleAdd(user);
|
|
||||||
navigation.goBack();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onModify = (user) => {
|
const onModify = async (user) => {
|
||||||
handleModify(user);
|
const putEndpoint = `${usersEndpoint}/${user.UserID}`;
|
||||||
navigation.replace('UserViewScreen', {user, onDelete, onModify});
|
const result = await API.put(putEndpoint, user);
|
||||||
|
if ( result.isSuccess ) {
|
||||||
|
await loadUsers(usersEndpoint)
|
||||||
|
navigation.navigate('UserListScreen');
|
||||||
|
} else
|
||||||
|
Alert.alert(result.message);
|
||||||
};
|
};
|
||||||
|
|
||||||
const goToViewScreen = (user) =>
|
const goToViewScreen = (user) =>
|
||||||
navigation.navigate('UserViewScreen', {user, onDelete, onModify});
|
navigation.navigate('UserViewScreen', {user, onDelete, onModify});
|
||||||
|
|
||||||
const goToAddScreen = () =>
|
const goToAddScreen = () =>
|
||||||
navigation.navigate('UserAddScreen', {onAdd});
|
navigation.navigate('UserAddScreen', {onAdd: handleAdd});
|
||||||
|
|
||||||
const handleSwitchToModules = () => {
|
|
||||||
navigation.navigate("ModuleListScreen");
|
|
||||||
};
|
|
||||||
|
|
||||||
// View -----------------------
|
// View -----------------------
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in new issue
Block a user