-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #149 from QCDIS/148-list-started-instances-on-vlab…
…-page 148 list started instances on vlab page
- Loading branch information
Showing
10 changed files
with
262 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import React, {useEffect, useState} from "react"; | ||
import getConfig from "next/config"; | ||
import {JWT} from "next-auth/jwt"; | ||
import {VLab} from "../types/vlab"; | ||
import {useSession} from "next-auth/react"; | ||
|
||
type Props = { | ||
vlab: VLab, | ||
slug: string | string[] | undefined, | ||
isAuthenticated: boolean, | ||
token: JWT, | ||
} | ||
|
||
interface VLabInstance { | ||
vlab: string, | ||
username: string, | ||
} | ||
|
||
const VLabInstances: React.FC<Props> = ({vlab, slug, isAuthenticated, token}) => { | ||
|
||
const {publicRuntimeConfig} = getConfig() | ||
|
||
const session = useSession() | ||
|
||
const [vlabInstances, setVlabInstances] = useState<Array<VLabInstance>>([]) | ||
const [backendError, setBackendError] = useState(false) | ||
const [hideInstance, setHideInstance] = useState(false) | ||
|
||
const registerInstance = async () => { | ||
if ( | ||
hideInstance | ||
|| (session.status != "authenticated") | ||
) { | ||
return | ||
} | ||
|
||
const username = session.data.user.name | ||
|
||
const requestOptions: RequestInit = { | ||
method: "POST", | ||
headers: { | ||
"Authorization": "Bearer: " + token.accessToken, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
"vlab": slug, | ||
"username": username, | ||
}), | ||
} | ||
|
||
const apiUrl = `${window.location.origin}/${publicRuntimeConfig.apiBasePath}` | ||
return fetch(`${apiUrl}/vlab_instances/`, requestOptions); | ||
} | ||
|
||
const fetchVlabInstances = async () => { | ||
|
||
var requestOptions: RequestInit = { | ||
method: "GET", | ||
headers: { | ||
"Authorization": "Bearer: " + token.accessToken | ||
}, | ||
}; | ||
|
||
const apiUrl = `${window.location.origin}/${publicRuntimeConfig.apiBasePath}` | ||
const res = await fetch(`${apiUrl}/vlab_instances/?vlab_slug=${slug}`, requestOptions); | ||
try { | ||
const dat = await res.json() | ||
setVlabInstances(dat) | ||
} catch (e) { | ||
console.log(e) | ||
setBackendError(true) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
if (isAuthenticated) { | ||
Promise.all([fetchVlabInstances()]) | ||
} | ||
}, [isAuthenticated]); | ||
|
||
return ( | ||
<div className="space-y-4"> | ||
<p className="text-2xl font-sans">Instances</p> | ||
{backendError || ( | ||
<> | ||
<p> | ||
{vlabInstances.length} instance{vlabInstances.length != 1 && "s"} | ||
</p> | ||
<ul | ||
className="flex flex-wrap" | ||
> | ||
{vlabInstances.map((vlab_instance) => { | ||
return ( | ||
<li | ||
key={vlab_instance.username} | ||
className="rounded-full m-1 px-2 bg-quinary text-onTertiary" | ||
> | ||
{vlab_instance.username} | ||
</li> | ||
) | ||
})} | ||
</ul> | ||
<div className="space-y-4 pt-4"> | ||
<div className="space-x-2"> | ||
<input | ||
type="checkbox" | ||
name="hideInstance" | ||
checked={hideInstance} | ||
onChange={ | ||
(e) => { | ||
setHideInstance(e.target.checked) | ||
} | ||
} | ||
/> | ||
<label className="text-sm"> | ||
Hide my instance from this list | ||
</label> | ||
</div> | ||
<div> | ||
<a | ||
target="blank" | ||
href={vlab.endpoint} | ||
onClick={registerInstance} | ||
> | ||
<button | ||
className="bg-primary hover:bg-primaryDark text-onPrimary font-bold py-2 px-4 rounded" | ||
> | ||
Launch my instance | ||
</button> | ||
</a> | ||
</div> | ||
</div> | ||
|
||
</> | ||
)} | ||
</div> | ||
) | ||
} | ||
|
||
export default VLabInstances |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
export interface VLab { | ||
title: string, | ||
slug: string, | ||
description: string, | ||
endpoint: string, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from django.contrib import admin | ||
from virtual_labs.models import VirtualLab | ||
from virtual_labs.models import VirtualLab, VirtualLabInstance | ||
|
||
admin.site.register(VirtualLab) | ||
admin.site.register(VirtualLabInstance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.