Application Android et IOS pour l'amicale des élèves https://play.google.com/store/apps/details?id=fr.amicaleinsat.application
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

customHooks.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2019 - 2020 Arnaud Vergnet.
  3. *
  4. * This file is part of Campus INSAT.
  5. *
  6. * Campus INSAT is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Campus INSAT is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Campus INSAT. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import { DependencyList, useEffect, useRef, useState } from 'react';
  20. import { REQUEST_STATUS } from './Requests';
  21. export function useMountEffect(func: () => void) {
  22. // eslint-disable-next-line react-hooks/exhaustive-deps
  23. useEffect(func, []);
  24. }
  25. /**
  26. * Effect that does not run on first render
  27. *
  28. * @param effect
  29. * @param deps
  30. */
  31. export function useSubsequentEffect(effect: () => void, deps?: DependencyList) {
  32. const didMountRef = useRef(false);
  33. useEffect(
  34. () => {
  35. if (didMountRef.current) {
  36. effect();
  37. } else {
  38. didMountRef.current = true;
  39. }
  40. },
  41. // eslint-disable-next-line react-hooks/exhaustive-deps
  42. deps ? deps : []
  43. );
  44. }
  45. export function useRequestLogic<T>(
  46. request: () => Promise<T>,
  47. cache?: T,
  48. onCacheUpdate?: (newCache: T) => void,
  49. startLoading?: boolean,
  50. minRefreshTime?: number
  51. ) {
  52. const [response, setResponse] = useState<{
  53. loading: boolean;
  54. lastRefreshDate?: Date;
  55. status: REQUEST_STATUS;
  56. code?: number;
  57. data: T | undefined;
  58. }>({
  59. loading: startLoading !== false && cache === undefined,
  60. lastRefreshDate: undefined,
  61. status: REQUEST_STATUS.SUCCESS,
  62. code: undefined,
  63. data: undefined,
  64. });
  65. const refreshData = (newRequest?: () => Promise<T>) => {
  66. let canRefresh;
  67. if (response.lastRefreshDate && minRefreshTime) {
  68. canRefresh =
  69. new Date().getTime() - response.lastRefreshDate.getTime() >
  70. minRefreshTime;
  71. } else {
  72. canRefresh = true;
  73. }
  74. if (canRefresh) {
  75. if (!response.loading) {
  76. setResponse((prevState) => ({
  77. ...prevState,
  78. loading: true,
  79. }));
  80. }
  81. const r = newRequest ? newRequest : request;
  82. r()
  83. .then((requestResponse: T) => {
  84. setResponse({
  85. loading: false,
  86. lastRefreshDate: new Date(),
  87. status: REQUEST_STATUS.SUCCESS,
  88. code: undefined,
  89. data: requestResponse,
  90. });
  91. if (onCacheUpdate) {
  92. onCacheUpdate(requestResponse);
  93. }
  94. })
  95. .catch(() => {
  96. setResponse((prevState) => ({
  97. loading: false,
  98. lastRefreshDate: prevState.lastRefreshDate,
  99. status: REQUEST_STATUS.CONNECTION_ERROR,
  100. code: 0,
  101. data: prevState.data,
  102. }));
  103. });
  104. }
  105. };
  106. const value: [
  107. boolean,
  108. Date | undefined,
  109. REQUEST_STATUS,
  110. number | undefined,
  111. T | undefined,
  112. (newRequest?: () => Promise<T>) => void
  113. ] = [
  114. response.loading,
  115. response.lastRefreshDate,
  116. response.status,
  117. response.code,
  118. cache ? cache : response.data,
  119. refreshData,
  120. ];
  121. return value;
  122. }