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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. status: REQUEST_STATUS;
  55. code?: number;
  56. data: T | undefined;
  57. }>({
  58. loading: startLoading !== false && cache === undefined,
  59. status: REQUEST_STATUS.SUCCESS,
  60. code: undefined,
  61. data: undefined,
  62. });
  63. const [lastRefreshDate, setLastRefreshDate] = useState<Date | undefined>(
  64. undefined
  65. );
  66. const refreshData = (newRequest?: () => Promise<T>) => {
  67. let canRefresh;
  68. if (lastRefreshDate && minRefreshTime) {
  69. const last = lastRefreshDate;
  70. canRefresh = new Date().getTime() - last.getTime() > 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. setLastRefreshDate(new Date());
  82. const r = newRequest ? newRequest : request;
  83. r()
  84. .then((requestResponse: T) => {
  85. setResponse({
  86. loading: false,
  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. status: REQUEST_STATUS.CONNECTION_ERROR,
  99. code: 0,
  100. data: prevState.data,
  101. }));
  102. });
  103. }
  104. };
  105. const value: [
  106. boolean,
  107. REQUEST_STATUS,
  108. number | undefined,
  109. T | undefined,
  110. (newRequest?: () => Promise<T>) => void
  111. ] = [
  112. response.loading,
  113. response.status,
  114. response.code,
  115. cache ? cache : response.data,
  116. refreshData,
  117. ];
  118. return value;
  119. }