Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | /* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved * Unauthorized copying of this file, via any medium is strictly prohibited * Proprietary and confidential * Written and maintained by the Click & Push Development team * <dev@clicknpush.ca>, January 2022 */ import axios, { AxiosRequestConfig } from "axios"; import { QueryClient, useMutation, useQuery, useQueryClient } from "react-query"; import { authStore } from "../libs/auth/AuthStore"; import { API_URL, reportAxiosError } from "../utils/RequestUtils"; import { useAuth } from "./useAuth"; import { LMComment } from "./useComments"; /** * Interface representing a landmark object */ export interface Landmark { /** * The id of the landmark. */ id?: string | null, /** * The rating of the landmark. */ rating?: number | null, /** * The id of the user who created the landmark. */ user?: string | null, /** * The x coordinate (or longitude) of the landmark's location. */ longitude?: number | null, /** * The y coordinate (or latitude) of the landmark's location. */ latitude?: number | null, /** * The landmark's title. */ title?: string | null, /** * The landmark's description. */ description?: string | null, /** * User [comments]{@link LMComment} associated with this landmark. */ comments?: LMComment[] | null, /** * [Photos]{@link LMPhoto} associated with this landmark. */ photos?: LMPhoto[] | null, /** * An integer representing the type of this landmark. */ landmark_type?: number | null, // for working with existing database schema, should be changed /** * A Date object representing when this landmark was created. */ time?: Date | null, //TODO: add floor property } export interface LMPhoto { id: string landmark: string, image_b64: string height: number, width: number } export interface UseLandmarkOptions { userId?: string landmarkId?: string userLMPairing?: {userId?: string, landmarkId?: string} changingPassword?: boolean } /** * A custom hook containing [react-query]{@link https://react-query.tanstack.com/} queries and mutations and other logic related to interacting with {@link Landmark} objects. * @category Hooks * @namespace useLandmarks */ export const useLandmarks = (options?: UseLandmarkOptions) => { const { refreshAccessToken } = useAuth(); /** * The local instance of the [react-query QueryClient]{@link https://react-query.tanstack.com/reference/QueryClient#_top}. * @memberOf useLandmarks */ let queryClient: QueryClient; try { queryClient = useQueryClient() } catch (error) { console.error("[LandmarkData]: Something went wrong when retrieving query client: " + error) } /** * The callback responsible for retrieving {@link Landmark} from the API, used by the [react-query useQuery]{@link https://react-query.tanstack.com/reference/useQuery#_top} hook. * * @memberOf useLandmarks */ const getLandmarks = async () => { let url = `${API_URL}/api/landmarks/` const config: AxiosRequestConfig = { method: 'GET', url: url, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } // } // else { // return []; // } } /** * The callback responsible for retrieving photos for a {@link Landmark}, used by the [react-query useQuery]{@link https://react-query.tanstack.com/reference/useQuery#_top} hook. * * @memberOf useLandmarks */ const getLandmark = async (landmarkId?: string) => { Iif (landmarkId) { let url = `${API_URL}/api/landmark/${landmarkId}` const config: AxiosRequestConfig = { method: 'GET', url: url, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } } // } // else { // return []; // } } const addLandmarkPhoto = async (photo: LMPhoto) => { Iif (photo) { let url = `${API_URL}/api/landmark/photos/` const config: AxiosRequestConfig = { method: 'POST', url: url, data: photo, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } } } const deleteLandmarkPhoto = async (photoId: string) => { Iif (photoId) { let url = `${API_URL}/api/landmark/photos/${photoId}` const config: AxiosRequestConfig = { method: 'DELETE', url: url, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } } // } // else { // return []; // } } const checkIfRatedByUser = async (userLMPairing: {userId?: string, landmarkId?: string}) => { Iif (options?.userLMPairing) { if (userLMPairing?.landmarkId && userLMPairing?.userId) { const config: AxiosRequestConfig = { method: 'GET', url: `${API_URL}/api/landmark/check-rate-pairing/?landmark=${options?.userLMPairing?.landmarkId}&user=${options.userLMPairing.userId}`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data; } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data; } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } } else { console.warn("[LandmarkData]: Can't check landmark rating pairing. Given id values must not be null.") } } } /** * The callback responsible for adding a new {@link Landmark} to the server, used by the [react-query useMutation]{@link https://react-query.tanstack.com/reference/useMutation#_top} hook. * * @memberOf useLandmarks */ const createLandmark = async (data: {landmarkValue: Landmark | undefined, photos?: LMPhoto[]}): Promise<Landmark | undefined> => { if (data.landmarkValue) { const config: AxiosRequestConfig = { method: 'POST', data: { landmark: data.landmarkValue, photos: data.photos }, url: API_URL + `/api/landmark/`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data; } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data; } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('[LandmarkData]: Something went wrong when retrieving landmarks', error) throw new Error; } } else { console.warn("[LandmarkData]: Can't create landmark. Given landmark value is null.") } } /** * The callback responsible for updating a {@link Landmark} on the server, used by the [react-query useMutation]{@link https://react-query.tanstack.com/reference/useMutation#_top} hook. * * @memberOf useLandmarks */ const editLandmark = async (landmarkValue: Landmark) => { if (landmarkValue) { const config: AxiosRequestConfig = { method: 'PUT', data: landmarkValue, url: API_URL + `/api/landmark/`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data; } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data; } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('Something went wrong when retrieving landmarks', error) throw new Error; } } else { console.warn("[LandmarkData]: Can't update landmark. Given landmark value is null.") } } /** * The callback responsible for updating a {@link Landmark} on the server, used by the [react-query useMutation]{@link https://react-query.tanstack.com/reference/useMutation#_top} hook. * * @memberOf useLandmarks */ const rateLandmark = async (data: {id: string, rating: 1 | -1}) => { const config: AxiosRequestConfig = { method: 'POST', data: data, url: API_URL + `/api/landmark/rate/`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data.rating; } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() const response = await axios({...config, headers: { "Authorization": "Bearer " + authStore.accessToken }}); return response.data; } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('[LandmarkData]: Something went wrong when retrieving landmarks', error) throw new Error; } } /** * The callback responsible for deleting a {@link Landmark} from the server, used by the [react-query useMutation]{@link https://react-query.tanstack.com/reference/useMutation#_top} hook. * * @memberOf useLandmarks */ const removeLandmark = async (id?: string | null) => { const config: AxiosRequestConfig = { method: 'DELETE', url: API_URL + `/api/landmark/${id}`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); return response.data; } catch (error) { Iif (error.response.status == 401) { try { await refreshAccessToken() // add new access token to header const response = await axios({...config, headers: {"Authorization": "Bearer " + authStore.accessToken}}); return response.data; } catch (error) { // refreshAccessToken will report errors } } reportAxiosError('[LandmarkData]: Something went wrong when retrieving landmarks', error) throw new Error; } } // get-all query const { data: landmarks, status: getLandmarksStatus, refetch: refetchLandmarks } = useQuery<Landmark[], Error>('getLandmarks', () => getLandmarks(), { placeholderData: () => queryClient.getQueryData('getLandmarks'), staleTime: 1000, refetchInterval: 30000, refetchOnReconnect: true, refetchOnMount: false }) // get-details query const { data: landmark, status: getLandmarkStatus, refetch: refetchLandmark } = useQuery<Landmark, Error>(['getLandmark', options?.landmarkId], () => getLandmark(options?.landmarkId), { placeholderData: () => queryClient.getQueryData('getLandmark'), refetchOnReconnect: true, refetchOnMount: false }) const { data: landmarkRatedByUser, status: checkIfRatedByUserStatus, refetch: refetchCheckIfRatedByUser } = useQuery<boolean, Error>(['checkIfRatedByUser', options?.userLMPairing], () => checkIfRatedByUser(options?.userLMPairing), { placeholderData: () => queryClient.getQueryData('checkIfRatedByUser'), refetchOnReconnect: true, refetchOnMount: false }) // mutations const { status: addLandmarkStatus, mutateAsync: addLandmarkAsync, reset: resetAddLm, data: newLandmark } = useMutation(createLandmark, { onSuccess: data => { queryClient.invalidateQueries('getLandmarks') }, }) const { status: updateLandmarkStatus, mutateAsync: updateLandmark, reset: resetUpdateLm } = useMutation(editLandmark, { onSuccess: () => { queryClient.invalidateQueries('getLandmarks')}, onError: () => queryClient.invalidateQueries('getLandmarks'), }) const { data: rating, status: rateLandmarkStatus, mutateAsync: rateLandmarkAsync, reset: resetRateLandmark } = useMutation(rateLandmark, { onSuccess: () => { queryClient.invalidateQueries('getLandmarks') queryClient.invalidateQueries('checkIfRatedByUser') }, onError: () => queryClient.invalidateQueries('getLandmarks'), }) const { status: deleteLandmarkStatus, mutateAsync: deleteLandmark, reset: resetDeleteLm } = useMutation(removeLandmark, { onSuccess: () => queryClient.invalidateQueries('getLandmarks'), onError: () => queryClient.invalidateQueries('getLandmarks'), }) const { status: addPhotoStatus, mutateAsync: addPhoto, reset: resetAddPhoto } = useMutation(addLandmarkPhoto, { onSuccess: () => { queryClient.invalidateQueries('getLandmark') }, onError: () => { queryClient.invalidateQueries('getLandmark') }, }) const { status: deletePhotoStatus, mutateAsync: deletePhoto, reset: resetDeletePhoto } = useMutation(deleteLandmarkPhoto, { onSuccess: () => { queryClient.invalidateQueries('getLandmark') }, onError: () => { queryClient.invalidateQueries('getLandmark') }, }) return { landmarks, getLandmarksStatus, refetchLandmarks, //reading landmark, getLandmarkStatus, refetchLandmark, //photos addPhoto, addPhotoStatus, resetAddPhoto, //add photos deletePhoto, deletePhotoStatus, resetDeletePhoto, //delete photos addLandmarkAsync, resetAddLm, addLandmarkStatus, newLandmark, // creating updateLandmark, resetUpdateLm, updateLandmarkStatus, // updating deleteLandmark, resetDeleteLm, deleteLandmarkStatus, // deleting rateLandmarkAsync, resetRateLandmark, rateLandmarkStatus, rating, // rating landmarkRatedByUser, checkIfRatedByUserStatus, refetchCheckIfRatedByUser // check if landmark is rated by user } } |