1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-22 19:44:21 +00:00
This commit is contained in:
Steffo 2020-06-23 23:14:57 +02:00
parent 6c5023e2c8
commit e5b919af6a
Signed by: steffo
GPG key ID: 896A80F55F7C97F0
3 changed files with 14 additions and 6 deletions

View file

@ -1,7 +1,7 @@
{ {
"private": false, "private": false,
"name": "bluelib", "name": "bluelib",
"version": "0.12.3", "version": "0.12.4",
"license": "AGPL-3.0-or-later", "license": "AGPL-3.0-or-later",
"source": "src/index.js", "source": "src/index.js",
"main": "dist/index.js", "main": "dist/index.js",

View file

@ -34,7 +34,7 @@ export default function(defaultInstanceUrl) {
}) })
}, []); }, []);
function onSuccessfulLogin(newInstanceUrl, newLoginStatus) { function storeValues(newInstanceUrl, newLoginStatus) {
console.debug(`Successfully logged in as ${newLoginStatus.user.username} @ ${newInstanceUrl} !`); console.debug(`Successfully logged in as ${newLoginStatus.user.username} @ ${newInstanceUrl} !`);
setInstanceUrl(newInstanceUrl); setInstanceUrl(newInstanceUrl);
setLoginStatus(newLoginStatus); setLoginStatus(newLoginStatus);
@ -44,12 +44,12 @@ export default function(defaultInstanceUrl) {
route("/"); route("/");
} }
function requestLogout() { function logout() {
console.debug("User requested logout, clearing loginStatus and localStorage..."); console.debug("User requested logout, clearing loginStatus and localStorage...");
setLoginStatus(null); setLoginStatus(null);
window.localStorage.setItem("loginStatus", null); window.localStorage.setItem("loginStatus", null);
route("/"); route("/");
} }
return [instanceUrl, loginStatus, onSuccessfulLogin, requestLogout] return [instanceUrl, loginStatus, storeValues, logout]
} }

View file

@ -10,6 +10,7 @@ export default function(method, path, body) {
const loginStatus = useContext(LoginStatus); const loginStatus = useContext(LoginStatus);
const [data, setData] = useState(undefined); const [data, setData] = useState(undefined);
const [error, setError] = useState(undefined); const [error, setError] = useState(undefined);
const [instanceTesterAbort, setInstanceTesterAbort] = useState(null);
if(body === undefined) { if(body === undefined) {
body = {} body = {}
@ -22,11 +23,18 @@ export default function(method, path, body) {
function refresh() { function refresh() {
setData(undefined); setData(undefined);
setError(undefined); setError(undefined);
royalnetApiRequest(instanceUrl, method, path, body).then(d => setData(d)).catch((e => setError(e)));
if(instanceTesterAbort !== null) {
instanceTesterAbort.abort();
}
let abort = new AbortController();
setInstanceTesterAbort(abort);
royalnetApiRequest(instanceUrl, method, path, body, abort).then(d => setData(d)).catch((e => setError(e)));
} }
useDeepCompareEffect(() => { useDeepCompareEffect(() => {
royalnetApiRequest(instanceUrl, method, path, body).then(d => setData(d)).catch((e => setError(e))); refresh();
}, [instanceUrl, method, path, body]); }, [instanceUrl, method, path, body]);
return [data, error, refresh]; return [data, error, refresh];