Skip to content

Commit

Permalink
fix: deconnexion devices
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinFlm committed Nov 25, 2024
1 parent eb0ef11 commit d2086ef
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 84 deletions.
161 changes: 89 additions & 72 deletions patient_mobile/lib/screens/2fa/devices_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ class DevicesPage extends StatefulWidget {

class _DevicesPageState extends State<DevicesPage> {
List<dynamic> devices = [];
@override
void initState() {
super.initState();
getDevices();
}

Future<void> getDevices() async {
Future<bool> getDevices() async {
List<dynamic> temp = await getAllDevices(context);
setState(() {
devices = temp;
});
devices = temp;
return true;
}

String devicesFormatTime(int time) {
Expand Down Expand Up @@ -105,67 +99,90 @@ class _DevicesPageState extends State<DevicesPage> {
width: 1,
),
),
child: Column(
children: [
for (var index = 0;
index < devices.length;
index++) ...[
DeviceTab(
icon: devices[index]['type'] == 'iPhone' ||
devices[index]['type'] == 'Android'
? 'PHONE'
: 'PC',
info: devicesFormatTime(
devices[index]['date'] * 1000),
subtitle:
"${devices[index]['city']}, ${devices[index]['country']}",
title:
"${devices[index]['device_type']} - ${devices[index]['browser']}",
onTap: () {
final model = Provider.of<BottomSheetModel>(
context,
listen: false);
model.resetCurrentIndex();

showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) {
return ListModal(
model: model,
children: [
modalInfoDevices(
"${devices[index]['device_type']} - ${devices[index]['browser']}",
DateFormat('dd/MM/yyyy').format(
DateTime.fromMillisecondsSinceEpoch(
devices[index]['date'] * 1000)),
"${devices[index]['city']}, ${devices[index]['country']}",
devices[index]['id'],
devices[index]['type'] == 'iPhone' ||
devices[index]['type'] ==
'Android'
? 'PHONE'
: 'PC',
context,
getDevices,
),
],
);
},
);
},
type:
devices.length > 1 && index > devices.length - 1
? 'Top'
: 'Only',
selected: false,
outlineIcon: SvgPicture.asset(
'assets/images/utils/chevron-right.svg',
child: FutureBuilder(
future: getDevices(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation<Color>(
AppColors.blue700,
),
),
),
),
],
],
);
} else if (snapshot.hasError) {
return Center(child: Text('Erreur de chargement'));
} else {
return Column(
children: [
for (var index = 0;
index < devices.length;
index++) ...[
DeviceTab(
icon: devices[index]['type'] == 'iPhone' ||
devices[index]['type'] == 'Android'
? 'PHONE'
: 'PC',
info: devicesFormatTime(
devices[index]['date'] * 1000),
subtitle:
"${devices[index]['city']}, ${devices[index]['country']}",
title:
"${devices[index]['device_type']} - ${devices[index]['browser']}",
onTap: () {
final model = Provider.of<BottomSheetModel>(
context,
listen: false);
model.resetCurrentIndex();

showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
isScrollControlled: true,
builder: (context) {
return ListModal(
model: model,
children: [
modalInfoDevices(
"${devices[index]['device_type']} - ${devices[index]['browser']}",
DateFormat('dd/MM/yyyy').format(
DateTime
.fromMillisecondsSinceEpoch(
devices[index]
['date'] *
1000)),
"${devices[index]['city']}, ${devices[index]['country']}",
devices[index]['id'],
devices[index]['type'] ==
'iPhone' ||
devices[index]['type'] ==
'Android'
? 'PHONE'
: 'PC',
context,
getDevices,
),
],
);
},
);
},
type: index < devices.length - 1
? 'Top'
: 'Only',
selected: false,
outlineIcon: SvgPicture.asset(
'assets/images/utils/chevron-right.svg',
),
),
],
],
);
}
},
),
),
],
Expand Down Expand Up @@ -242,11 +259,11 @@ Widget modalInfoDevices(String name, String date, String location, String id,
variant: Variant.deleteBordered,
size: SizeButton.md,
msg: const Text('Déconnecter l\'appareil'),
onPressed: () {
removeDevice(id, context).then(
onPressed: () async {
await removeDevice(id, context).then(
(name) {
load2fa();
Navigator.pop(context);
load2fa();
},
);
},
Expand Down
22 changes: 10 additions & 12 deletions patient_mobile/lib/services/devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

Future<List<dynamic>> getAllDevices(BuildContext context) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String? token = prefs.getString('token');
String url = '${dotenv.env['URL']}/dashboard/devices';
final response = await http.get(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token'
},
final response = await httpRequest(
type: RequestType.get,
endpoint: '/dashboard/devices',
needsToken: true,
context: context,
);
if (response.statusCode == 200) {
return jsonDecode(response.body)['devices'];
if (response != null) {
return response['devices'];
}

return [];
}

Expand Down Expand Up @@ -70,11 +67,12 @@ Future removeDevice(String id, BuildContext context) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String? token = prefs.getString('token');
String url = '${dotenv.env['URL']}/dashboard/device/$id';
await http.delete(
final response = await http.delete(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token'
},
);
return response;
}

0 comments on commit d2086ef

Please sign in to comment.