From f5987662523ae03c88a353bcc13e7341ad809d3b Mon Sep 17 00:00:00 2001 From: ammarahm-ed Date: Mon, 2 Nov 2020 20:44:54 +0500 Subject: [PATCH] Refactor ActionSheetTagsSection and fix bugs --- .../ActionSheetTagsSection.js | 124 ++++++++++-------- 1 file changed, 71 insertions(+), 53 deletions(-) diff --git a/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js index a707f7b1a..d155bd608 100644 --- a/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js +++ b/apps/mobile/src/components/ActionSheetComponent/ActionSheetTagsSection.js @@ -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 {useTracked} from '../../provider'; import {Actions} from '../../provider/Actions'; @@ -7,20 +7,41 @@ import {db} from '../../utils/DB'; import {SIZE, WEIGHT} from '../../utils/SizeUtils'; 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 {colors, tags, premiumUser} = state; const [suggestions, setSuggestions] = useState([]); const [focused, setFocused] = useState(false); - let tagToAdd = null; - let backPressCount = 0; + const [note, setNote] = useState(item); - 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) { - ToastEvent.show('Empty Tag', 'success'); + console.log('Calling submit', tagToAdd); + ToastEvent.show('Empty Tag', 'error', 'local'); return; } + let tag = tagToAdd; tag = tag.trim(); if (tag.includes(' ')) { @@ -37,9 +58,13 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => { text: '', }); tagToAdd = ''; - }; + }); - const _onKeyPress = async (event) => { + useEffect(() => { + getSuggestions(prevQuery); + }, [note]); + + const _onKeyPress = useCallback(async (event) => { if (event.nativeEvent.key === 'Backspace') { if (backPressCount === 0 && !tagToAdd) { backPressCount = 1; @@ -73,14 +98,17 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => { text: '', }); } - }; + }); const getSuggestions = (query) => { - let _suggestions = tags.filter(t => t.title.startsWith(query)) - if (_suggestions && _suggestions.length > 0) { + console.log(note.tags); + prevQuery = query; + + let _suggestions = tags.filter( + (t) => t.title.startsWith(query) && !note.tags.find((n) => n === t.title), + ); setSuggestions(_suggestions); - } - } + }; const _renderTag = (tag) => ( { .untag(oldProps.tags[oldProps.tags.indexOf(tag)]); sendNoteEditedEvent(note.id, false, true); dispatch({type: Actions.TAGS}); + localRefresh(note.type); } catch (e) { sendNoteEditedEvent(note.id, false, true); } }} style={{ - flexDirection: 'row', - justifyContent: 'flex-start', - alignItems: 'center', - margin: 1, paddingHorizontal: 5, - paddingVertical: 2.5, + backgroundColor: colors.shade, + marginLeft: 5, + borderRadius: 2.5, }}> { : 'Suggested: '} - {[...suggestions,...suggestions,...suggestions,...suggestions].map((tag) => ( + {suggestions.map((tag) => ( { @@ -187,45 +214,29 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => { flexDirection: 'row', flexWrap: 'wrap', borderRadius: 5, - borderWidth: 1.5, + borderBottomWidth: 1.5, borderColor: focused ? colors.accent : colors.nav, alignItems: 'center', - height: 40, }}> - { - if (!premiumUser) { - close('premium'); - return; - } - tagsInputRef.current?.focus(); - }} - activeOpacity={1} - style={{ - position: 'absolute', - width: '100%', - height: '100%', - justifyContent: 'flex-start', - alignItems: 'flex-end', - zIndex: 10, - }}> - {!premiumUser ? ( - - PRO - - ) : null} - + {!premiumUser ? ( + + PRO + + ) : null} + {note && note.tags ? note.tags.map(_renderTag) : null} { paddingHorizontal: 5, paddingVertical: 0, height: 40, + fontSize: SIZE.sm, textAlignVertical: 'center', }} + autoCapitalize="none" + textAlignVertical="center" blurOnSubmit={false} ref={tagsInputRef} placeholderTextColor={colors.icon} onFocus={() => { + if (!premiumUser) { + close('premium'); + return; + } setFocused(true); }} selectionColor={colors.accent} @@ -248,7 +266,7 @@ export const ActionSheetTagsSection = ({note, localRefresh}) => { placeholder="#hashtag" onChangeText={(value) => { tagToAdd = value; - getSuggestions(value) + getSuggestions(value); if (tagToAdd.length > 0) backPressCount = 0; }} onSubmitEditing={_onSubmit}