Disallow removing used subjects

Pass timetable as prop in containers.
This commit is contained in:
Ceda EI 2020-03-31 14:36:55 +05:30
parent f9f4f40822
commit b89b67bf68
2 changed files with 25 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import {
Card, Card,
FAB, FAB,
Portal, Portal,
Snackbar,
Text, Text,
withTheme withTheme
} from "react-native-paper"; } from "react-native-paper";
@ -16,12 +17,21 @@ import {
import InputDialog from "./InputDialog"; import InputDialog from "./InputDialog";
function Subjects({ theme, subjects, addSubject, removeSubject }) { function Subjects({ theme, subjects, timetable, addSubject, removeSubject }) {
const [ showDialog, setShowDialog ] = useState(false); const [ showDialog, setShowDialog ] = useState(false);
const [ snackbar, setSnackbar ] = useState(false);
function onInput(text) { function onInput(text) {
addSubject(text); addSubject(text);
setShowDialog(false); setShowDialog(false);
} }
function deleteSubject(id) {
if (timetable.reduce((acc, val) => acc.concat(val), []).filter(i => i.sub_id === id).length){
setSnackbar(true);
setTimeout(() => setSnackbar(false), 2000);
return;
}
removeSubject(id);
}
return ( return (
<Portal.Host><ScrollView style={{ backgroundColor: theme.colors.background }}> <Portal.Host><ScrollView style={{ backgroundColor: theme.colors.background }}>
{subjects.length === 0 ? {subjects.length === 0 ?
@ -43,7 +53,7 @@ function Subjects({ theme, subjects, addSubject, removeSubject }) {
<Card.Title title={subject.name} /> <Card.Title title={subject.name} />
<Card.Actions style={{justifyContent: "flex-end"}}> <Card.Actions style={{justifyContent: "flex-end"}}>
<IconButton <IconButton
onPress={() => removeSubject(subject.id)} onPress={() => deleteSubject(subject.id)}
icon="delete" icon="delete"
color={theme.colors.primary} color={theme.colors.primary}
/> />
@ -63,6 +73,7 @@ function Subjects({ theme, subjects, addSubject, removeSubject }) {
large large
icon="plus" icon="plus"
onPress={() => setShowDialog(true)} onPress={() => setShowDialog(true)}
visible={!snackbar}
style={{ style={{
position: "absolute", position: "absolute",
margin: 16, margin: 16,
@ -70,6 +81,16 @@ function Subjects({ theme, subjects, addSubject, removeSubject }) {
bottom: 0, bottom: 0,
}} }}
/> />
<Snackbar
visible={snackbar}
onDismiss={() => setSnackbar(false)}
action={{
label: "Dismiss",
onPress: () => setSnackbar(false),
}}
>
Subject is in use in TimeTable.
</Snackbar>
</Portal> </Portal>
</ScrollView></Portal.Host> </ScrollView></Portal.Host>
); );
@ -77,6 +98,7 @@ function Subjects({ theme, subjects, addSubject, removeSubject }) {
Subjects.propTypes = { Subjects.propTypes = {
subjects: PropTypes.array, subjects: PropTypes.array,
timetable: PropTypes.array,
addSubject: PropTypes.func, addSubject: PropTypes.func,
removeSubject: PropTypes.func, removeSubject: PropTypes.func,
theme: PropTypes.object, theme: PropTypes.object,

View File

@ -5,6 +5,7 @@ import { addSubject, removeSubject } from "../actions";
const mapStateToProps = state => { const mapStateToProps = state => {
return { return {
subjects: state.subjects, subjects: state.subjects,
timetable: state.timetable,
}; };
}; };