2020-03-16 16:01:05 +01:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import {
|
2020-03-20 15:23:06 +01:00
|
|
|
IconButton,
|
2020-03-16 16:01:05 +01:00
|
|
|
Card,
|
|
|
|
FAB,
|
2020-03-19 19:43:34 +01:00
|
|
|
Portal,
|
2020-03-31 11:06:55 +02:00
|
|
|
Snackbar,
|
2020-03-20 15:23:06 +01:00
|
|
|
Text,
|
|
|
|
withTheme
|
2020-03-16 16:01:05 +01:00
|
|
|
} from "react-native-paper";
|
|
|
|
|
|
|
|
import {
|
2020-03-19 19:43:34 +01:00
|
|
|
ScrollView,
|
2020-03-16 16:01:05 +01:00
|
|
|
StyleSheet
|
|
|
|
} from "react-native";
|
|
|
|
|
|
|
|
import InputDialog from "./InputDialog";
|
|
|
|
|
2020-03-31 11:06:55 +02:00
|
|
|
function Subjects({ theme, subjects, timetable, addSubject, removeSubject }) {
|
2020-03-16 16:01:05 +01:00
|
|
|
const [ showDialog, setShowDialog ] = useState(false);
|
2020-03-31 11:06:55 +02:00
|
|
|
const [ snackbar, setSnackbar ] = useState(false);
|
2020-03-16 16:01:05 +01:00
|
|
|
function onInput(text) {
|
|
|
|
addSubject(text);
|
|
|
|
setShowDialog(false);
|
|
|
|
}
|
2020-03-31 11:06:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2020-03-16 16:01:05 +01:00
|
|
|
return (
|
2020-03-28 13:48:49 +01:00
|
|
|
<Portal.Host><ScrollView style={{ backgroundColor: theme.colors.background }}>
|
2020-03-16 16:13:14 +01:00
|
|
|
{subjects.length === 0 ?
|
2020-03-19 19:43:34 +01:00
|
|
|
<Text style={style.text}>
|
|
|
|
No Subjects added. Press + to add a subject.
|
|
|
|
</Text>
|
2020-03-16 16:13:14 +01:00
|
|
|
: null
|
|
|
|
}
|
2020-03-19 19:43:34 +01:00
|
|
|
{subjects.map((subject, idx) => (
|
|
|
|
<Card
|
|
|
|
key={subject.id}
|
|
|
|
style={
|
|
|
|
[
|
|
|
|
style.card,
|
2020-03-21 09:04:18 +01:00
|
|
|
{marginBottom: idx === subjects.length - 1 ? 80 : 0 }
|
2020-03-19 19:43:34 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
>
|
2020-03-16 16:01:05 +01:00
|
|
|
<Card.Title title={subject.name} />
|
2020-03-20 15:23:06 +01:00
|
|
|
<Card.Actions style={{justifyContent: "flex-end"}}>
|
|
|
|
<IconButton
|
2020-03-31 11:06:55 +02:00
|
|
|
onPress={() => deleteSubject(subject.id)}
|
2020-03-20 15:23:06 +01:00
|
|
|
icon="delete"
|
|
|
|
color={theme.colors.primary}
|
|
|
|
/>
|
2020-03-16 16:01:05 +01:00
|
|
|
</Card.Actions>
|
|
|
|
</Card>
|
|
|
|
))}
|
|
|
|
<InputDialog
|
|
|
|
visible={showDialog}
|
|
|
|
onDismiss={() => setShowDialog(false)}
|
|
|
|
title="Enter Name of New Subject"
|
|
|
|
placeholder="Subject Name"
|
|
|
|
label="Subject Name"
|
|
|
|
onOK={onInput}
|
|
|
|
/>
|
2020-03-19 19:43:34 +01:00
|
|
|
<Portal>
|
|
|
|
<FAB
|
|
|
|
large
|
|
|
|
icon="plus"
|
|
|
|
onPress={() => setShowDialog(true)}
|
2020-03-31 11:06:55 +02:00
|
|
|
visible={!snackbar}
|
2020-03-19 19:43:34 +01:00
|
|
|
style={{
|
|
|
|
position: "absolute",
|
|
|
|
margin: 16,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
}}
|
|
|
|
/>
|
2020-03-31 11:06:55 +02:00
|
|
|
<Snackbar
|
|
|
|
visible={snackbar}
|
|
|
|
onDismiss={() => setSnackbar(false)}
|
|
|
|
action={{
|
|
|
|
label: "Dismiss",
|
|
|
|
onPress: () => setSnackbar(false),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Subject is in use in TimeTable.
|
|
|
|
</Snackbar>
|
2020-03-19 19:43:34 +01:00
|
|
|
</Portal>
|
|
|
|
</ScrollView></Portal.Host>
|
2020-03-16 16:01:05 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Subjects.propTypes = {
|
|
|
|
subjects: PropTypes.array,
|
2020-03-31 11:06:55 +02:00
|
|
|
timetable: PropTypes.array,
|
2020-03-16 16:01:05 +01:00
|
|
|
addSubject: PropTypes.func,
|
|
|
|
removeSubject: PropTypes.func,
|
2020-03-20 15:23:06 +01:00
|
|
|
theme: PropTypes.object,
|
2020-03-16 16:01:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const style = StyleSheet.create({
|
|
|
|
card: {
|
|
|
|
marginTop: 12,
|
2020-03-17 14:55:03 +01:00
|
|
|
marginLeft: 10,
|
|
|
|
marginRight: 10,
|
2020-03-16 16:13:14 +01:00
|
|
|
},
|
|
|
|
text: {
|
|
|
|
marginTop: 12,
|
|
|
|
textAlign: "center",
|
2020-03-16 16:01:05 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-20 15:23:06 +01:00
|
|
|
|
|
|
|
export default withTheme(Subjects);
|