MapContext.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import React, { createContext, useReducer, useContext } from "react";
  2. import { v4 as uuidv4 } from 'uuid';
  3. function reducer(state, action) {
  4. switch (action.type) {
  5. case "UPDATE_SELECTED_LANDMARK":
  6. return { ...state, selectedLandmark: action.payload, };
  7. case "UPDATE_LANDMARKS":
  8. return { ...state, landmarks: action.payload, };
  9. case "UPDATE_PLACES":
  10. return { ...state, places: action.payload, };
  11. case "UPDATE_SELECTED_PLACES":
  12. return { ...state, selectedPlaces: action.payload, };
  13. case "UPDATE_SELECTED_PLACE":
  14. return { ...state, selectedPlace: action.payload, };
  15. case "UPDATE_LOCATION_PERMISSION":
  16. return { ...state, locationPermission: action.payload, };
  17. case "UPDATE_LOCATION":
  18. return { ...state, location: action.payload, };
  19. default:
  20. console.log(state);
  21. console.log(action);
  22. throw new Error();
  23. }
  24. }
  25. // export const seedLandmarkData = () => {
  26. // return [
  27. // {
  28. // id: uuidv4(),
  29. // rating: 5,
  30. // postedBy: 'cdmoss',
  31. // latitude: 53.48914616441755,
  32. // longitude: -113.49762260454305,
  33. // dateAdded: new Date(),
  34. // title: "Allard way pothole",
  35. // desc: "A massive pothole on the Allard Way sidewalk.",
  36. // icon: 'pothole',
  37. // },
  38. // {
  39. // id: uuidv4(),
  40. // rating: 4,
  41. // postedBy: 'cdmoss',
  42. // latitude: 53.51839016964649,
  43. // longitude: -113.49473594001775,
  44. // dateAdded: new Date(),
  45. // title: "Accessible bathroom on Whyte",
  46. // desc: "A conveniently placed accessible bathroom.",
  47. // icon: 'washroom',
  48. // comments: [
  49. // ]
  50. // },
  51. // {
  52. // id: uuidv4(),
  53. // rating: 0,
  54. // postedBy: 'cendersby',
  55. // latitude: 53.48963,
  56. // longitude: -113.49401,
  57. // dateAdded: new Date(),
  58. // title: "Peter's Drive-in Construction",
  59. // desc: "The sidewalk in front of Peter's is being rebuilt.",
  60. // icon: 'roadblock',
  61. // comments: [
  62. // {
  63. // id: uuidv4(),
  64. // name: "shampshire",
  65. // dateAdded: new Date(),
  66. // text: "No! Not peters!"
  67. // },
  68. // {
  69. // id: uuidv4(),
  70. // name: "nmaeda",
  71. // dateAdded: new Date(),
  72. // text: "Good, Peter's is gross!"
  73. // },
  74. // ]
  75. // },
  76. // ];
  77. // };
  78. // const seedPlaces = () => {
  79. // return [{
  80. // id: uuidv4(),
  81. // color: 'green',
  82. // postedBy: 'cdmoss',
  83. // coordinates: [
  84. // [
  85. // [
  86. // -113.53052616119385,
  87. // 53.53087032262937
  88. // ],
  89. // [
  90. // -113.53065490722656,
  91. // 53.523038509337624
  92. // ],
  93. // [
  94. // -113.53241443634032,
  95. // 53.52298748300469
  96. // ],
  97. // [
  98. // -113.53250026702881,
  99. // 53.518701051607486
  100. // ],
  101. // [
  102. // -113.53310108184813,
  103. // 53.51783350677023
  104. // ],
  105. // [
  106. // -113.5234022140503,
  107. // 53.51640457066658
  108. // ],
  109. // [
  110. // -113.52087020874023,
  111. // 53.51811418498503
  112. // ],
  113. // [
  114. // -113.52095603942871,
  115. // 53.52298748300469
  116. // ],
  117. // [
  118. // -113.51490497589111,
  119. // 53.52291094339006
  120. // ],
  121. // [
  122. // -113.51499080657958,
  123. // 53.525895885886285
  124. // ],
  125. // [
  126. // -113.51833820343018,
  127. // 53.526941841607
  128. // ],
  129. // [
  130. // -113.52099895477295,
  131. // 53.52854899152384
  132. // ],
  133. // [
  134. // -113.52481842041016,
  135. // 53.529773445765805
  136. // ],
  137. // [
  138. // -113.53052616119385,
  139. // 53.53087032262937
  140. // ]
  141. // ]
  142. // ],
  143. // dateAdded: new Date(),
  144. // name: "UofA",
  145. // desc: "The University of Alberta.",
  146. // tips: [
  147. // {
  148. // id: uuidv4(),
  149. // name: "shampshire",
  150. // dateAdded: new Date(),
  151. // text: "There are lots of great accessibility features at the University.",
  152. // rating: 3,
  153. // liked: false,
  154. // },
  155. // ]
  156. // },
  157. // {
  158. // id: uuidv4(),
  159. // postedBy: 'cdmoss',
  160. // coordinates: [
  161. // [
  162. // [
  163. // -113.52713584899902,
  164. // 53.52285991690344
  165. // ],
  166. // [
  167. // -113.52730751037598,
  168. // 53.52074226350543
  169. // ],
  170. // [
  171. // -113.52591276168823,
  172. // 53.52081880703848
  173. // ],
  174. // [
  175. // -113.52580547332764,
  176. // 53.522847160272185
  177. // ],
  178. // [
  179. // -113.52713584899902,
  180. // 53.52285991690344
  181. // ]
  182. // ]
  183. // ],
  184. // dateAdded: new Date(),
  185. // name: "ECHA",
  186. // color: 'red',
  187. // desc: "The Edmonton Clinic Health Academy building.",
  188. // tips: [
  189. // {
  190. // id: uuidv4(),
  191. // name: "shampshire",
  192. // dateAdded: new Date(),
  193. // text: "There are lots of great accessibility features at ECHA.",
  194. // rating: 3,
  195. // liked: false
  196. // },
  197. // ]
  198. // },
  199. // {
  200. // id: uuidv4(),
  201. // rating: 4,
  202. // postedBy: 'cdmoss',
  203. // color: 'blue',
  204. // dateAdded: new Date(),
  205. // name: "SUB",
  206. // desc: "The Student Union Building.",
  207. // tips: [
  208. // {
  209. // id: uuidv4(),
  210. // name: "shampshire",
  211. // dateAdded: new Date(),
  212. // text: "There are lots of great accessibility features at SUB.",
  213. // rating: 3,
  214. // liked: false
  215. // },
  216. // ],
  217. // coordinates: [
  218. // [
  219. // [
  220. // -113.52796196937561,
  221. // 53.525691793953534
  222. // ],
  223. // [
  224. // -113.52800488471983,
  225. // 53.5249710864483
  226. // ],
  227. // [
  228. // -113.5263204574585,
  229. // 53.5249710864483
  230. // ],
  231. // [
  232. // -113.52630972862244,
  233. // 53.525398410077315
  234. // ],
  235. // [
  236. // -113.52684617042542,
  237. // 53.52544943350612
  238. // ],
  239. // [
  240. // -113.5268783569336,
  241. // 53.52566628239279
  242. // ],
  243. // [
  244. // -113.52796196937561,
  245. // 53.525691793953534
  246. // ]
  247. // ]
  248. // ]
  249. // }]
  250. // }
  251. const state = {
  252. location: null,
  253. locationPermission: false,
  254. landmarks: [],
  255. selectedLandmark: {
  256. icon: 'barrier'
  257. },
  258. places: [],
  259. selectedPlaces: [],
  260. selectedPlace: {name: ''},
  261. }
  262. export const MapContext = createContext(state);
  263. export const MapProvider = ({ children }) => {
  264. const [mapState, mapDispatch] = useReducer(reducer, state);
  265. return (
  266. <MapContext.Provider value={{ mapState, mapDispatch }}>
  267. {children}
  268. </MapContext.Provider>
  269. );
  270. };
  271. export const useMapState = () => {
  272. const context = useContext(MapContext);
  273. if (context === undefined) {
  274. throw new Error(
  275. "useMapState must be used within a MapProvider"
  276. );
  277. }
  278. return context;
  279. }