Skip to content

Commit

Permalink
Use provider and context to get Album data
Browse files Browse the repository at this point in the history
  • Loading branch information
skantus committed Apr 8, 2020
1 parent 591d72c commit 2fc2426
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import AppNavigator from 'src/components/AppNavigator';

const App: () => React.ReactNode = () => {
const App = () => {
return <AppNavigator />;
};

Expand Down
15 changes: 15 additions & 0 deletions src/api/AlbumProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react';
import AlbumContext from 'src/store/AlbumContext';
import data from 'src/api/data/data';

class AlbumProvider extends React.Component {
render() {
return (
<AlbumContext.Provider value={{data}}>
{this.props.children}
</AlbumContext.Provider>
);
}
}

export default AlbumProvider;
39 changes: 21 additions & 18 deletions src/components/Albums/Albums.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {SafeAreaView} from 'react-navigation';
import StatusBar from '@react-native-community/status-bar';
import {SharedElementRenderer} from 'react-native-motion';
import AnimatedHeader from 'src/components/common/AnimatedHeader';
import AlbumProvider from 'src/api/AlbumProvider';
import List from './List';
import Detail from './Detail';
import styles from './styles';
Expand Down Expand Up @@ -50,24 +51,26 @@ const Albums = () => {
<View style={styles.content}>
<AnimatedHeader isHidden={phase !== PHASE_1 && phase !== PHASE_2} />
<View style={styles.container}>
<List
selectedItem={selectedItem}
onItemPress={onItemPressed}
phase={phase}
theme={null}
screenProps={null}
/>
<Detail
phase={phase}
selectedItem={selectedItem}
onBackPress={onBackPressed}
onSharedElementMovedToDestination={
onSharedElementMovedToDestination
}
onSharedElementMovedToSource={onSharedElementMovedToSource}
theme={null}
screenProps={null}
/>
<AlbumProvider>
<List
selectedItem={selectedItem}
onItemPress={onItemPressed}
phase={phase}
theme={null}
screenProps={null}
/>
<Detail
phase={phase}
selectedItem={selectedItem}
onBackPress={onBackPressed}
onSharedElementMovedToDestination={
onSharedElementMovedToDestination
}
onSharedElementMovedToSource={onSharedElementMovedToSource}
theme={null}
screenProps={null}
/>
</AlbumProvider>
</View>
</View>
</SafeAreaView>
Expand Down
34 changes: 20 additions & 14 deletions src/components/Albums/List/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {NavigationStackScreenProps} from 'react-navigation-stack';
import {SharedElement} from 'react-native-motion';
import Header from './Header';
import Card from 'src/components/common/Card';
import data from 'src/api/data/data';
import AlbumContext from 'src/store/AlbumContext';
import styles from './styles';

type Props = {
Expand Down Expand Up @@ -73,19 +73,25 @@ const List = (props: Props & NavigationStackScreenProps) => {
const {phase} = props;

return (
<View style={styles.container}>
<Header
isHidden={phase !== PHASE_0}
onPress={() => props.navigation.navigate('ProfileTab')}
/>
<FlatList
data={data}
showsVerticalScrollIndicator={false}
extraData={{phase, opacityOfSelectedItem}}
keyExtractor={(item) => item.title}
renderItem={renderItem}
/>
</View>
<AlbumContext.Consumer>
{(context) => {
return (
<View style={styles.container}>
<Header
isHidden={phase !== PHASE_0}
onPress={() => props.navigation.navigate('ProfileTab')}
/>
<FlatList
data={context.data}
showsVerticalScrollIndicator={false}
extraData={{phase, opacityOfSelectedItem}}
keyExtractor={(item) => item.title}
renderItem={renderItem}
/>
</View>
);
}}
</AlbumContext.Consumer>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {AnimatedValue} from 'react-navigation';

type Props = {
isHidden: boolean;
children: React.ReactNode;
children: any;
};

const SHOW = 1;
Expand Down
5 changes: 5 additions & 0 deletions src/store/AlbumContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react';

const AlbumContent = React.createContext(null);

export default AlbumContent;

0 comments on commit 2fc2426

Please sign in to comment.