MockContext.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import React, { createContext, useReducer, useContext } from "react";
  2. import { v4 as uuidv4 } from 'uuid';
  3. function mockReducer(state, action) {
  4. switch (action.type) {
  5. case "CHANGE_TABBAR_VISIBILITY":
  6. return { ...state, tabBarVisible: action.payload}
  7. case "LOGIN":
  8. return { ...state, loggedIn: true, };
  9. case "LOGOUT":
  10. return { ...state, loggedIn: false, };
  11. case "UPDATE_SELECTED_LANDMARK":
  12. return { ...state, selectedLandmark: action.payload, };
  13. case "UPDATE_LANDMARKS":
  14. return { ...state, landmarks: 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. throw new Error();
  21. }
  22. }
  23. export const seedLandmarkData = () => {
  24. return [
  25. {
  26. id: uuidv4(),
  27. rating: 5,
  28. postedBy: 'cdmoss',
  29. latitude: 53.51803,
  30. longitude: -113.46831,
  31. dateAdded: Date.now(),
  32. title: "Millcreek Dental pothole",
  33. desc: "A small pothole right in front of the doors to the Millcreek dental clinic.",
  34. icon: 'pothole',
  35. comments: [
  36. {
  37. id: uuidv4(),
  38. dateAdded: new Date(),
  39. name: "Brian Bell",
  40. text: "This was very helpful! Thanks!"
  41. },
  42. {
  43. id: uuidv4(),
  44. dateAdded: new Date(),
  45. name: "Martin Ferguson-Pell",
  46. text: "The pothole is in front of the left door, try use the right one!"
  47. },
  48. {
  49. id: uuidv4(),
  50. name: "cdmoss",
  51. dateAdded: new Date(),
  52. text: "Potholes suck!"
  53. },
  54. {
  55. id: uuidv4(),
  56. name: "Nathan Maeda",
  57. dateAdded: new Date(),
  58. text: "I've notified the city, they should be fixing this within the next week."
  59. },
  60. ]
  61. },
  62. {
  63. id: uuidv4(),
  64. rating: 4,
  65. postedBy: 'cdmoss',
  66. latitude: 53.51839016964649,
  67. longitude: -113.49473594001775,
  68. dateAdded: Date.now(),
  69. title: "Accessible bathroom on Whyte",
  70. desc: "A conveniently placed accessible bathroom.",
  71. icon: 'washroom',
  72. comments: [
  73. ]
  74. },
  75. {
  76. id: uuidv4(),
  77. rating: 0,
  78. postedBy: 'cendersby',
  79. latitude: 53.48963,
  80. longitude: -113.49401,
  81. dateAdded: new Date(),
  82. title: "Peter's Drive-in Construction",
  83. desc: "The sidewalk in front of Peter's is being rebuilt.",
  84. icon: 'roadblock',
  85. comments: [
  86. {
  87. id: uuidv4(),
  88. name: "shampshire",
  89. dateAdded: new Date(),
  90. text: "No! Not peters!"
  91. },
  92. {
  93. id: uuidv4(),
  94. name: "nmaeda",
  95. dateAdded: new Date(),
  96. text: "Good, Peter's is gross!"
  97. },
  98. ]
  99. },
  100. ];
  101. };
  102. const mockState = {
  103. tabBarVisible: true,
  104. location: null,
  105. locationPermission: false,
  106. loggedIn: false,
  107. username: "cdmoss",
  108. email: "cdmoss@gmail.com",
  109. rank: 0,
  110. height: 170,
  111. weight: 91,
  112. landmarks: seedLandmarkData(),
  113. places: null,
  114. placeTips: null,
  115. selectedLandmark: {
  116. icon: 'barrier'
  117. },
  118. }
  119. export const MockContext = createContext(mockState);
  120. export const MockProvider = ({ children }) => {
  121. const [state, dispatch] = useReducer(mockReducer, mockState);
  122. return (
  123. <MockContext.Provider value={{ state, dispatch }}>
  124. {children}
  125. </MockContext.Provider>
  126. );
  127. };
  128. export const useMockState = () => {
  129. const context = useContext(MockContext);
  130. if (context === undefined) {
  131. throw new Error(
  132. "useMockState must be used within a MockProvider"
  133. );
  134. }
  135. return context;
  136. }