linked up filter selection modal with redux

This commit is contained in:
Nouman Tahir 2021-04-22 05:05:44 +05:00
parent 1136505261
commit 34b105549e
8 changed files with 116 additions and 43 deletions

View File

@ -1,9 +1,12 @@
import React, { forwardRef, Ref, useImperativeHandle, useRef, useState } from 'react'; import React, { forwardRef, Ref, useImperativeHandle, useRef, useState } from 'react';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import { TouchableOpacity } from 'react-native';
import { KeyboardAvoidingView, Platform, View, Text } from 'react-native'; import { KeyboardAvoidingView, Platform, View, Text } from 'react-native';
import ActionSheet from 'react-native-actions-sheet'; import ActionSheet from 'react-native-actions-sheet';
import EStyleSheet from 'react-native-extended-stylesheet'; import EStyleSheet from 'react-native-extended-stylesheet';
import {useDispatch} from 'react-redux'; import {useDispatch, useSelector} from 'react-redux';
import { CheckBox } from '..';
import { DEFAULT_FEED_FILTERS_LOGGED_IN, FEED_SCREEN_FILTER_MAP } from '../../constants/options/filters';
import { ThemeContainer } from '../../containers'; import { ThemeContainer } from '../../containers';
import { setFeedScreenFilters } from '../../redux/actions/postsAction'; import { setFeedScreenFilters } from '../../redux/actions/postsAction';
@ -18,13 +21,13 @@ export interface CustomiseFiltersModalRef {
const CustomiseFiltersModal = (props:any, ref:Ref<CustomiseFiltersModalRef>) => { const CustomiseFiltersModal = (props:any, ref:Ref<CustomiseFiltersModalRef>) => {
const sheetModalRef = useRef<ActionSheet>(); const sheetModalRef = useRef<ActionSheet>();
const dispatch = useDispatch() const dispatch = useDispatch();
const [selectedFilters, setSelectedFilters] = useState([ const feedScreenFilters = useSelector(state => state.posts.feedScreenFilters);
'friends',
'hot', const [selectedFilters, setSelectedFilters] = useState<Map<string, number>>(
'created', new Map(feedScreenFilters.map((key:string, index:number)=>[key, index]))
]); );
const intl = useIntl(); const intl = useIntl();
@ -36,10 +39,60 @@ const CustomiseFiltersModal = (props:any, ref:Ref<CustomiseFiltersModalRef>) =>
})); }));
const _onClose = () => {
sheetModalRef.current?.setModalVisible(false);
}
//save snippet based on editor type //save snippet based on editor type
const _onDone = async () => { const _onApply = () => {
dispatch(setFeedScreenFilters(selectedFilters)) if(selectedFilters.size !== 3){
sheetModalRef.current?.setModalVisible(false); alert("Please select exactly three filters");
return;
}
const entries = Array.from(selectedFilters.entries());
entries.sort((a, b)=>a[1]<b[1]?-1:1);
dispatch(setFeedScreenFilters(entries.map((e)=>e[0])));
_onClose();
}
const _renderOptions = () => {
const options = [];
for(const key in FEED_SCREEN_FILTER_MAP){
if(FEED_SCREEN_FILTER_MAP.hasOwnProperty(key)){
const isSelected = selectedFilters.has(key);
const _onPress = () => {
if(isSelected){
selectedFilters.delete(key);
}else{
var index = Object.keys(FEED_SCREEN_FILTER_MAP).indexOf(key);
selectedFilters.set(key, index);
}
setSelectedFilters(new Map([...selectedFilters]));
}
options.push((
<TouchableOpacity onPress={_onPress}>
<View style={styles.checkView}>
<Text style={styles.informationText}>
{intl.formatMessage({
id:FEED_SCREEN_FILTER_MAP[key]
})}
</Text>
<CheckBox locked isChecked={isSelected} />
</View>
</TouchableOpacity>
))
}
}
return (
<View style={styles.textContainer}>
{options}
</View>
)
} }
@ -51,23 +104,20 @@ const CustomiseFiltersModal = (props:any, ref:Ref<CustomiseFiltersModalRef>) =>
keyboardVerticalOffset={Platform.OS == 'ios' ? 64 : null} keyboardVerticalOffset={Platform.OS == 'ios' ? 64 : null}
behavior={Platform.OS === 'ios' ? 'padding' : null} behavior={Platform.OS === 'ios' ? 'padding' : null}
> >
<Text>Customise Filters</Text> <Text style={styles.title}>Customise Filters</Text>
<View style={styles.inputContainer}>
{_renderOptions()}
{[0,0,0,0,0,0].map(()=>(
<Text>Filter</Text>
))}
</View>
<View style={styles.actionPanel}> <View style={styles.actionPanel}>
<TextButton <TextButton
text={'DONE'} text={'APPLY'}
onPress={_onDone} onPress={_onApply}
textStyle={styles.btnText} textStyle={styles.btnText}
style={styles.button} style={styles.button}
/> />
</View> </View>
</KeyboardAvoidingView> </KeyboardAvoidingView>
)} )}
@ -79,7 +129,7 @@ const CustomiseFiltersModal = (props:any, ref:Ref<CustomiseFiltersModalRef>) =>
ref={sheetModalRef} ref={sheetModalRef}
containerStyle={styles.sheetContent} containerStyle={styles.sheetContent}
indicatorColor={EStyleSheet.value('$primaryWhiteLightBackground')} indicatorColor={EStyleSheet.value('$primaryWhiteLightBackground')}
onClose={_onDone} onClose={_onClose}
> >
{_renderContent} {_renderContent}
</ActionSheet> </ActionSheet>

View File

@ -1,4 +1,4 @@
import { TextStyle, StyleSheet, ViewStyle, ImageStyle } from 'react-native'; import { TextStyle, StyleSheet, ViewStyle, ImageStyle, Dimensions } from 'react-native';
import EStyleSheet from 'react-native-extended-stylesheet'; import EStyleSheet from 'react-native-extended-stylesheet';
export default EStyleSheet.create({ export default EStyleSheet.create({
@ -71,4 +71,22 @@ export default EStyleSheet.create({
alignItems:'center', alignItems:'center',
} as ViewStyle, } as ViewStyle,
checkView: {
width:Dimensions.get('screen').width - 80,
flexDirection: 'row',
justifyContent: 'space-between',
alignItems:'center',
marginHorizontal: 20,
marginVertical:4,
} as ViewStyle,
informationText: {
color: '$primaryBlack',
margin: 10,
fontSize:18,
} as TextStyle,
}) })

View File

@ -31,12 +31,17 @@ export const TabbedPosts = ({
//initialize state //initialize state
const [initialTabIndex] = useState(selectedOptionIndex == 0 && stackedTabs ? filterOptions.length : selectedOptionIndex) const [initialTabIndex] = useState(selectedOptionIndex == 0 && stackedTabs ? filterOptions.length : selectedOptionIndex)
const [mainFilters] = useState<TabItem[]>( // const [mainFilters] = useState<TabItem[]>(
filterOptions.map((label, index)=>({ // filterOptions.map((label, index)=>({
filterKey:filterOptionsValue[index], // filterKey:filterOptionsValue[index],
label // label
} as TabItem)) // } as TabItem))
) // )
const mainFilters = filterOptions.map((label, index)=>({
filterKey:filterOptionsValue[index],
label
} as TabItem));
const [subFilters] = useState<TabItem[]>( const [subFilters] = useState<TabItem[]>(
feedSubfilterOptions feedSubfilterOptions
@ -47,7 +52,8 @@ export const TabbedPosts = ({
: [] : []
) )
const [combinedFilters] = useState([...mainFilters, ...subFilters]); const combinedFilters = [...mainFilters, ...subFilters]
// const [combinedFilters] = useState([...mainFilters, ...subFilters]);
const [selectedFilter, setSelectedFilter] = useState(combinedFilters[initialTabIndex].filterKey) const [selectedFilter, setSelectedFilter] = useState(combinedFilters[initialTabIndex].filterKey)
const [filterScrollRequest, createFilterScrollRequest] = useState<string|null>(null) const [filterScrollRequest, createFilterScrollRequest] = useState<string|null>(null)

View File

@ -4,7 +4,7 @@ import { getPromotedPosts, loadPosts } from '../services/tabbedPostsFetch';
import { LoadPostsOptions, TabContentProps, TabMeta } from '../services/tabbedPostsModels'; import { LoadPostsOptions, TabContentProps, TabMeta } from '../services/tabbedPostsModels';
import {useSelector, useDispatch } from 'react-redux'; import {useSelector, useDispatch } from 'react-redux';
import TabEmptyView from './listEmptyView'; import TabEmptyView from './listEmptyView';
import { setInitPosts } from '../../../redux/actions/postsAction'; import { setInitPosts } from '../../../redux/actions/postsAction.ts';
import NewPostsPopup from './newPostsPopup'; import NewPostsPopup from './newPostsPopup';
import { calculateTimeLeftForPostCheck } from '../services/tabbedPostsReducer'; import { calculateTimeLeftForPostCheck } from '../services/tabbedPostsReducer';
import { AppState } from 'react-native'; import { AppState } from 'react-native';

View File

@ -6,11 +6,11 @@ export const DEFAULT_FEED_FILTERS_LOGGED_IN = ['friends', 'communities', 'hot'];
export const FEED_SCREEN_FILTER_MAP = { export const FEED_SCREEN_FILTER_MAP = {
friends:'home.friends',
communities:'home.communities',
trending:'home.top', trending:'home.top',
hot:'home.hot', hot:'home.hot',
created:'home.new', created:'home.new',
friends:'home.friends',
communities:'home.communities'
} }
export const GLOBAL_POST_FILTERS = ['home.TOP', 'home.HOT', 'home.NEW']; export const GLOBAL_POST_FILTERS = ['home.TOP', 'home.HOT', 'home.NEW'];

View File

@ -34,7 +34,6 @@ export const CHANGE_REBLOG_NOTIFICATION = 'CHANGE_REBLOG_NOTIFICATION';
export const CHANGE_TRANSFERS_NOTIFICATION = 'CHANGE_TRANSFERS_NOTIFICATION'; export const CHANGE_TRANSFERS_NOTIFICATION = 'CHANGE_TRANSFERS_NOTIFICATION';
export const CHANGE_ALL_NOTIFICATION_SETTINGS = 'CHANGE_ALL_NOTIFICATION_SETTINGS'; export const CHANGE_ALL_NOTIFICATION_SETTINGS = 'CHANGE_ALL_NOTIFICATION_SETTINGS';
// Accounts // Accounts
export const ADD_OTHER_ACCOUNT = 'ADD_OTHER_ACCOUNT'; export const ADD_OTHER_ACCOUNT = 'ADD_OTHER_ACCOUNT';
export const FETCH_ACCOUNT_FAIL = 'FETCH_ACCOUNT_FAIL'; export const FETCH_ACCOUNT_FAIL = 'FETCH_ACCOUNT_FAIL';

View File

@ -80,7 +80,7 @@ import {
toastNotification, toastNotification,
updateActiveBottomTab, updateActiveBottomTab,
} from '../../../redux/actions/uiAction'; } from '../../../redux/actions/uiAction';
import { resetLocalVoteMap } from '../../../redux/actions/postsAction.ts'; import { resetLocalVoteMap, setFeedScreenFilters } from '../../../redux/actions/postsAction';
import { encryptKey } from '../../../utils/crypto'; import { encryptKey } from '../../../utils/crypto';

View File

@ -1,4 +1,4 @@
import React, { Fragment } from 'react'; import React, { Fragment, useEffect } from 'react';
import { SafeAreaView } from 'react-native'; import { SafeAreaView } from 'react-native';
import { useSelector } from 'react-redux'; import { useSelector } from 'react-redux';
import get from 'lodash/get'; import get from 'lodash/get';
@ -24,11 +24,11 @@ import {
} from '../../../constants/options/filters'; } from '../../../constants/options/filters';
const FeedScreen = () => { const FeedScreen = () => {
const feedScreenFilters = useSelector( const feedScreenFilters = useSelector((state) => state.posts.feedScreenFilters);
(state) => state.posts.feedScreenFilters || DEFAULT_FEED_FILTERS_LOGGED_IN,
);
const filterOptions = feedScreenFilters.map((key) => FEED_SCREEN_FILTER_MAP[key]); const filterOptions = feedScreenFilters.map((key) => FEED_SCREEN_FILTER_MAP[key]);
useEffect(() => {}, [feedScreenFilters]);
return ( return (
<AccountContainer> <AccountContainer>
{({ currentAccount }) => ( {({ currentAccount }) => (