SidebarPage.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import {
  2. LayoutAnimation,
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. UIManager,
  7. View,
  8. ScrollView,
  9. TouchableOpacity,
  10. Image,
  11. } from 'react-native';
  12. import React, { useContext, useEffect, useState } from 'react';
  13. import { List } from 'react-native-paper';
  14. import EntypoIcon from 'react-native-vector-icons/Entypo';
  15. import { horizontalScale, moderateScale, verticalScale } from '../../constants/metrics';
  16. import NewscoutLogo from '../../components/atoms/Logo/NewscoutLogo';
  17. import fonts from '../../constants/fonts';
  18. import CircularPrimaryBackButton from '../../components/atoms/Buttons/CircularPrimaryBackButton';
  19. import colors from '../../constants/colors';
  20. import NewscoutHomeHeader from '../../components/molecules/Header/NewscoutHomeHeader';
  21. import { navigateToListViewPage, useConstructor } from '../../constants/functions';
  22. import { getArticlesByCategory, getCategories } from '../../api/data';
  23. import { BASE_URL } from '../../api/urls';
  24. import CustomSwitch from '../../components/atoms/Switch/CustomSwitch';
  25. import { ThemeContext } from '../../context/theme.context';
  26. const SidebarPage = ({ navigation, route }) => {
  27. const [data, setData] = React.useState([])
  28. const [expandedId, setExpandedId] = React.useState('');
  29. const handleAccordionPress = accordionId => {
  30. setExpandedId(accordionId === expandedId ? '' : accordionId);
  31. };
  32. const fetchMenus = () => {
  33. getCategories().then(res => {
  34. setData(res.data.body.results)
  35. }).catch(err => console.error(err))
  36. .finally(() => console.log("Fetch Sidebar Categories Executed"))
  37. }
  38. useConstructor(() => {
  39. fetchMenus()
  40. })
  41. const styles = StyleSheet.create({
  42. sideBarContainer: {
  43. paddingHorizontal: horizontalScale(24),
  44. backgroundColor: colors().dominant,
  45. height: '100%'
  46. },
  47. logo: {
  48. width: horizontalScale(84),
  49. height: verticalScale(84),
  50. },
  51. logoContainer: {
  52. justifyContent: 'center',
  53. alignItems: 'center',
  54. },
  55. buttonContainer: { paddingVertical: verticalScale(8) },
  56. accordionContainer: {
  57. paddingVertical: verticalScale(16),
  58. gap: moderateScale(8),
  59. height: '100%'
  60. },
  61. accordionStyle: {
  62. backgroundColor: colors().dominant,
  63. borderRadius: moderateScale(8),
  64. borderWidth: moderateScale(2),
  65. borderColor: colors().grayShade_400,
  66. maxHeight: verticalScale(64),
  67. },
  68. accordionTextStyle: { fontFamily: fonts.type.medium, color: colors().recessive },
  69. accordionItemStyle: {
  70. backgroundColor: colors().dominant
  71. },
  72. accordionItemTextStyle: {
  73. color: colors().recessive,
  74. fontFamily: fonts.type.regular
  75. },
  76. });
  77. const theme = useContext(ThemeContext)
  78. const [isDarkMode, setDarkMode] = useState(theme.state.theme === "light" ? false : true)
  79. useEffect(() => {
  80. if (theme.state.theme === 'light') {
  81. setDarkMode(false)
  82. } else {
  83. setDarkMode(true)
  84. }
  85. },[theme.state.theme])
  86. return (
  87. <View >
  88. <ScrollView style={{ backgroundColor: colors().dominant, minHeight: '100%' }} showsVerticalScrollIndicator={false}>
  89. <NewscoutHomeHeader backButtonShown={true} onBackClick={() => navigation.toggleDrawer()} />
  90. <View style={styles.sideBarContainer}>
  91. <View style={styles.accordionContainer}>
  92. <List.AccordionGroup
  93. expandedId={expandedId}
  94. onAccordionPress={handleAccordionPress}>
  95. {data.map(item => {
  96. let acc_check = item.heading.name.toLowerCase();
  97. return (
  98. <List.Accordion
  99. key={acc_check}
  100. id={acc_check}
  101. theme={{colors:{background: colors().dominant}}}
  102. style={[
  103. styles.accordionStyle,
  104. acc_check === expandedId && {
  105. backgroundColor: colors().secondaryColor,
  106. borderColor: colors().secondaryColor,
  107. },
  108. ]}
  109. titleStyle={[
  110. ,
  111. styles.accordionTextStyle,
  112. acc_check === expandedId && {
  113. color: colors().white,
  114. fontFamily: fonts.type.semibold,
  115. },
  116. ]}
  117. onPress={() => {}}
  118. left={() => (
  119. <View style={{ paddingLeft: 16 }}>
  120. <Image
  121. source={{ uri: BASE_URL + "/" + item.heading.icon }}
  122. style={{
  123. height: 24,
  124. width: 24,
  125. tintColor:
  126. acc_check === expandedId ?
  127. colors().white :
  128. colors().primaryColor
  129. }}
  130. />
  131. </View>
  132. )}
  133. right={() => (
  134. <EntypoIcon
  135. name={
  136. acc_check === expandedId ? 'chevron-up' : 'chevron-down'
  137. }
  138. size={moderateScale(16)}
  139. color={
  140. acc_check === expandedId ? colors().white : colors().recessive
  141. }
  142. />
  143. )}
  144. title={item.heading.name}>
  145. {item.heading.submenu.map(subTitle => (
  146. <TouchableOpacity
  147. onPress={
  148. () => {
  149. navigation.toggleDrawer()
  150. navigateToListViewPage(navigation, "category", subTitle.name)
  151. }}
  152. >
  153. <List.Item
  154. key={`${item.title}_${subTitle.name}`}
  155. // key={`${item.title}_${item.subTitles.indexOf(subTitle)}`}
  156. //key={`Sidebar_${item.title}_${item.subTitles.indexOf(subTitle)}`}
  157. style={styles.accordionItemStyle}
  158. titleStyle={styles.accordionItemTextStyle}
  159. title={subTitle.name}
  160. />
  161. </TouchableOpacity>
  162. ))}
  163. </List.Accordion>
  164. );
  165. })}
  166. </List.AccordionGroup>
  167. <List.Item
  168. style={styles.accordionStyle}
  169. titleStyle={styles.accordionTextStyle}
  170. title="Switch Theme"
  171. left={props => <List.Icon {...props} icon="theme-light-dark" color={colors().primaryColor} />}
  172. right={props => <CustomSwitch
  173. onSelectSwitch={() => {
  174. setDarkMode(!isDarkMode)
  175. if (isDarkMode !== false){
  176. theme.dispatch({ type: "LIGHTMODE" });
  177. setDarkMode(false)
  178. }
  179. else{
  180. theme.dispatch({ type: "DARKMODE" });
  181. setDarkMode(true)
  182. }
  183. }}
  184. value={isDarkMode}
  185. />}
  186. />
  187. <List.Item
  188. style={styles.accordionStyle}
  189. titleStyle={styles.accordionTextStyle}
  190. title="Reader Mode"
  191. left={props => <List.Icon {...props} icon="book" color={colors().primaryColor} />}
  192. />
  193. </View>
  194. </View>
  195. </ScrollView>
  196. </View>
  197. );
  198. };
  199. export default SidebarPage;