Some updates to the frontent

This commit is contained in:
Nikolai Rodionov 2024-05-02 19:09:44 +02:00
parent bbbe689313
commit 60f4934e60
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
8 changed files with 240 additions and 108 deletions

View File

@ -7,13 +7,15 @@ class EnvironmentLocalData {
required this.serverLocation, required this.serverLocation,
required this.provider, required this.provider,
required this.name, required this.name,
required this.description}); required this.description,
this.uuid});
final String name; final String name;
final String description; final String description;
final String provider; final String provider;
final String serverType; final String serverType;
final String serverLocation; final String serverLocation;
String? uuid;
} }
class EnvironmentsGrpc { class EnvironmentsGrpc {
@ -24,9 +26,9 @@ class EnvironmentsGrpc {
EnvironmentsGrpc(channel) : envStub = EnvironmentsClient(channel); EnvironmentsGrpc(channel) : envStub = EnvironmentsClient(channel);
// Get environments from the API // Get environments from the API
Future<EnvironmentLocalData> get(String name, SoftplayerCreds creds) async { Future<EnvironmentLocalData> get(String uuid, SoftplayerCreds creds) async {
final request = GetOptions( final request = GetOptions(
metadata: EnvironmentMetadata(name: name), id: EnvironmentId(uuid: uuid),
ownerId: OwnerId(uuid: creds.uuid), ownerId: OwnerId(uuid: creds.uuid),
token: Token(token: creds.token), token: Token(token: creds.token),
); );
@ -34,6 +36,7 @@ class EnvironmentsGrpc {
try { try {
final response = await envStub.get(request); final response = await envStub.get(request);
return EnvironmentLocalData( return EnvironmentLocalData(
uuid: uuid,
serverType: response.spec.serverType.toString(), serverType: response.spec.serverType.toString(),
serverLocation: response.spec.serverLocation.toString(), serverLocation: response.spec.serverLocation.toString(),
provider: response.spec.provider.toString(), provider: response.spec.provider.toString(),
@ -46,9 +49,7 @@ class EnvironmentsGrpc {
Future<EnvironmentLocalData> create( Future<EnvironmentLocalData> create(
EnvironmentLocalData data, SoftplayerCreds creds) async { EnvironmentLocalData data, SoftplayerCreds creds) async {
Location.values.forEach((element) => print(element.toString() + " - " + data.serverLocation)); print(data);
print(ServerType.values);
final request = CreateOptions( final request = CreateOptions(
metadata: metadata:
EnvironmentMetadata(description: data.description, name: data.name), EnvironmentMetadata(description: data.description, name: data.name),
@ -68,6 +69,7 @@ class EnvironmentsGrpc {
try { try {
final response = await envStub.create(request); final response = await envStub.create(request);
return EnvironmentLocalData( return EnvironmentLocalData(
uuid: response.id.uuid,
serverType: response.spec.serverType.toString(), serverType: response.spec.serverType.toString(),
serverLocation: response.spec.serverLocation.toString(), serverLocation: response.spec.serverLocation.toString(),
provider: response.spec.provider.toString(), provider: response.spec.provider.toString(),
@ -88,6 +90,7 @@ class EnvironmentsGrpc {
), ),
)) { )) {
envs.add(EnvironmentLocalData( envs.add(EnvironmentLocalData(
uuid: feature.id.uuid,
serverType: feature.spec.serverType.toString(), serverType: feature.spec.serverType.toString(),
serverLocation: feature.spec.serverLocation.toString(), serverLocation: feature.spec.serverLocation.toString(),
provider: feature.spec.provider.toString(), provider: feature.spec.provider.toString(),

View File

@ -2,24 +2,30 @@ import 'dart:html';
import 'dart:js' as js; import 'dart:js' as js;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:grpc/grpc_web.dart'; import 'package:grpc/grpc_web.dart';
import 'package:softplayer_dart_proto/main.dart';
import 'package:softplayer_web/api/grpc/creds.dart'; import 'package:softplayer_web/api/grpc/creds.dart';
import 'package:softplayer_web/api/grpc/environments.dart'; import 'package:softplayer_web/api/grpc/environments.dart';
import 'package:softplayer_web/helpers/providers/common.dart' as helper;
class CreateEnvForm extends StatefulWidget { class CreateEnvForm extends StatefulWidget {
CreateEnvForm(GrpcWebClientChannel channel, {super.key}) CreateEnvForm(GrpcWebClientChannel channel, {super.key})
: environmentsGrpc = EnvironmentsGrpc(channel); : environmentsGrpc = EnvironmentsGrpc(channel);
final EnvironmentsGrpc environmentsGrpc; final EnvironmentsGrpc environmentsGrpc;
@override @override
State<StatefulWidget> createState() => _CreateEnvFormState(); State<StatefulWidget> createState() => _CreateEnvFormState();
} }
class _CreateEnvFormState extends State<CreateEnvForm> { class _CreateEnvFormState extends State<CreateEnvForm> {
final _formKey = GlobalKey<FormState>(); final _formKey = GlobalKey<FormState>();
final String defaultProvider = Provider.PROVIDER_HETZNER.toString();
late helper.Provider provider =
helper.ProviderHelper().getProvider(defaultProvider);
late String defaultLocation = provider.defaultLocation();
final nameCtl = TextEditingController(); final nameCtl = TextEditingController();
final descriptionCtl = TextEditingController(); final descriptionCtl = TextEditingController();
String? serverLocation; late String? serverLocation = defaultLocation;
String? serverType; late String? serverType = ServerType.SERVER_TYPE_REGULAR.toString();
void createEnvironment() { void createEnvironment() {
// Validate returns true if the form is valid, or false otherwise. // Validate returns true if the form is valid, or false otherwise.
@ -31,14 +37,13 @@ class _CreateEnvFormState extends State<CreateEnvForm> {
EnvironmentLocalData( EnvironmentLocalData(
serverType: serverType!, serverType: serverType!,
serverLocation: serverLocation!, serverLocation: serverLocation!,
provider: "", provider: defaultProvider,
name: name, name: name,
description: description), description: description),
SoftplayerCredsHelpers().fromLocalStorage()) SoftplayerCredsHelpers().fromLocalStorage())
.then((rs) { .then((rs) {
Navigator.pop(context); Navigator.pop(context);
}).catchError((e) { }).catchError((e) {
print(e);
GrpcError error = e; GrpcError error = e;
String msg; String msg;
if (error.message != null) { if (error.message != null) {
@ -56,14 +61,14 @@ class _CreateEnvFormState extends State<CreateEnvForm> {
} }
} }
Widget createEnvForm() => SizedBox( String defaultServerType = "Regular";
Widget createEnvForm() => SingleChildScrollView(
child: SizedBox(
width: 420, width: 420,
child: Form( child: Form(
key: _formKey, key: _formKey,
child: Center( child: Center(
child: Column(children: [ child: Column(children: [
Divider(),
Text("Provider the environment metadata"),
TextFormField( TextFormField(
autofocus: true, autofocus: true,
controller: nameCtl, controller: nameCtl,
@ -77,7 +82,7 @@ class _CreateEnvFormState extends State<CreateEnvForm> {
cursorRadius: const Radius.circular(10), cursorRadius: const Radius.circular(10),
), ),
TextFormField( TextFormField(
autofocus: true, autofocus: false,
controller: descriptionCtl, controller: descriptionCtl,
decoration: const InputDecoration( decoration: const InputDecoration(
hintText: "Enter the environment description", hintText: "Enter the environment description",
@ -89,32 +94,58 @@ class _CreateEnvFormState extends State<CreateEnvForm> {
cursorHeight: 18, cursorHeight: 18,
cursorRadius: const Radius.circular(10), cursorRadius: const Radius.circular(10),
), ),
Divider(),
DropdownButtonFormField( DropdownButtonFormField(
decoration: const InputDecoration( decoration: const InputDecoration(
hintText: "Enter the environment description", hintText: "Enter the environment description",
icon: Icon(Icons.computer), icon: Icon(Icons.computer),
label: Text("Type of the server"), label: Text("Type of the server"),
), ),
value: null, value: ServerType.SERVER_TYPE_REGULAR.toString(),
isDense: true, isDense: true,
items: [ items: ServerType.values
DropdownMenuItem( .where((element) =>
child: Text("lala1"), element != ServerType.SERVER_TYPE_CUSTOM &&
value: "test1", element != ServerType.SERVER_TYPE_UNSPECIFIED)
), .map((serverType) {
DropdownMenuItem(child: Text("lala2"), value: "test2"), return DropdownMenuItem(
DropdownMenuItem(child: Text("lala3"), value: "test3"), value: serverType.toString(),
DropdownMenuItem(child: Text("lala4"), value: "test4"), child: Text(helper.ProviderHelper()
], .getServerType(serverType.toString())),
onChanged: (value) => print(value), );
}).toList(),
onChanged: (value) => setState(() {
serverType = value;
}),
), ),
TextButton( TextButton(
onPressed: () => js.context.callMethod( onPressed: () => js.context.callMethod(
'open', ['https://stackoverflow.com/questions/ask']), 'open', ['https://stackoverflow.com/questions/ask']),
child: Text("Read more about environment types here"), child: Text("Read more about environment types here"),
) ),
])))); DropdownButtonFormField(
decoration: const InputDecoration(
hintText: "Enter the environment location",
icon: Icon(Icons.location_on),
label: Text("Location of the server"),
),
value: defaultLocation,
isDense: true,
items: Location.values
.where((element) => element
.toString()
.contains(provider.getProviderName().toUpperCase()))
.map((serverType) {
return DropdownMenuItem(
value: serverType.toString(),
child: Text(
provider.getServerLocation(serverType.toString())),
);
}).toList(),
onChanged: (value) => setState(() {
serverLocation = value;
}),
),
])))));
List<Widget> createEnvActions() => [ List<Widget> createEnvActions() => [
TextButton( TextButton(
onPressed: createEnvironment, onPressed: createEnvironment,

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:softplayer_web/api/grpc/environments.dart'; import 'package:softplayer_web/api/grpc/environments.dart';
import 'package:softplayer_web/helpers/providers/common.dart';
class EnvirnomentCard extends StatelessWidget { class EnvirnomentCard extends StatelessWidget {
final EnvironmentLocalData env; final EnvironmentLocalData env;
@ -11,6 +12,17 @@ class EnvirnomentCard extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Provider provider = ProviderHelper().getProvider(env.provider);
String serverType;
String serverLocation;
try {
serverType = ProviderHelper().getServerType(env.serverType);
serverLocation = provider.getServerLocation(env.serverLocation);
} catch (e) {
rethrow;
}
return Card( return Card(
child: SelectionArea( child: SelectionArea(
child: Column( child: Column(
@ -25,21 +37,21 @@ class EnvirnomentCard extends StatelessWidget {
top: BorderSide.none, top: BorderSide.none,
), ),
children: [ children: [
TableRow(children: [
Text("Provider"),
Text(env.provider),
]),
TableRow(children: [ TableRow(children: [
Text("Description"), Text("Description"),
Text(env.description), Text(env.description),
]), ]),
TableRow(children: [
Text("Provider"),
Text(provider.getProviderName()),
]),
TableRow(children: [ TableRow(children: [
Text("Server Type"), Text("Server Type"),
Text(env.serverType), Text(serverType),
]), ]),
TableRow(children: [ TableRow(children: [
Text("Location"), Text("Location"),
Text(env.serverLocation), Text(serverLocation),
]), ]),
], ],
) )

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:grpc/grpc_web.dart'; import 'package:grpc/grpc_web.dart';
import 'package:softplayer_web/api/grpc/creds.dart'; import 'package:softplayer_web/api/grpc/creds.dart';
import 'package:softplayer_web/api/grpc/environments.dart'; import 'package:softplayer_web/api/grpc/environments.dart';
@ -48,16 +49,25 @@ class _EnvirnomentListState extends State<EnvirnomentList> {
if (snapshot.hasData) { if (snapshot.hasData) {
var data = snapshot.requireData; var data = snapshot.requireData;
if (data.isNotEmpty) { if (data.isNotEmpty) {
return GridView.count( return Column(children: [
Flexible(
child: TextField(
decoration: InputDecoration(
icon: Icon(Icons.search),
),
),
),
Flexible(
child: GridView.count(
crossAxisCount: 4, crossAxisCount: 4,
children: snapshot.data! children: snapshot.data!
.map((e) => EnvirnomentCard( .map((e) => EnvirnomentCard(
env: e, env: e,
)) ))
.toList(), .toList(),
); ))
]);
} else { } else {
print("npo data");
return Center( return Center(
child: Container( child: Container(
height: 300, height: 300,

View File

@ -1,4 +1,52 @@
import 'package:softplayer_dart_proto/environments/environments_v1.pbenum.dart';
import 'package:softplayer_web/helpers/providers/hetzner.dart';
abstract class Provider { abstract class Provider {
String getServerType(String serverType); String getServerLocation(String serverLocation);
String rawServerType(String serverType); String rawServerLocation(String serverLocation);
String defaultLocation();
String getProviderName();
String rawProviderName();
}
class ProviderHelper {
String getServerType(String serverType) {
if (serverType == ServerType.SERVER_TYPE_STARTER.toString()) {
return "Starter";
} else if (serverType == ServerType.SERVER_TYPE_REGULAR.toString()) {
return "Regular";
} else if (serverType == ServerType.SERVER_TYPE_PLUS.toString()) {
return "Plus";
} else if (serverType == ServerType.SERVER_TYPE_PRO.toString()) {
return "Pro";
} else if (serverType == ServerType.SERVER_TYPE_CUSTOM.toString()) {
throw 'Custom server type is not yet supported';
} else {
throw 'Unknown server type';
}
}
String rawServerType(String serverType) {
if (serverType == "Starter") {
return ServerType.SERVER_TYPE_STARTER.toString();
} else if (serverType == "Regular") {
return ServerType.SERVER_TYPE_REGULAR.toString();
} else if (serverType == "Plus") {
return ServerType.SERVER_TYPE_PLUS.toString();
} else if (serverType == "Pro") {
return ServerType.SERVER_TYPE_PRO.toString();
} else {
throw 'Unknown server type';
}
}
Hetzner getProvider(String provider) {
switch (provider) {
case "hetzner" || "PROVIDER_HETZNER":
return Hetzner();
default:
throw "unknown prodiver";
}
}
} }

View File

@ -1,26 +1,54 @@
import 'package:softplayer_web/helpers/providers/common.dart'; import 'package:softplayer_dart_proto/environments/environments_v1.pbgrpc.dart';
import 'package:softplayer_web/helpers/providers/common.dart' as helper;
class Hetzner implements helper.Provider {
class Hetzner implements Provider {
@override @override
String getServerType(String serverType) { String getProviderName() {
switch (serverType) { return "Hetzner";
case "SERVER_TYPE_STARTER":
return "starter";
case "SERVER_TYPE_S":
return "starter";
case "SERVER_TYPE_STARTER":
return "starter";
case "SERVER_TYPE_STARTER":
return "starter";
default:
}
throw UnimplementedError();
} }
@override @override
String rawServerType(string) { String rawProviderName() {
// TODO: implement rawServerType return Provider.PROVIDER_HETZNER.toString();
throw UnimplementedError(); }
@override
String getServerLocation(String serverType) {
if (serverType == Location.LOCATION_HETZNER_ASHBURN.toString()) {
return "Ashburn, USA";
} else if (serverType == Location.LOCATION_HETZNER_FALKENSTEIN.toString()) {
return "Falkenstein, Germany";
} else if (serverType == Location.LOCATION_HETZNER_HELSINKI.toString()) {
return "Helsinki, Finland";
} else if (serverType == Location.LOCATION_HETZNER_HILLSBORO.toString()) {
return "Hillsboro, USA";
} else if (serverType == Location.LOCATION_HETZNER_NUREMBERG.toString()) {
return "Nuremberg, Germany";
} else {
throw 'Unknown server type';
}
}
@override
String rawServerLocation(String serverLocation) {
if (serverLocation == "Ashburn, USA") {
return Location.LOCATION_HETZNER_ASHBURN.toString();
} else if (serverLocation == "Falkenstein, Germany") {
return Location.LOCATION_HETZNER_FALKENSTEIN.toString();
} else if (serverLocation == "Helsinki, Finland") {
return Location.LOCATION_HETZNER_HELSINKI.toString();
} else if (serverLocation == "Hillsboro, USA") {
return Location.LOCATION_HETZNER_HILLSBORO.toString();
} else if (serverLocation == "Nuremberg, Germany") {
return Location.LOCATION_HETZNER_NUREMBERG.toString();
} else {
throw 'Unknown server type';
}
}
@override
String defaultLocation() {
return Location.LOCATION_HETZNER_NUREMBERG.toString();
} }
} }

View File

@ -245,7 +245,7 @@ packages:
description: description:
path: "." path: "."
ref: main ref: main
resolved-ref: bccaaff90c8ef1e9d23c565a58cab6db841282d3 resolved-ref: "3bf6e1bef80350848f0f0407685e4eaf868d4c7e"
url: "https://git.badhouseplants.net/softplayer/softplayer-dart-proto.git" url: "https://git.badhouseplants.net/softplayer/softplayer-dart-proto.git"
source: git source: git
version: "1.0.0+1" version: "1.0.0+1"