mirror of https://gitlab.com/ceda_ei/sonzai.git
Disallow removing used subjects
Pass timetable as prop in containers.
This commit is contained in:
parent
f9f4f40822
commit
b89b67bf68
|
@ -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,
|
||||||
|
|
|
@ -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,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue