add notes search

This commit is contained in:
ammarahm-ed
2020-02-07 00:53:59 +05:00
parent 1a9ffd6b18
commit e1e30d4da4
6 changed files with 57 additions and 17 deletions

View File

@@ -18,7 +18,8 @@ import {getElevation, h, w} from '../../utils/utils';
import {Header} from '../header'; import {Header} from '../header';
import {Search} from '../SearchInput'; import {Search} from '../SearchInput';
import SelectionHeader from '../SelectionHeader'; import SelectionHeader from '../SelectionHeader';
import {DDS} from '../../../App'; import {DDS, db} from '../../../App';
import {ACTIONS} from '../../provider/actions';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent( export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView, SafeAreaView,
); );
@@ -45,6 +46,7 @@ export const Container = ({
noSearch = false, noSearch = false,
noSelectionHeader = false, noSelectionHeader = false,
headerColor = null, headerColor = null,
type = null,
}) => { }) => {
// State // State
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
@@ -82,17 +84,25 @@ export const Container = ({
}; };
const onChangeText = value => { const onChangeText = value => {
//setText(value); setText(value);
}; };
const onSubmitEditing = async () => { const onSubmitEditing = async () => {
if (!text || text.length < 1) { if (!text || text.length < 1) {
clearSearch(); clearSearch();
} else { } else {
//setKeyword(text); //setKeyword(text);
searchResult = await db.searchNotes(text); if (type === 'notes') {
searchResult = await db.notes.filter(text);
} else if (type === 'notebooks') {
searchResult = await db.notebooks.filter(text);
} else if (type === 'topic') {
return;
} else {
return;
}
if (searchResult && searchResult.length > 0) { if (searchResult && searchResult.length > 0) {
//setSearchResults([...searchResult]); dispatch({type: ACTIONS.SEARCH_RESULTS, results: searchResult});
} else { } else {
ToastEvent.show('No search results found', 'error', 3000, () => {}, ''); ToastEvent.show('No search results found', 'error', 3000, () => {}, '');
} }
@@ -111,7 +121,7 @@ export const Container = ({
const clearSearch = () => { const clearSearch = () => {
searchResult = null; searchResult = null;
//setSearchResults([...[]]); dispatch({type: ACTIONS.SEARCH_RESULTS, results: []});
}; };
useEffect(() => { useEffect(() => {

View File

@@ -20,15 +20,9 @@ import SelectionWrapper from '../SelectionWrapper';
export const NotesList = ({isGrouped = false}) => { export const NotesList = ({isGrouped = false}) => {
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
const { const {colors, selectionMode, currentEditingNote, loading, keyword} = state;
colors,
selectionMode,
currentEditingNote,
loading,
searchResults,
keyword,
} = state;
const notes = [...state.notes]; const notes = [...state.notes];
const searchResults = [...state.searchResults];
const [refreshing, setRefreshing] = useState(false); const [refreshing, setRefreshing] = useState(false);
const _renderItem = ({item, index}) => ( const _renderItem = ({item, index}) => (
<SelectionWrapper <SelectionWrapper
@@ -164,7 +158,34 @@ export const NotesList = ({isGrouped = false}) => {
: notes[0] : notes[0]
? 155 ? 155
: 155 - 60, : 155 - 60,
}}></View> flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 12,
}}>
<Text
style={{
fontFamily: WEIGHT.bold,
color: colors.accent,
fontSize: SIZE.xs,
}}>
Search Results for War
</Text>
<Text
onPress={() => {
dispatch({
type: ACTIONS.SEARCH_RESULTS,
results: [],
});
}}
style={{
fontFamily: WEIGHT.regular,
color: colors.errorText,
fontSize: SIZE.xs,
}}>
Clear
</Text>
</View>
); );
const _listKeyExtractor = (item, index) => item.id.toString(); const _listKeyExtractor = (item, index) => item.id.toString();

View File

@@ -11,7 +11,7 @@ const {Value, timing, block} = Animated;
export const Search = props => { export const Search = props => {
const [state, dispatch] = useTracked(); const [state, dispatch] = useTracked();
const {colors} = state; const {colors, searchResults} = state;
const [focus, setFocus] = useState(false); const [focus, setFocus] = useState(false);
const inputRef = createRef(); const inputRef = createRef();
@@ -94,12 +94,12 @@ export const Search = props => {
style={{paddingRight: DDS.isTab ? 12 : 12}} style={{paddingRight: DDS.isTab ? 12 : 12}}
onPress={() => { onPress={() => {
props.clear(); props.clear();
props.value.length > 0 ? props.clearSearch() : null; searchResults.length > 0 ? props.clearSearch() : null;
inputRef.current.setNativeProps({ inputRef.current.setNativeProps({
text: '', text: '',
}); });
}} }}
name={props.value && props.value.length > 0 ? '' : 'search'} name={searchResults.length > 0 ? 'x' : 'search'}
color={ color={
focus focus
? props.headerColor ? props.headerColor

View File

@@ -13,4 +13,5 @@ export const ACTIONS = {
LOGIN_NAVIGATOR: 'loginNavigator', LOGIN_NAVIGATOR: 'loginNavigator',
CURRENT_EDITING_NOTE: 'currentEditingNote', CURRENT_EDITING_NOTE: 'currentEditingNote',
COLORS: 'colorNotes', COLORS: 'colorNotes',
SEARCH_RESULTS: 'searchResults',
}; };

View File

@@ -112,6 +112,13 @@ export const reducer = (state, action) => {
currentEditingNote: action.id, currentEditingNote: action.id,
}; };
} }
case ACTIONS.SEARCH_RESULTS: {
let results = action.results;
return {
...state,
searchResults: [...results],
};
}
default: default:
throw new Error('unknown action type'); throw new Error('unknown action type');
} }

View File

@@ -32,6 +32,7 @@ export const Home = ({navigation}) => {
heading="Home" heading="Home"
customIcon="menu" customIcon="menu"
verticalMenu verticalMenu
type="notes"
menu menu
placeholder="Search all notes" placeholder="Search all notes"
canGoBack={false} canGoBack={false}