This commit is contained in:
Nicholas Orlowsky 2023-01-07 11:29:58 -06:00
commit ddb20ee17c
No known key found for this signature in database
GPG key ID: 3845F78A73B14100
23 changed files with 2498 additions and 170 deletions

View file

@ -1,4 +1,4 @@
import {Incident, UptimeResponse} from "./types";
import {Blog, Incident, Service, SessionKey, UptimeResponse} from "./types";
export async function getUptime(): Promise<UptimeResponse> {
let response: Response = await fetch('https://api-nws.nickorlow.com/uptime');
@ -17,3 +17,27 @@ export async function getIncidents(): Promise<Incident[]> {
}
}
export async function getBlogs(): Promise<Blog[]> {
let response: Response = await fetch('https://api-nws.nickorlow.com/blogs');
let blogs: Blog[] = await response.json();
return blogs;
}
export async function getSessionKey(accountId: string, password: string): Promise<SessionKey> {
let response: Response = await fetch('https://api-nws.nickorlow.com/Account/session',
{
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'id': accountId,
'password': password
})
});
let sessionKey: SessionKey = await response.json();
return sessionKey;
}

90
src/nws-api/hooks.ts Normal file
View file

@ -0,0 +1,90 @@
import {useEffect, useState} from "react";
import {Account, Service, SessionKey} from "./types";
export function useNonLoggedInRedirect() {
useEffect(()=>{
let rawSession: string | null = localStorage.getItem("session_key");
if(rawSession != null) {
let session: SessionKey = JSON.parse(rawSession);
if(session.expiry < new Date()) {
localStorage.removeItem("session_key");
} else {
window.location.href = "/dashboard";
}
}
}, []);
return true;
}
export function useLoggedInRedirect() {
useEffect(()=>{
let rawSession: string | null = localStorage.getItem("session_key");
if(rawSession != null) {
let session: SessionKey = JSON.parse(rawSession);
if(session.expiry > new Date()) {
window.location.href = "/login";
}
} else {
window.location.href = "/login";
}
}, []);
return true;
}
export function useGetAccountServices() {
const [services, setService] = useState<Service[]>([]);
useEffect(() => {
let rawSession: string | null = localStorage.getItem("session_key");
if(rawSession != null) {
let session: SessionKey = JSON.parse(rawSession);
fetch("https://api-nws.nickorlow.com/Account/services?accountId=" + session.accountId,
{
headers: {
"Authorization": btoa(session.accountId + ":" + session.id)
}
}).then((response)=>{
response.json().then((svcs: Service[]) => {
console.log(svcs)
setService(svcs);
});
});
}
}, []);
return services;
}
export function useNWSAccount() {
const [accountInfo, setAccountInfo] = useState<Account>();
useEffect(()=>{
let rawSession: string | null = localStorage.getItem("session_key");
if(rawSession != null) {
let session: SessionKey = JSON.parse(rawSession);
fetch("https://api-nws.nickorlow.com/Account/"+session.accountId, {
headers: {
"Authorization": btoa(session.accountId+":"+session.id)
}
}).then((e)=>{
if(e.status == 200) {
e.json().then((o: Account) => {
setAccountInfo(o)
})
} else {
localStorage.removeItem("session_key");
window.location.href = "/login";
}
});
} else {
localStorage.removeItem("session_key");
window.location.href = "/login";
}
}, []);
return accountInfo;
}

View file

@ -29,7 +29,39 @@ export type Incident = {
};
enum IncidentSeverity {
LOW,
MEDIUM,
HIGH
LOW,
MEDIUM,
HIGH
};
// Below is primarily for user-facing things
export type Account = {
id?: string,
email: string,
name?: string,
password?: string,
status?: string
};
export type Service = {
serviceId: string,
serviceName: string,
namespace: string,
containerUrl: string,
ownerId: string
}
export type ApiError = {
StatusCode: number,
ErrorMessage: string
};
export type SessionKey = {
id: string,
expiry: Date,
accountId: string,
ip: string
};