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

199 lines
5.6 KiB
JavaScript
Raw Normal View History

2020-03-02 12:12:23 +05:00
import React, {createRef} from 'react';
2020-01-18 01:04:33 +05:00
import {Modal, Text, TouchableOpacity, View} from 'react-native';
import {TextInput} from 'react-native-gesture-handler';
2020-02-11 20:33:36 +05:00
import Icon from 'react-native-vector-icons/MaterialCommunityIcons';
2020-01-18 01:04:33 +05:00
import {opacity, ph, pv, SIZE, WEIGHT} from '../../common/common';
2020-01-19 21:31:26 +05:00
2020-01-18 01:04:33 +05:00
import {getElevation, ToastEvent} from '../../utils/utils';
2020-02-03 00:18:40 +05:00
import {db} from '../../../App';
import {eSendEvent} from '../../services/eventManager';
import {eOnNewTopicAdded} from '../../services/events';
2019-12-09 13:17:40 +05:00
2020-01-18 18:14:17 +05:00
export class AddTopicDialog extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false,
titleFocused: false,
};
this.title;
2020-03-02 12:12:23 +05:00
this.titleRef = createRef();
2020-01-18 18:14:17 +05:00
}
2020-01-08 22:47:49 +05:00
2020-01-18 18:14:17 +05:00
addNewTopic = async () => {
if (!this.title)
2019-12-09 13:17:40 +05:00
return ToastEvent.show('Title is required', 'error', 3000, () => {}, '');
2020-01-18 18:14:17 +05:00
2020-03-03 14:55:44 +05:00
await db.notebooks.notebook(this.props.notebookID).topics.add(this.title);
2020-02-03 00:18:40 +05:00
this.close();
2020-03-03 14:55:44 +05:00
eSendEvent(eOnNewTopicAdded);
ToastEvent.show('New topic added', 'success', 3000, () => {}, '');
2019-12-09 13:17:40 +05:00
};
2020-01-18 18:14:17 +05:00
open() {
this.setState({
visible: true,
});
}
close() {
this.title = null;
this.setState({
visible: false,
});
}
render() {
2020-01-19 21:31:26 +05:00
const {visible, titleFocused} = this.state;
2020-01-18 18:14:17 +05:00
const {colors, toEdit} = this.props;
2020-01-08 22:47:49 +05:00
2020-01-18 18:14:17 +05:00
return (
<Modal
visible={visible}
animated
animationType="fade"
transparent={true}
2020-03-02 12:12:23 +05:00
onShow={() => {
this.titleRef.current?.focus();
}}
2020-02-03 00:18:40 +05:00
onRequestClose={() => {
refs = [];
this.close();
}}>
2019-12-09 13:17:40 +05:00
<View
style={{
2020-01-18 18:14:17 +05:00
width: '100%',
height: '100%',
backgroundColor: 'rgba(0,0,0,0.3)',
justifyContent: 'center',
alignItems: 'center',
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<TouchableOpacity
2020-02-03 00:18:40 +05:00
onPress={() => this.close()}
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
width: '100%',
height: '100%',
position: 'absolute',
2019-12-09 13:17:40 +05:00
}}
/>
<View
style={{
2020-01-18 18:14:17 +05:00
...getElevation(5),
width: '80%',
maxHeight: 350,
borderRadius: 5,
backgroundColor: colors.bg,
paddingHorizontal: ph,
paddingVertical: pv,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<View
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
flexDirection: 'row',
2019-12-09 13:17:40 +05:00
justifyContent: 'center',
alignItems: 'center',
}}>
2020-02-11 20:33:36 +05:00
<Icon name="book-outline" color={colors.accent} size={SIZE.lg} />
2019-12-09 13:17:40 +05:00
<Text
style={{
2020-01-18 18:14:17 +05:00
color: colors.accent,
fontFamily: WEIGHT.bold,
marginLeft: 5,
fontSize: SIZE.md,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
{toEdit ? 'Edit Topic' : 'Add New Topic'}
2019-12-09 13:17:40 +05:00
</Text>
2020-01-18 18:14:17 +05:00
</View>
2019-12-09 13:17:40 +05:00
2020-01-18 18:14:17 +05:00
<TextInput
2020-03-02 12:12:23 +05:00
ref={this.titleRef}
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
padding: pv,
borderWidth: 1.5,
borderColor: titleFocused ? colors.accent : colors.nav,
2019-12-09 13:17:40 +05:00
paddingHorizontal: ph,
borderRadius: 5,
2020-01-18 18:14:17 +05:00
fontSize: SIZE.sm,
fontFamily: WEIGHT.regular,
color: colors.pri,
marginTop: 20,
}}
onFocus={() => {
this.setState({
titleFocused: true,
});
}}
onBlur={() => {
this.setState({
titleFocused: true,
});
}}
defaultValue={toEdit ? toEdit.title : null}
onChangeText={value => {
this.title = value;
}}
placeholder="Enter title of topic"
placeholderTextColor={colors.icon}
/>
<View
style={{
justifyContent: 'space-around',
2019-12-09 13:17:40 +05:00
alignItems: 'center',
2020-01-18 18:14:17 +05:00
flexDirection: 'row',
marginTop: 20,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<TouchableOpacity
activeOpacity={opacity}
onPress={async () => await this.addNewTopic()}
2019-12-09 13:17:40 +05:00
style={{
2020-01-18 18:14:17 +05:00
paddingVertical: pv,
paddingHorizontal: ph,
borderRadius: 5,
width: '45%',
justifyContent: 'center',
alignItems: 'center',
borderColor: colors.accent,
backgroundColor: colors.accent,
borderWidth: 1,
2019-12-09 13:17:40 +05:00
}}>
2020-01-18 18:14:17 +05:00
<Text
style={{
fontFamily: WEIGHT.medium,
color: 'white',
fontSize: SIZE.sm,
}}>
Add
</Text>
</TouchableOpacity>
<TouchableOpacity
activeOpacity={opacity}
onPress={() => this.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>
2019-12-09 13:17:40 +05:00
</View>
</View>
2020-01-18 18:14:17 +05:00
</Modal>
);
}
}