Added component unmount cleanup
This commit is contained in:
parent
960ad7ce0d
commit
8fc85e415f
@ -1,15 +1,30 @@
|
|||||||
import { useState } from "react"
|
import { useEffect, useRef, useState } from "react"
|
||||||
import { API_URL } from "../../config"
|
import { API_URL } from "../../config"
|
||||||
|
|
||||||
const useAddAnnouncement = () => {
|
const useAddAnnouncement = () => {
|
||||||
const [status, setStatus] = useState("Опубликовать")
|
const [status, setStatus] = useState("Опубликовать")
|
||||||
|
|
||||||
|
const timerIdRef = useRef()
|
||||||
|
const abortControllerRef = useRef()
|
||||||
|
|
||||||
const doAdd = async (formData) => {
|
const doAdd = async (formData) => {
|
||||||
setStatus(true)
|
if (status === "Загрузка") {
|
||||||
|
abortControllerRef.current?.abort()
|
||||||
|
setStatus("Отменено")
|
||||||
|
timerIdRef.current = setTimeout(() => setStatus("Опубликовать"), 3000)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
setStatus("Загрузка")
|
||||||
|
|
||||||
|
const abortController = new AbortController()
|
||||||
|
abortControllerRef.current = abortController
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(API_URL + "/announcement", {
|
const res = await fetch(API_URL + "/announcement", {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: formData,
|
body: formData,
|
||||||
|
signal: abortController.signal
|
||||||
})
|
})
|
||||||
|
|
||||||
const data = await res.json()
|
const data = await res.json()
|
||||||
@ -21,9 +36,17 @@ const useAddAnnouncement = () => {
|
|||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
setStatus(err.message ?? err)
|
setStatus(err.message ?? err)
|
||||||
setTimeout(() => setStatus("Опубликовать"), 10000)
|
timerIdRef.current = setTimeout(() => setStatus("Опубликовать"), 10000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const abortController = abortControllerRef.current
|
||||||
|
return () => {
|
||||||
|
clearTimeout(timerIdRef.current)
|
||||||
|
abortController?.abort()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return {doAdd, status}
|
return {doAdd, status}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ function useBook(id) {
|
|||||||
const [status, setStatus] = useState('')
|
const [status, setStatus] = useState('')
|
||||||
|
|
||||||
const handleBook = () => {
|
const handleBook = () => {
|
||||||
const token = getToken() || "Test"
|
const token = getToken()
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
setStatus("Загрузка")
|
setStatus("Загрузка")
|
||||||
|
@ -1,12 +1,22 @@
|
|||||||
import { useEffect, useState } from "react"
|
import { useEffect, useRef, useState } from "react"
|
||||||
|
import { isAborted } from '../../utils'
|
||||||
|
|
||||||
const useFetch = (url, params, initialData) => {
|
const useFetch = (url, params, initialData) => {
|
||||||
const [data, setData] = useState(initialData)
|
const [data, setData] = useState(initialData)
|
||||||
const [loading, setLoading] = useState(true)
|
const [loading, setLoading] = useState(true)
|
||||||
const [error, setError] = useState("")
|
const [error, setError] = useState("")
|
||||||
|
|
||||||
|
const abortControllerRef = useRef(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetch(url, params)
|
if (abortControllerRef.current) {
|
||||||
|
abortControllerRef.current.abort()
|
||||||
|
}
|
||||||
|
|
||||||
|
const abortController = new AbortController()
|
||||||
|
abortControllerRef.current = abortController
|
||||||
|
|
||||||
|
fetch(url, { ...params, signal: abortControllerRef.current.signal })
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
switch (res.status) {
|
switch (res.status) {
|
||||||
@ -27,16 +37,21 @@ const useFetch = (url, params, initialData) => {
|
|||||||
setLoading(false)
|
setLoading(false)
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
setError("Ошибка сети")
|
if (!isAborted(err)) {
|
||||||
|
setError("Ошибка сети")
|
||||||
|
}
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
|
||||||
if (import.meta.env.DEV) {
|
if (import.meta.env.DEV) {
|
||||||
console.log(url, params, err)
|
console.log(url, params, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return () => abortControllerRef.current.abort()
|
||||||
}, [url, params])
|
}, [url, params])
|
||||||
|
|
||||||
return { data, loading, error }
|
return { data, loading, error, abort: abortControllerRef.current?.abort }
|
||||||
}
|
}
|
||||||
|
|
||||||
export default useFetch
|
export default useFetch
|
||||||
|
@ -5,6 +5,8 @@ const removeNull = (obj) => Object.fromEntries(
|
|||||||
key,
|
key,
|
||||||
value === Object(value) ? removeNull(value) : value,
|
value === Object(value) ? removeNull(value) : value,
|
||||||
]),
|
]),
|
||||||
);
|
)
|
||||||
|
|
||||||
export { removeNull }
|
const isAborted = (err) => err.name == 'AbortError'
|
||||||
|
|
||||||
|
export { removeNull, isAborted }
|
Loading…
x
Reference in New Issue
Block a user