Files
notesnook/apps/mobile/src/components/AddTopicDialog/index.js

165 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-01-18 01:04:33 +05:00
import React, {useState} from 'react';
import {Modal, Text, TouchableOpacity, View} from 'react-native';
import {TextInput} from 'react-native-gesture-handler';
2019-12-09 13:17:40 +05:00
import Icon from 'react-native-vector-icons/Feather';
2019-12-11 01:10:04 +05:00
import {db} from '../../../App';
2020-01-18 01:04:33 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-01-17 21:26:01 +05:00
import {useTracked} from '../../provider';
2020-01-18 01:04:33 +05:00
import {getElevation, ToastEvent} from '../../utils/utils';
2019-12-09 13:17:40 +05:00
export const AddTopicDialog = ({
visible,
close = () => {},
notebookID,
toEdit = null,
}) => {
2020-01-17 21:26:01 +05:00
const [state, dispatch] = useTracked();
const {colors} = state;
2020-01-08 22:47:49 +05:00
const [titleFocused, setTitleFocused] = useState(false);
2019-12-09 13:17:40 +05:00
let title = null;
const addNewTopic = async () => {
if (!title)
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
2019-12-11 01:10:04 +05:00
db.addTopicToNotebook(notebookID, title);
2019-12-09 13:17:40 +05:00
ToastEvent.show('New topic added', 'success', 3000, () => {}, '');
close(true);
};
return (
<Modal
visible={visible}
2019-12-11 15:20:18 +05:00
animated
animationType="fade"
2019-12-09 13:17:40 +05:00
transparent={true}
onRequestClose={() => (refs = [])}>
<View
style={{
width: '100%',
height: '100%',
2020-01-08 22:47:49 +05:00
backgroundColor: 'rgba(0,0,0,0.3)',
2019-12-09 13:17:40 +05:00
justifyContent: 'center',
alignItems: 'center',
}}>
2020-01-08 22:47:49 +05:00
<TouchableOpacity
onPress={() => close()}
style={{
width: '100%',
height: '100%',
position: 'absolute',
}}
/>
2019-12-09 13:17:40 +05:00
<View
style={{
2019-12-11 15:20:18 +05:00
...getElevation(5),
2019-12-09 13:17:40 +05:00
width: '80%',
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
}}>
<View
style={{
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
}}>
<Icon name="book-open" color={colors.accent} size={SIZE.lg} />
<Text
style={{
color: colors.accent,
2019-12-14 16:00:16 +05:00
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
2019-12-09 13:17:40 +05:00
}}>
{toEdit ? 'Edit Topic' : 'Add New Topic'}
</Text>
</View>
<TextInput
style={{
padding: pv,
borderWidth: 1.5,
2020-01-08 22:47:49 +05:00
borderColor: titleFocused ? colors.accent : colors.nav,
2019-12-09 13:17:40 +05:00
paddingHorizontal: ph,
borderRadius: 5,
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
marginTop: 20,
}}
2020-01-08 22:47:49 +05:00
onFocus={() => {
setTitleFocused(true);
}}
onBlur={() => {
setTitleFocused(false);
}}
2019-12-09 13:17:40 +05:00
defaultValue={toEdit ? toEdit.title : null}
onChangeText={value => {
title = value;
}}
placeholder="Enter title of topic"
placeholderTextColor={colors.icon}
/>
<View
style={{
justifyContent: 'space-around',
alignItems: 'center',
flexDirection: 'row',
marginTop: 20,
}}>
<TouchableOpacity
activeOpacity={opacity}
onPress={async () => await addNewTopic()}
style={{
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '45%',
justifyContent: 'center',
alignItems: 'center',
borderColor: colors.accent,
backgroundColor: colors.accent,
borderWidth: 1,
}}>
<Text
style={{
fontFamily: WEIGHT.medium,
color: 'white',
fontSize: SIZE.sm,
}}>
Add
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={opacity}
onPress={() => close()}
style={{
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '45%',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: colors.nav,
}}>
<Text
style={{
fontFamily: WEIGHT.medium,
color: colors.icon,
fontSize: SIZE.sm,
}}>
Cancel
</Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
);
};