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 {Search} from '../SearchInput';
import SelectionHeader from '../SelectionHeader';
import {DDS} from '../../../App';
import {DDS, db} from '../../../App';
import {ACTIONS} from '../../provider/actions';
export const AnimatedSafeAreaView = Animatable.createAnimatableComponent(
SafeAreaView,
);
@@ -45,6 +46,7 @@ export const Container = ({
noSearch = false,
noSelectionHeader = false,
headerColor = null,
type = null,
}) => {
// State
const [state, dispatch] = useTracked();
@@ -82,17 +84,25 @@ export const Container = ({
};
const onChangeText = value => {
//setText(value);
setText(value);
};
const onSubmitEditing = async () => {
if (!text || text.length < 1) {
clearSearch();
} else {
//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) {
//setSearchResults([...searchResult]);
dispatch({type: ACTIONS.SEARCH_RESULTS, results: searchResult});
} else {
ToastEvent.show('No search results found', 'error', 3000, () => {}, '');
}
@@ -111,7 +121,7 @@ export const Container = ({
const clearSearch = () => {
searchResult = null;
//setSearchResults([...[]]);
dispatch({type: ACTIONS.SEARCH_RESULTS, results: []});
};
useEffect(() => {

View File

@@ -20,15 +20,9 @@ import SelectionWrapper from '../SelectionWrapper';
export const NotesList = ({isGrouped = false}) => {
const [state, dispatch] = useTracked();
const {
colors,
selectionMode,
currentEditingNote,
loading,
searchResults,
keyword,
} = state;
const {colors, selectionMode, currentEditingNote, loading, keyword} = state;
const notes = [...state.notes];
const searchResults = [...state.searchResults];
const [refreshing, setRefreshing] = useState(false);
const _renderItem = ({item, index}) => (
<SelectionWrapper
@@ -164,7 +158,34 @@ export const NotesList = ({isGrouped = false}) => {
: notes[0]
? 155
: 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();

View File

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

View File

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

View File

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

View File

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