shadiff

new one

May 12th, 2022 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. ----------------------------------------------------------------------------------------------------
  2. context.js
  3. import React, {useState, useEffect} from 'react';
  4.  
  5. const ProductContext = React.createContext();
  6. const ProductConsumer = ProductContext.Consumer
  7.  
  8. export {ProductConsumer}
  9.  
  10.  
  11.  
  12. export function ProductProvider(props){
  13. // Responsible for holding all the data that goes into the Navbar
  14. const [entryStore, setEntryStore] = useState(null);
  15. // Responsible for opening and closing the sub container
  16. const [subContainer, setSubContainer] = useState(false);
  17. //Responsible for storing the subContainers children
  18. const [subContainerChildren, setSubContainerChildren] = useState([]);
  19.  
  20. useEffect(() => {
  21. fetch("http://127.0.0.1:8000/sidebar/").then(data => data.json()).then(response => {
  22. setEntryStore(response)
  23. })
  24. }, []);
  25.  
  26. const value = {
  27. entryStore,
  28. setEntryStore,
  29. subContainerChildren,
  30. setSubContainerChildren,
  31. subContainer,
  32. setSubContainer
  33. }
  34. return(
  35. <ProductContext.Provider value={value}>
  36. {props.children}
  37. </ProductContext.Provider>
  38. )
  39. }
  40.  
  41. export default ProductContext;
  42. ----------------------------------------------------------------------------------------------------
  43. subContainer.js
  44. import React from 'react'
  45. import ProductContext from '../Context/ProductContext'
  46. import {useContext} from 'react'
  47. import SideNavRow from './SideNavRow'
  48.  
  49. function SubContainer(props) {
  50. const {subContainerChildren, setSubContainer} = useContext(ProductContext)
  51.  
  52. return (
  53. <div className="sub-container" style={props.state === "entering" ? {animation: "moveSubContainer .3s forwards"} :
  54. props.state === "entered" ? {transform: "translateX(0px)"} : {animation: "moveSubContainer .3s reverse backwards"}}>
  55. <div className="sub-header" onClick={() => setSubContainer(false)} >
  56. <i className="fas fa-chevron-left"></i> MAIN MENU
  57. </div>
  58. {subContainerChildren.map((subEntry => (
  59. <>
  60. <SideNavRow text={subEntry.title} />
  61. </>
  62. )))}
  63.  
  64. <div style={{minHeight:"60px"}}></div>
  65. </div>
  66. );
  67. }
  68. export default SubContainer;
  69. ------------------------------------------------------------------------------------------------------
  70. SideNavRow.js
  71.  
  72. ------import React from 'react';
  73. import ProductContext from '../Context/ProductContext'
  74. import {useContext} from 'react'
  75.  
  76.  
  77. function SideNavRow(props) {
  78. const {setSubContainer, setSubContainerChildren} = useContext(ProductContext)
  79. console.log(setSubContainerChildren)
  80.  
  81.  
  82. const openRow = () => {
  83. setSubContainer(true);
  84. setSubContainerChildren(props.children);
  85. }
  86.  
  87. return (
  88. <div className="sidenavRow" onClick={() => (openRow())}>
  89. <div>{props.text}</div>
  90. <i className="fas fa-chevron-right"></i>
  91. </div>
  92. )
  93. }
  94. export default SideNavRow;
  95. --------------------------------------------------------------------------------------------------
  96. json
  97. [
  98. {
  99. "id": 3,
  100. "title": "GIFTS BY CATEGORY",
  101. "parent": null,
  102. "type": 3,
  103. "children": [
  104. {
  105. "id": 20,
  106. "title": "Funny",
  107. "parent": 3,
  108. "type": null,
  109. "children": []
  110. },
  111. {
  112. "id": 18,
  113. "title": "Gamers",
  114. "parent": 3,
  115. "type": null,
  116. "children": []
  117. },
  118. {
  119. "id": 19,
  120. "title": "Geek",
  121. "parent": 3,
  122. "type": null,
  123. "children": []
  124. },
  125. {
  126. "id": 21,
  127. "title": "Pranks",
  128. "parent": 3,
  129. "type": null,
  130. "children": []
  131. }
  132. ]
  133. },
  134. {
  135. "id": 2,
  136. "title": "GIFTS BY OCCASSION",
  137. "parent": null,
  138. "type": 4,
  139. "children": [
  140. {
  141. "id": 15,
  142. "title": "Bridesmaids",
  143. "parent": 2,
  144. "type": null,
  145. "children": []
  146. },
  147. {
  148. "id": 14,
  149. "title": "Graduation",
  150. "parent": 2,
  151. "type": null,
  152. "children": []
  153. },
  154. {
  155. "id": 16,
  156. "title": "Groomsmen",
  157. "parent": 2,
  158. "type": null,
  159. "children": []
  160. },
  161. {
  162. "id": 17,
  163. "title": "Wedding",
  164. "parent": 2,
  165. "type": null,
  166. "children": []
  167. }
  168. ]
  169. },
  170. {
  171. "id": 1,
  172. "title": "GIFTS BY RECIPIENT",
  173. "parent": null,
  174. "type": 5,
  175. "children": [
  176. {
  177. "id": 10,
  178. "title": "Dad",
  179. "parent": 1,
  180. "type": null,
  181. "children": []
  182. },
  183. {
  184. "id": 9,
  185. "title": "Men",
  186. "parent": 1,
  187. "type": null,
  188. "children": []
  189. },
  190. {
  191. "id": 12,
  192. "title": "Mom",
  193. "parent": 1,
  194. "type": null,
  195. "children": []
  196. },
  197. {
  198. "id": 11,
  199. "title": "Women",
  200. "parent": 1,
  201. "type": null,
  202. "children": []
  203. }
  204. ]
  205. },
  206. {
  207. "id": 22,
  208. "title": "Help & Settings",
  209. "parent": null,
  210. "type": 6,
  211. "children": [
  212. {
  213. "id": 24,
  214. "title": "Customer Service",
  215. "parent": 22,
  216. "type": 8,
  217. "children": []
  218. },
  219. {
  220. "id": 23,
  221. "title": "Your Account",
  222. "parent": 22,
  223. "type": 7,
  224. "children": []
  225. }
  226. ]
  227. },
  228. {
  229. "id": 4,
  230. "title": "MORE CATEGORIES",
  231. "parent": null,
  232. "type": 2,
  233. "children": [
  234. {
  235. "id": 13,
  236. "title": "All Gift Guides",
  237. "parent": 4,
  238. "type": null,
  239. "children": []
  240. },
  241. {
  242. "id": 7,
  243. "title": "Contact Us",
  244. "parent": 4,
  245. "type": null,
  246. "children": []
  247. },
  248. {
  249. "id": 8,
  250. "title": "Random",
  251. "parent": 4,
  252. "type": null,
  253. "children": []
  254. },
  255. {
  256. "id": 6,
  257. "title": "Submit a product",
  258. "parent": 4,
  259. "type": null,
  260. "children": []
  261. }
  262. ]
  263. },
  264. {
  265. "id": 5,
  266. "title": "WHAT'S NEW",
  267. "parent": null,
  268. "type": 1,
  269. "children": []
  270. }
  271. ]
Add Comment
Please, Sign In to add comment