1
Fork 0
mirror of https://github.com/Steffo99/bluelib.git synced 2024-12-22 11:34: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,
"name": "bluelib",
"version": "0.12.3",
"version": "0.12.4",
"license": "AGPL-3.0-or-later",
"source": "src/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} !`);
setInstanceUrl(newInstanceUrl);
setLoginStatus(newLoginStatus);
@ -44,12 +44,12 @@ export default function(defaultInstanceUrl) {
route("/");
}
function requestLogout() {
function logout() {
console.debug("User requested logout, clearing loginStatus and localStorage...");
setLoginStatus(null);
window.localStorage.setItem("loginStatus", null);
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 [data, setData] = useState(undefined);
const [error, setError] = useState(undefined);
const [instanceTesterAbort, setInstanceTesterAbort] = useState(null);
if(body === undefined) {
body = {}
@ -22,11 +23,18 @@ export default function(method, path, body) {
function refresh() {
setData(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(() => {
royalnetApiRequest(instanceUrl, method, path, body).then(d => setData(d)).catch((e => setError(e)));
refresh();
}, [instanceUrl, method, path, body]);
return [data, error, refresh];