first
This commit is contained in:
16
src/hooks/useAuth.js
Normal file
16
src/hooks/useAuth.js
Normal file
@@ -0,0 +1,16 @@
|
||||
import { useContext } from 'react';
|
||||
|
||||
// auth provider
|
||||
import AuthContext from 'contexts/JWTContext';
|
||||
|
||||
// ==============================|| AUTH HOOKS ||============================== //
|
||||
|
||||
const useAuth = () => {
|
||||
const context = useContext(AuthContext);
|
||||
|
||||
if (!context) throw new Error('context must be use inside provider');
|
||||
|
||||
return context;
|
||||
};
|
||||
|
||||
export default useAuth;
|
||||
8
src/hooks/useConfig.js
Normal file
8
src/hooks/useConfig.js
Normal file
@@ -0,0 +1,8 @@
|
||||
import { useContext } from 'react';
|
||||
import { ConfigContext } from 'contexts/ConfigContext';
|
||||
|
||||
// ==============================|| CONFIG - HOOKS ||============================== //
|
||||
|
||||
const useConfig = () => useContext(ConfigContext);
|
||||
|
||||
export default useConfig;
|
||||
33
src/hooks/useLocalStorage.js
Normal file
33
src/hooks/useLocalStorage.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
export default function useLocalStorage(key, defaultValue) {
|
||||
const [value, setValue] = useState(() => {
|
||||
const storedValue = typeof window !== 'undefined' ? localStorage.getItem(key) : null;
|
||||
return storedValue === null ? defaultValue : JSON.parse(storedValue);
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
const listener = (e) => {
|
||||
if (typeof window !== 'undefined' && e.storageArea === localStorage && e.key === key) {
|
||||
setValue(e.newValue ? JSON.parse(e.newValue) : e.newValue);
|
||||
}
|
||||
};
|
||||
window.addEventListener('storage', listener);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('storage', listener);
|
||||
};
|
||||
}, [key, defaultValue]);
|
||||
|
||||
const setValueInLocalStorage = (newValue) => {
|
||||
setValue((currentValue) => {
|
||||
const result = typeof newValue === 'function' ? newValue(currentValue) : newValue;
|
||||
if (typeof window !== 'undefined') localStorage.setItem(key, JSON.stringify(result));
|
||||
return result;
|
||||
});
|
||||
};
|
||||
|
||||
return [value, setValueInLocalStorage];
|
||||
}
|
||||
29
src/hooks/usePagination.js
Normal file
29
src/hooks/usePagination.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
// ==============================|| CARD - PAGINATION ||============================== //
|
||||
|
||||
export default function usePagination(data, itemsPerPage) {
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const maxPage = Math.ceil(data.length / itemsPerPage);
|
||||
|
||||
function currentData() {
|
||||
const begin = (currentPage - 1) * itemsPerPage;
|
||||
const end = begin + itemsPerPage;
|
||||
return data.slice(begin, end);
|
||||
}
|
||||
|
||||
function next() {
|
||||
setCurrentPage((currentPage) => Math.min(currentPage + 1, maxPage));
|
||||
}
|
||||
|
||||
function prev() {
|
||||
setCurrentPage((currentPage) => Math.max(currentPage - 1, 1));
|
||||
}
|
||||
|
||||
function jump(page) {
|
||||
const pageNumber = Math.max(1, page);
|
||||
setCurrentPage(() => Math.min(pageNumber, maxPage));
|
||||
}
|
||||
|
||||
return { next, prev, jump, currentData, currentPage, maxPage };
|
||||
}
|
||||
18
src/hooks/useScriptRef.js
Normal file
18
src/hooks/useScriptRef.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
// ==============================|| ELEMENT REFERENCE HOOKS ||============================== //
|
||||
|
||||
const useScriptRef = () => {
|
||||
const scripted = useRef(true);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
scripted.current = false;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return scripted;
|
||||
};
|
||||
|
||||
export default useScriptRef;
|
||||
Reference in New Issue
Block a user