Skip to content

Commit

Permalink
Add inital fetch layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Yasamato committed Apr 15, 2021
1 parent 1a7e4b7 commit f6b11b1
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 69 deletions.
6 changes: 3 additions & 3 deletions api/models.py
Expand Up @@ -41,14 +41,14 @@ class Table(db.Model):
uselist=True
)

def to_dict(self):
def to_dict(self, recursive=False):
return {
"id": self.id,
"tab_id": self.tab_id,
"name": self.name,
"description": self.description,
"columns": [t.column_id for t in self.columns],
"data": [t.id for t in self.data]
"columns": [t.to_dict() if recursive else t.column_id for t in self.columns],
"data": [t.to_dict() if recursive else t.id for t in self.data]
}


Expand Down
2 changes: 1 addition & 1 deletion api/queries.py
Expand Up @@ -39,7 +39,7 @@ def resolve_table(table_id):
table_id,
f"table {table_id} not found"
)
return jsonify(table.to_dict())
return jsonify(table.to_dict(True))
except Exception as e:
logging.error(str(e))
return 500
Expand Down
2 changes: 1 addition & 1 deletion api/start.sh
Expand Up @@ -17,7 +17,7 @@ mkdir -p /config

# migrate if db does not exists
if [ ! -f /config/data.db ]; then
echo "Could not find existing db, trying to run migration script"
echo "Could not find existing db, trying to run init script"
python init.py
fi

Expand Down
124 changes: 111 additions & 13 deletions lib/api.dart
Expand Up @@ -5,14 +5,6 @@ import 'package:http/http.dart' as http;
// for debugging only, on prod everything will be relative, meaning domain = ""
var domain = "http://localhost:8080";

Future<bool> health() async {
return http.get(Uri.parse(domain + '/api/health')).then((resp) {
return resp.statusCode == 200;
}, onError: (e) {
return false;
});
}

class Tab {
final int id;
String name;
Expand All @@ -23,13 +15,15 @@ class Tab {
required this.id,
this.name = "",
this.description = "",
this.tables,
});

factory Tab.fromJson(Map<String, dynamic> json) {
return Tab(
id: json["id"],
name: json["name"],
description: json["description"],
tables: List<int>.from(json["tables"]),
);
}

Expand All @@ -39,7 +33,7 @@ class Tab {
if (resp.statusCode == 200) {
return Tab.fromJson(jsonDecode(resp.body));
}
throw Exception('Failed to load Tab');
throw Exception('Failed to load Tab $id');
},
onError: (e) {
print('Request failed: $e');
Expand All @@ -55,10 +49,10 @@ class Table {
int? tabId;

// data should be fetched via /api/tables/<id>/data
List<int>? data;
List<dynamic>? data;

// columns should be fetched via /api/tables/<id>/columns
List<int>? columns;
List<dynamic>? columns;

Table({
required this.id,
Expand Down Expand Up @@ -86,7 +80,7 @@ class Table {
if (resp.statusCode == 200) {
return Table.fromJson(jsonDecode(resp.body));
}
throw Exception('Failed to load Table');
throw Exception('Failed to load Table $id');
},
onError: (e) {
print('Request failed: $e');
Expand Down Expand Up @@ -123,11 +117,115 @@ class Column {
if (resp.statusCode == 200) {
return Column.fromJson(jsonDecode(resp.body));
}
throw Exception('Failed to load Column');
throw Exception('Failed to load Column $id');
},
onError: (e) {
print('Request failed: $e');
},
);
}
}

Future<List<Column>> fetchAllColumns() async {
print("Fetching columns from remote");
return http.get(Uri.parse(domain + '/api/columns')).then(
(resp) {
if (resp.statusCode == 200) {
return List<Column>.from(
jsonDecode(resp.body).map((c) => Column.fromJson(c)));
}
throw Exception('Failed to load Columns');
},
onError: (e) {
print('Request failed: $e');
},
);
}

Future<List<dynamic>> fetchColumns(int id) async {
print("Fetching columns of table $id from remote");
return http
.get(Uri.parse(domain + '/api/tables/' + id.toString() + '/columns'))
.then(
(resp) {
if (resp.statusCode == 200) {
return jsonDecode(resp.body);
}
throw Exception('Failed to load columns of table $id');
},
onError: (e) {
print('Request failed: $e');
},
);
}

Future<List<Tab>> fetchTabs() async {
print("Fetching tabs from remote");
return http.get(Uri.parse(domain + '/api/tabs')).then(
(resp) {
if (resp.statusCode == 200) {
return List<Tab>.from(
jsonDecode(resp.body).map((c) => Tab.fromJson(c)));
}
throw Exception('Failed to load Tabs');
},
onError: (e) {
throw Exception('Request failed: $e');
},
);
}

Future<List<Table>> fetchTables() async {
print("Fetching tables from remote");
return http.get(Uri.parse(domain + '/api/tables')).then(
(resp) {
if (resp.statusCode == 200) {
return List<Table>.from(
jsonDecode(resp.body).map((c) => Table.fromJson(c)));
}
throw Exception('Failed to load Tables');
},
onError: (e) {
print('Request failed: $e');
},
);
}

Future<List<dynamic>> fetchData(int id) async {
print("Fetching data of table $id from remote");
return http
.get(Uri.parse(domain + '/api/tables/' + id.toString() + '/data'))
.then(
(resp) {
if (resp.statusCode == 200) {
return jsonDecode(resp.body);
}
throw Exception('Failed to load data of table $id');
},
onError: (e) {
print('Request failed: $e');
},
);
}

Future<bool> health() async {
return http.get(Uri.parse(domain + '/api/health')).then((resp) {
return resp.statusCode == 200;
}, onError: (e) {
return false;
});
}

Future<bool> isLoggedIn() async {
return http.get(Uri.parse(domain + '/user/is-login')).then(
(resp) {
if (resp.statusCode == 200) {
return jsonDecode(resp.body)["edit"];
}
throw Exception('Failed to check login');
},
onError: (e) {
print('Request failed: $e');
},
);
}
9 changes: 9 additions & 0 deletions lib/layout/adaptive.dart
@@ -0,0 +1,9 @@
import 'package:flutter/material.dart';
import 'package:adaptive_breakpoints/adaptive_breakpoints.dart';

bool isDesktop(BuildContext context) =>
getWindowType(context) >= AdaptiveWindowType.medium;

bool isTablet(BuildContext context) {
return getWindowType(context) == AdaptiveWindowType.medium;
}
45 changes: 45 additions & 0 deletions lib/layout/navbar.dart
@@ -0,0 +1,45 @@
import 'package:flutter/material.dart';

class SideDrawer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Drawer(
child: Column(
children: <Widget>[
DrawerHeader(
child: Center(
child: Text(
'Side menu FlutterCorner',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
decoration: BoxDecoration(
color: Colors.black,
),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () => {},
),
ListTile(
leading: Icon(Icons.shopping_cart),
title: Text('Cart'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
leading: Icon(Icons.border_color),
title: Text('Feedback'),
onTap: () => {Navigator.of(context).pop()},
),
ListTile(
leading: Icon(Icons.exit_to_app),
title: Text('Logout'),
onTap: () => {Navigator.of(context).pop()},
),
],
),
);
}
}
59 changes: 59 additions & 0 deletions lib/layout/tab.dart
@@ -0,0 +1,59 @@
import 'package:flutter/material.dart';
import 'package:index/api.dart' as api;
import 'package:index/layout/table.dart';

class TabPage extends StatefulWidget {
final int id;

const TabPage({Key? key, required this.id}) : super(key: key);

@override
_TabPageState createState() => _TabPageState();
}

class _TabPageState extends State<TabPage> {
api.Tab? _tab;

@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(10),
child: FutureBuilder<api.Tab>(
future: api.Tab.fetch(widget.id),
builder: (BuildContext context, AsyncSnapshot<api.Tab> snapshot) {
if (snapshot.hasData) {
_tab = snapshot.data!;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Card(
child: Container(
padding: const EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text("Tab-id: ${_tab!.id}"),
Text("Tab-name: ${_tab!.name}"),
Text("Tab-description: ${_tab!.description}"),
],
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _tab!.tables!
.map((table) => TablePage(id: table))
.toList(),
),
],
);
} else if (snapshot.hasError) {
return Center(
child: Text("Tab: ${widget.id} errored: ${snapshot.error}"));
} else {
return Center(child: Text("Tab: ${widget.id} is loading..."));
}
},
),
);
}
}

0 comments on commit f6b11b1

Please sign in to comment.