Merge remote-tracking branch 'origin/main' into dev/chat
commit
acbd36a8b4
@ -0,0 +1,16 @@
|
|||||||
|
import { useState } from 'react'
|
||||||
|
|
||||||
|
export function useFormInput(initialValue) {
|
||||||
|
const [value, setValue] = useState(initialValue);
|
||||||
|
|
||||||
|
function handleChange(e) {
|
||||||
|
setValue(e.target.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inputProps = {
|
||||||
|
value: value,
|
||||||
|
onChange: handleChange
|
||||||
|
};
|
||||||
|
|
||||||
|
return inputProps;
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
import { useEffect, useState } from 'react'
|
||||||
|
|
||||||
|
function checkStatus(response) {
|
||||||
|
if (response.status >= 200 && response.status < 300) {
|
||||||
|
return response
|
||||||
|
} else {
|
||||||
|
const message =
|
||||||
|
'Fetch error: ' + response.url + ' ' + response.status + ' (' +
|
||||||
|
response.statusText + ')'
|
||||||
|
const error = new Error(message)
|
||||||
|
error.response = response
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useJson(url) {
|
||||||
|
const [data, setData] = useState(null)
|
||||||
|
useEffect(() => {
|
||||||
|
if (url) {
|
||||||
|
let ignore = false
|
||||||
|
fetch(url)
|
||||||
|
.then(checkStatus)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(json => {
|
||||||
|
if (!ignore) {
|
||||||
|
setData(json)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return () => {
|
||||||
|
ignore = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [url])
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
import { createContext, useContext } from 'react'
|
import { createContext, useContext } from 'react'
|
||||||
|
|
||||||
export const AuthContext = createContext(null)
|
export const AuthContext = createContext({})
|
||||||
|
|
||||||
export function useAuth() {
|
export function useAuthContext() {
|
||||||
return useContext(AuthContext)
|
return useContext(AuthContext)
|
||||||
}
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
import { createContext, useContext } from 'react'
|
||||||
|
|
||||||
|
export const ThemeContext = createContext({})
|
||||||
|
|
||||||
|
export function useThemeContext() {
|
||||||
|
return useContext(ThemeContext)
|
||||||
|
}
|
Loading…
Reference in New Issue