Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. // React dependencies
  2. import React, { useState, useEffect } from "react";
  3. import { ScrollView } from "react-native";
  4.  
  5. // Screen Icon
  6. import EmptyIcon from "../../assets/Shocked.png";
  7.  
  8. // Higher-order-components
  9. import { withTheme } from "styled-components";
  10. import { NavigationEvents } from "react-navigation";
  11.  
  12. // Stateless components
  13. import Empty from "../Components/UI/States/Empty";
  14. import ActionButton from "../Components/UI/Action-Blocks/ActionButton";
  15. import Modal from "../Components/UI/Modal";
  16. import ShoppingListCard from "../Components/UI/Cards/ShoppingLists/ShoppingList";
  17. import Loading from "../Components/UI/States/Loading";
  18. import { TextInput } from "react-native-paper";
  19.  
  20. /*
  21. Context API Consumer:
  22. - Wraps the entire component, Consumer then renders the children (This components JSX)
  23. - When using the <Consumer> component you can access the value which has access to the entire state and the dispatch method
  24.  
  25. Usage:
  26.  
  27. <Consumer>
  28. {value => {
  29. <Text colour={value.isDark : "White" : "Black"}> Hello </Text>
  30. }}
  31. </Consumer>
  32. */
  33. import { Consumer } from "../Context";
  34. import { getItem, setItem } from "../Utils/AsyncStorage";
  35.  
  36. // Utility libraries
  37. import shortid from "shortid";
  38. import moment from "moment";
  39.  
  40. const ShoppingLists = props => {
  41. const [
  42. isCreateShoppingModalVisible,
  43. setIsCreateShoppingModalVisible
  44. ] = useState(false);
  45. const [isLoading, setIsLoading] = useState(false);
  46. const [shoppingListName, setShoppingListName] = useState("");
  47. const [shoppingLists, setShoppingLists] = useState([]);
  48.  
  49. useEffect(() => {
  50. fetchShoppingLists();
  51. }, []);
  52.  
  53. const fetchShoppingLists = async () => {
  54. console.log("Fetching all the shopping lists avaliable");
  55. setIsLoading(!isLoading);
  56. try {
  57. const value = await getItem("ShoppingLists");
  58. if (value) {
  59. setShoppingLists(value);
  60. setIsLoading(false);
  61. } else {
  62. setShoppingLists([]);
  63. setIsLoading(false);
  64. }
  65. console.log(value);
  66. } catch (e) {
  67. console.log(e);
  68. }
  69. };
  70.  
  71. createShoppingList = async () => {
  72. setIsLoading(!isLoading);
  73.  
  74. // Material colours
  75. const red = "#e53935"; // Red with a shade of 500
  76. const green = "#43a047"; // Green with a shade of 500
  77. const blue = "#2196f3"; // Blue with a shade of 500
  78. const purple = "#9c27b0"; // Purple with a shade of 500
  79.  
  80. const shoppingListThemes = [red, blue, green, purple];
  81. let randomTheme =
  82. shoppingListThemes[Math.floor(Math.random() * shoppingListThemes.length)];
  83.  
  84. try {
  85. let shoppingListData = {
  86. id: shortid.generate(),
  87. name: shoppingListName,
  88. createdOn: moment().format("Do MMMM YYYY"),
  89. shoppingListTheme: randomTheme,
  90. items: []
  91. };
  92.  
  93. // After updating this state I want to update local storage
  94. // This hook is used to update the state on load and when creating the shopping list. I only want to update the shopping list sotrage after calling this function
  95. setShoppingLists([...shoppingLists, shoppingListData]);
  96.  
  97.  
  98. setIsCreateShoppingModalVisible(false);
  99. setShoppingListName("");
  100. setIsLoading(false);
  101. } catch (err) {
  102. console.log(err);
  103. }
  104. };
  105.  
  106. if (isLoading) {
  107. return (
  108. <Consumer>
  109. {value => {
  110. return <Loading isDark={value.isDark} />;
  111. }}
  112. </Consumer>
  113. );
  114. }
  115.  
  116. return (
  117. <Consumer>
  118. {value => {
  119. return (
  120. <>
  121. {/* <NavigationEvents onDidFocus={fetchShoppingLists} /> */}
  122.  
  123. <Modal
  124. isDark={value.isDark}
  125. visible={isCreateShoppingModalVisible}
  126. title="Create a shopping list"
  127. onDismiss={() =>
  128. setIsCreateShoppingModalVisible(!isCreateShoppingModalVisible)
  129. }
  130. onCancel={() => {
  131. setIsCreateShoppingModalVisible(!isCreateShoppingModalVisible);
  132. setShoppingListName("");
  133. }}
  134. onOk={() => createShoppingList()}
  135. submitDisabled={shoppingListName < 1 ? true : false}
  136. >
  137. <TextInput
  138. placeholder="Enter a shopping list name"
  139. value={shoppingListName}
  140. onChangeText={value => setShoppingListName(value)}
  141. underlineColor="transparent"
  142. mode="flat"
  143. />
  144. </Modal>
  145.  
  146. <ScrollView
  147. showsHorizontalScrollIndicator={false}
  148. showsVerticalScrollIndicator={false}
  149. contentContainerStyle={{
  150. flexGrow: 1,
  151. backgroundColor: value.isDark
  152. ? props.theme.Primary
  153. : props.theme.Secondary
  154. }}
  155. >
  156. {shoppingLists.length < 1 ? (
  157. <Empty
  158. image={EmptyIcon}
  159. label="No shipping lists exist"
  160. heading="No shopping lists exist"
  161. overview="Why not try adding one ?"
  162. isDark={value.isDark}
  163. />
  164. ) : (
  165. shoppingLists.map((data, index) => {
  166. return (
  167. <ShoppingListCard
  168. key={index}
  169. title={data.name}
  170. background={data.shoppingListTheme}
  171. action={() =>
  172. // Passing data to the ShoppingList component
  173. props.navigation.navigate("ShoppingList", {
  174. ShoppingList: data,
  175. title: data.name
  176. })
  177. }
  178. />
  179. );
  180. })
  181. )}
  182. </ScrollView>
  183.  
  184. <ActionButton
  185. colour="white"
  186. icon={isCreateShoppingModalVisible ? "remove" : "add"}
  187. action={() =>
  188. setIsCreateShoppingModalVisible(!isCreateShoppingModalVisible)
  189. }
  190. />
  191. </>
  192. );
  193. }}
  194. </Consumer>
  195. );
  196. };
  197.  
  198. export default withTheme(ShoppingLists);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement