Refactor ActionSheetTagsSection and fix bugs

This commit is contained in:
ammarahm-ed
2020-11-02 20:44:54 +05:00
parent 45a1a8064e
commit f598766252

View File

@@ -1,4 +1,4 @@
import React, {createRef, useState} from 'react'; import React, {createRef, useCallback, useEffect, useState} from 'react';
import {Text, TextInput, TouchableOpacity, View} from 'react-native'; import {Text, TextInput, TouchableOpacity, View} from 'react-native';
import {useTracked} from '../../provider'; import {useTracked} from '../../provider';
import {Actions} from '../../provider/Actions'; import {Actions} from '../../provider/Actions';
@@ -7,20 +7,41 @@ import {db} from '../../utils/DB';
import {SIZE, WEIGHT} from '../../utils/SizeUtils'; import {SIZE, WEIGHT} from '../../utils/SizeUtils';
const tagsInputRef = createRef(); const tagsInputRef = createRef();
let prevQuery = null;
let tagToAdd = '';
let backPressCount = 0;
export const ActionSheetTagsSection = ({note, localRefresh}) => { export const ActionSheetTagsSection = ({item}) => {
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
const {colors, tags, premiumUser} = state; const {colors, tags, premiumUser} = state;
const [suggestions, setSuggestions] = useState([]); const [suggestions, setSuggestions] = useState([]);
const [focused, setFocused] = useState(false); const [focused, setFocused] = useState(false);
let tagToAdd = null; const [note, setNote] = useState(item);
let backPressCount = 0;
const _onSubmit = async () => { const localRefresh = () => {
toAdd = db.notes.note(note.id);
if (toAdd) {
toAdd = toAdd.data;
} else {
setTimeout(() => {
toAdd = db.notes.note(note.id);
if (toAdd) {
toAdd = toAdd.data;
}
}, 500);
}
dispatch({type: Actions.NOTES});
dispatch({type: Actions.TAGS});
setNote({...toAdd});
};
const _onSubmit = useCallback(async () => {
if (!tagToAdd || tagToAdd === '' || tagToAdd.trimStart().length == 0) { if (!tagToAdd || tagToAdd === '' || tagToAdd.trimStart().length == 0) {
ToastEvent.show('Empty Tag', 'success'); console.log('Calling submit', tagToAdd);
ToastEvent.show('Empty Tag', 'error', 'local');
return; return;
} }
let tag = tagToAdd; let tag = tagToAdd;
tag = tag.trim(); tag = tag.trim();
if (tag.includes(' ')) { if (tag.includes(' ')) {
@@ -37,9 +58,13 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
text: '', text: '',
}); });
tagToAdd = ''; tagToAdd = '';
}; });
const _onKeyPress = async (event) => { useEffect(() => {
getSuggestions(prevQuery);
}, [note]);
const _onKeyPress = useCallback(async (event) => {
if (event.nativeEvent.key === 'Backspace') { if (event.nativeEvent.key === 'Backspace') {
if (backPressCount === 0 && !tagToAdd) { if (backPressCount === 0 && !tagToAdd) {
backPressCount = 1; backPressCount = 1;
@@ -73,14 +98,17 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
text: '', text: '',
}); });
} }
}; });
const getSuggestions = (query) => { const getSuggestions = (query) => {
let _suggestions = tags.filter(t => t.title.startsWith(query)) console.log(note.tags);
if (_suggestions && _suggestions.length > 0) { prevQuery = query;
let _suggestions = tags.filter(
(t) => t.title.startsWith(query) && !note.tags.find((n) => n === t.title),
);
setSuggestions(_suggestions); setSuggestions(_suggestions);
} };
}
const _renderTag = (tag) => ( const _renderTag = (tag) => (
<TouchableOpacity <TouchableOpacity
@@ -93,17 +121,16 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
.untag(oldProps.tags[oldProps.tags.indexOf(tag)]); .untag(oldProps.tags[oldProps.tags.indexOf(tag)]);
sendNoteEditedEvent(note.id, false, true); sendNoteEditedEvent(note.id, false, true);
dispatch({type: Actions.TAGS}); dispatch({type: Actions.TAGS});
localRefresh(note.type);
} catch (e) { } catch (e) {
sendNoteEditedEvent(note.id, false, true); sendNoteEditedEvent(note.id, false, true);
} }
}} }}
style={{ style={{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
margin: 1,
paddingHorizontal: 5, paddingHorizontal: 5,
paddingVertical: 2.5, backgroundColor: colors.shade,
marginLeft: 5,
borderRadius: 2.5,
}}> }}>
<Text <Text
style={{ style={{
@@ -149,7 +176,7 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
: 'Suggested: '} : 'Suggested: '}
</Text> </Text>
{[...suggestions,...suggestions,...suggestions,...suggestions].map((tag) => ( {suggestions.map((tag) => (
<TouchableOpacity <TouchableOpacity
key={tag.title} key={tag.title}
onPress={() => { onPress={() => {
@@ -187,45 +214,29 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
flexDirection: 'row', flexDirection: 'row',
flexWrap: 'wrap', flexWrap: 'wrap',
borderRadius: 5, borderRadius: 5,
borderWidth: 1.5, borderBottomWidth: 1.5,
borderColor: focused ? colors.accent : colors.nav, borderColor: focused ? colors.accent : colors.nav,
alignItems: 'center', alignItems: 'center',
height: 40,
}}> }}>
<TouchableOpacity {!premiumUser ? (
onPress={() => { <Text
if (!premiumUser) { style={{
close('premium'); color: colors.accent,
return; fontFamily: WEIGHT.regular,
} fontSize: 10,
tagsInputRef.current?.focus(); marginRight: 4,
}} marginTop: 2.5,
activeOpacity={1} position: 'absolute',
style={{ right: 0,
position: 'absolute', top: 0,
width: '100%', }}>
height: '100%', PRO
justifyContent: 'flex-start', </Text>
alignItems: 'flex-end', ) : null}
zIndex: 10,
}}>
{!premiumUser ? (
<Text
style={{
color: colors.accent,
fontFamily: WEIGHT.regular,
fontSize: 10,
marginRight: 4,
marginTop: 2.5,
}}>
PRO
</Text>
) : null}
</TouchableOpacity>
{note && note.tags ? note.tags.map(_renderTag) : null} {note && note.tags ? note.tags.map(_renderTag) : null}
<TextInput <TextInput
style={{ style={{
backgroundColor: 'transparent',
minWidth: 100, minWidth: 100,
zIndex: 10, zIndex: 10,
fontFamily: WEIGHT.regular, fontFamily: WEIGHT.regular,
@@ -233,12 +244,19 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
paddingHorizontal: 5, paddingHorizontal: 5,
paddingVertical: 0, paddingVertical: 0,
height: 40, height: 40,
fontSize: SIZE.sm,
textAlignVertical: 'center', textAlignVertical: 'center',
}} }}
autoCapitalize="none"
textAlignVertical="center"
blurOnSubmit={false} blurOnSubmit={false}
ref={tagsInputRef} ref={tagsInputRef}
placeholderTextColor={colors.icon} placeholderTextColor={colors.icon}
onFocus={() => { onFocus={() => {
if (!premiumUser) {
close('premium');
return;
}
setFocused(true); setFocused(true);
}} }}
selectionColor={colors.accent} selectionColor={colors.accent}
@@ -248,7 +266,7 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => {
placeholder="#hashtag" placeholder="#hashtag"
onChangeText={(value) => { onChangeText={(value) => {
tagToAdd = value; tagToAdd = value;
getSuggestions(value) getSuggestions(value);
if (tagToAdd.length > 0) backPressCount = 0; if (tagToAdd.length > 0) backPressCount = 0;
}} }}
onSubmitEditing={_onSubmit} onSubmitEditing={_onSubmit}