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 | /* 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 { RegisterCredsValues } from "../utils/RegistrationUtils"; import { API_URL, reportAxiosError } from "../utils/RequestUtils"; import { useAuth } from "./useAuth"; /** * Interface that contains data in a user's profile. */ export interface UserProfile { /** * The user's id */ id?: string /** * The user's username */ username?: string /** * The user's email */ email?: string /** * The user's base64 encoded profile picture. */ image_b64?: string /** * The user's preference for seeing tips. */ show_tips?: boolean } export interface UserNotification { id: string user: string title: string data: string read: boolean } /** * A custom hook containing [react-query]{@link https://react-query.tanstack.com/} queries and mutations and other logic related to interacting with {@link UserProfile} objects. * @category Hooks * @namespace useProfile */ export const useProfile = (userId: string) => { /** * 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.warn("[ProfileData]: Something went wrong when retrieving query client: " + error) } const { refreshAccessToken } = useAuth(); /** * The callback responsible for retrieving the {@link Profile} matching the given ID from the API , used by the [react-query useQuery]{@link https://react-query.tanstack.com/reference/useQuery#_top} hook. * * @memberOf useProfile */ const getProfile = async (id: string) => { Iif (userId) { const config: AxiosRequestConfig = { method: 'GET', url: API_URL + `/api/user-profile/${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() 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 user profile', error) throw new 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 getNotifications = async () => { Iif (authStore.userId) { let url = `${API_URL}/api/user-profile/notifications/${authStore.userId}/` 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 notifications', error) throw new Error; } } } const markNotificationRead = async (notificationId: string) => { const config: AxiosRequestConfig = { method: 'POST', url: API_URL + `/api/user-profile/notifications/mark-read/${notificationId}/`, 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 editing user profile', error) throw new Error; } } const removeNotificaiton = async (notificationId: string) => { const config: AxiosRequestConfig = { method: 'DELETE', url: API_URL + `/api/user-profile/notifications/delete/${notificationId}/`, 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 editing user profile', error) throw new Error; } } const editProfile = async (values: RegisterCredsValues) => { Iif (userId) { const config: AxiosRequestConfig = { method: 'PUT', url: API_URL + `/api/user-profile/${authStore.userId}/`, data: values, 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 editing user profile', error) throw new Error; } } } const changePassword = async (password: string) => { Iif (userId) { const config: AxiosRequestConfig = { method: 'POST', url: API_URL + `/api/user-profile/change-password/${authStore.userId}/`, data: {password: password}, 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 editing user profile', error) throw new Error; } } } //*NOT CURRENTLY IN USER* // const changePicture = async () => { // if (userId) { // const config: AxiosRequestConfig = { // method: 'POST', // url: API_URL + `/api/user-profile/change-picture/${authStore.userId}/`, // headers: { "Authorization": "Bearer " + authStore.accessToken, } // } // try { // const response = await axios(config); // return response.data; // } catch (error) { // if (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 // } // } // } // } // } const deleteProfile = async () => { Iif (userId) { const config: AxiosRequestConfig = { method: 'DELETE', url: API_URL + `/api/user-profile/${authStore.userId}/`, headers: { "Authorization": "Bearer " + authStore.accessToken, } } try { const response = await axios(config); Iif (response.status == 200) { await authStore.setAccessTokenAsync('') await authStore.setIdAsync('') await authStore.setNotificationTokenAsync('') await authStore.setRefreshTokenAsync('') } } 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 deleting when editing user profile', error) throw new Error; } } } const toggleTips = async () => { Iif (userId) { const config: AxiosRequestConfig = { method: 'POST', url: API_URL + `/api/user-profile/toggle-tips/${authStore.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 editing user profile', error) throw new Error; } } } // get profile query const { data: profile, status: getProfileStatus, refetch: refetchProfile } = useQuery<UserProfile, Error>(['getProfile', userId], async () => getProfile(userId)) // get notifications query const { data: notifications, status: getNotificationsStatus, refetch: refetchNotifications } = useQuery<UserNotification[], Error>(['getNotifications', userId], async () => getNotifications()) // edit profile query const { status: updateProfileStatus, mutateAsync: updateProfile, reset: resetUpdateProfile } = useMutation(editProfile, { onSuccess: () => { queryClient.invalidateQueries('getProfile')}, onError: () => queryClient.invalidateQueries('getProfile'), }) const { status: deleteAccountStatus, mutateAsync: deleteAccount, reset: resetDeleteAccount } = useMutation(deleteProfile, { onSuccess: () => { queryClient.invalidateQueries('getProfile')}, onError: () => queryClient.invalidateQueries('getProfile'), }) const { status: markNotificationReadStatus, mutateAsync: markNotificationAsRead, reset: resetMarkNotificationRead } = useMutation(markNotificationRead, { onSuccess: () => { queryClient.invalidateQueries('getNotifications')}, onError: () => queryClient.invalidateQueries('getNotifications'), }) const { status: deleteNotificationStatus, mutateAsync: deleteNotification, reset: resetDeleteNotification } = useMutation(removeNotificaiton, { onSuccess: () => { queryClient.invalidateQueries('getNotifications')}, onError: () => queryClient.invalidateQueries('getNotifications'), }) // toggle tips const { status: changePasswordStatus, mutateAsync: changePasswordAsync, reset: resetChangePassword } = useMutation(changePassword, { onSuccess: () => { queryClient.invalidateQueries('getProfile')}, onError: () => queryClient.invalidateQueries('getProfile'), }) // toggle tips const { status: toggleTipsStatus, mutateAsync: toggleTipsAsync, reset: resetToggleTips } = useMutation(toggleTips, { onSuccess: () => { queryClient.invalidateQueries('getProfile')}, onError: () => queryClient.invalidateQueries('getProfile'), }) return { profile, getProfileStatus, refetchProfile, //reading updateProfile, updateProfileStatus, resetUpdateProfile, //editing deleteAccount, deleteAccountStatus, resetDeleteAccount, //deleting notifications, getNotificationsStatus, refetchNotifications, //notifications deleteNotification, deleteNotificationStatus, resetDeleteNotification, //delete notification markNotificationAsRead, markNotificationReadStatus, resetMarkNotificationRead, //update notifications changePasswordAsync, changePasswordStatus, resetChangePassword, //changePassword toggleTipsAsync, toggleTipsStatus //toggleTips } } |