From ded4c84d51e6be233a2a00e772dca8267b853af1 Mon Sep 17 00:00:00 2001
From: Moe <moe@chocola.dev>
Date: Sat, 30 Apr 2022 23:47:48 +0700
Subject: [PATCH] Add Active Sessions Page

---
 lib/api/api.dart                |  9 ++++++
 lib/components/session_box.dart | 50 +++++++++++++++++++++++++++++++++
 lib/screens/sessions.dart       | 49 ++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 lib/components/session_box.dart
 create mode 100644 lib/screens/sessions.dart

diff --git a/lib/api/api.dart b/lib/api/api.dart
index 7ab7577..1c3091c 100644
--- a/lib/api/api.dart
+++ b/lib/api/api.dart
@@ -44,6 +44,15 @@ Future<dynamic> loginPost({email, password}) async {
   return req.statusCode;
 }
 
+Future<dynamic> getSessions() async {
+  String tempKey =
+      'rTugfHPB7Cd4I1OmsbFCHuJvBSjA2C48WOcMghviohNlNj8IZqazvtwJrdGFHDwp';
+  var req = await Dio()
+      .post(host + '/sessions', queryParameters: {'key': tempKey});
+  var res = req.data;
+  return res;
+}
+
 Future<dynamic> getTransactions() async {
   String tempKey =
       'rTugfHPB7Cd4I1OmsbFCHuJvBSjA2C48WOcMghviohNlNj8IZqazvtwJrdGFHDwp';
diff --git a/lib/components/session_box.dart b/lib/components/session_box.dart
new file mode 100644
index 0000000..fc2e8f7
--- /dev/null
+++ b/lib/components/session_box.dart
@@ -0,0 +1,50 @@
+import 'package:flutter/material.dart';
+
+class SessionBox extends StatelessWidget {
+  const SessionBox({Key? key, required this.icon, required this.ip, required this.userAgent}) : super(key: key);
+
+  final Icon icon;
+  final String ip;
+  final String userAgent;
+
+  @override
+  Widget build(BuildContext context) {
+    return Container(
+      margin: const EdgeInsets.all(8.0),
+      child: Card(
+        color: const Color(0xff212226),
+        shape: RoundedRectangleBorder(
+          borderRadius: BorderRadius.circular(10),
+        ),
+        child: InkWell(
+          onTap: (){},
+          borderRadius: BorderRadius.circular(10),
+          child: Row(
+            children: [
+              Flexible(
+                flex: 1,
+                child: Container(
+                  margin: const EdgeInsets.all(10.0),
+                  child: icon
+                ),
+              ),
+              Flexible(
+                flex: 3,
+                child: Container(
+                  margin: const EdgeInsets.all(10.0),
+                  child: Column(
+                    children: [
+                      Text(ip, style: const TextStyle(color: Colors.white, fontSize: 20.0, fontWeight: FontWeight.w600), textAlign: TextAlign.start,),
+                      const SizedBox(height: 10.0,),
+                      Text(userAgent, style: const TextStyle(color: Colors.white, fontSize: 15.0, fontWeight: FontWeight.w300),)
+                    ],
+                  ),
+                ),
+              )
+            ],
+          ),
+        ),
+      ),
+    );
+  }
+}
\ No newline at end of file
diff --git a/lib/screens/sessions.dart b/lib/screens/sessions.dart
new file mode 100644
index 0000000..e1e3715
--- /dev/null
+++ b/lib/screens/sessions.dart
@@ -0,0 +1,49 @@
+import 'package:flutter/material.dart';
+
+import 'package:nekoya_flutter/api/api.dart';
+import 'package:nekoya_flutter/components/session_box.dart';
+
+class Sessions extends StatefulWidget {
+  const Sessions({Key? key}) : super(key: key);
+
+  @override
+  State<Sessions> createState() => _SessionsState();
+}
+
+class _SessionsState extends State<Sessions> {
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      backgroundColor: const Color(0xff1b1c1e),
+      appBar: AppBar(
+        title: const Text('Active Sessions'),
+        centerTitle: true,
+        backgroundColor: const Color(0xff212226),
+      ),
+      body: FutureBuilder<dynamic>(
+        future: getSessions(),
+        builder: (context, snapshot) {
+          if (snapshot.hasData) {
+            var data = snapshot.data;
+            return ListView.builder(
+              itemCount: data!.length,
+              itemBuilder: (context, index) {
+                return SessionBox(
+                  icon: const Icon(Icons.key, color: Colors.white, size: 50,),
+                  ip: data[index]['ip'].split(', ')[0],
+                  userAgent: data[index]['user_agent'],
+                );
+              }
+            );
+          }
+
+          return const Center(
+            child: CircularProgressIndicator(
+              color: Color(0xff8B0000),
+            ),
+          );
+        }
+      )
+    );
+  }
+}
\ No newline at end of file