Compare commits

...

2 Commits

Author SHA1 Message Date
de8d898289
Fix sign-in, title bar, formatting 2024-08-27 08:21:15 +02:00
e996702408
Implement deleting favorites on frontend 2024-08-27 08:17:48 +02:00
3 changed files with 121 additions and 97 deletions

View File

@ -67,10 +67,8 @@ Future<bool> isLoggedIn(BuildContext context) async {
final prefs = await SharedPreferences.getInstance(); final prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token'); final token = prefs.getString('token');
if (token == null){ if (token == null) {
prefs.remove('id'); prefs.remove('id');
loggedIn = false;
user = User as User?;
return false; return false;
} }

View File

@ -52,101 +52,96 @@ class _SideMenuState extends State<SideMenu> {
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary, backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Row( title: Text('SkanTravels',
children: [ style: GoogleFonts.jacquesFrancois(
const SizedBox(width: 55), fontSize: 30,
Text('SkanTravels', color: Colors.black,
style: GoogleFonts.jacquesFrancois(
fontSize: 30,
color: Colors.black,
),
), ),
], ),
), ),
), drawer: Drawer(
drawer: Drawer( child: ListView(
child: ListView( padding: EdgeInsets.zero,
padding: EdgeInsets.zero, children: [
children: [ DrawerHeader(
DrawerHeader( child: Column(
child: Column( children: [
children: [ const Image(image: AssetImage('assets/logo.png'), height: 100),
const Image(image: AssetImage('assets/logo.png'), height: 100), Text(
Text( 'SkanTravels',
'SkanTravels', style: GoogleFonts.jacquesFrancois(
style: GoogleFonts.jacquesFrancois( fontSize: 20,
fontSize: 20, color: const Color(0xFF1862E7),
color: const Color(0xFF1862E7), ),
), ),
), ],
], ),
), ),
), ListTile(
ListTile( title: const Text('Home'),
title: const Text('Home'), leading: const Icon(Icons.home),
leading: const Icon(Icons.home), selected: _selectedIndex == 0,
selected: _selectedIndex == 0, onTap: () {
onTap: () { Navigator.pushReplacementNamed(context, '/home');
Navigator.pushReplacementNamed(context, '/home'); },
}, ),
), ListTile(
ListTile( title: const Text('Favorites'),
title: const Text('Favorites'), leading: const Icon(Icons.star),
leading: const Icon(Icons.star), selected: _selectedIndex == 1,
selected: _selectedIndex == 1, onTap: () {
onTap: () { Navigator.pushReplacementNamed(context, '/favorites');
Navigator.pushReplacementNamed(context, '/favorites'); },
}, ),
), ListTile(
ListTile( title: const Text('Profile'),
title: const Text('Profile'), leading: const Icon(Icons.person),
leading: const Icon(Icons.person), selected: _selectedIndex == 2,
selected: _selectedIndex == 2, onTap: () {
onTap: () { Navigator.pushReplacementNamed(context, '/profile');
Navigator.pushReplacementNamed(context, '/profile'); },
}, ),
), const Divider(
const Divider( color: Colors.grey,
color: Colors.grey, thickness: 2,
thickness: 2, indent: 40,
indent: 40, ),
), ...(loggedIn
...(loggedIn ? [
? [ ListTile(
ListTile( title: const Text('Log out'),
title: const Text('Log out'), leading: const Icon(Icons.logout),
leading: const Icon(Icons.logout), selected: false,
selected: false, onTap: _logout,
onTap: _logout, ),
), ]
] : [
: [ ListTile(
ListTile( title: const Text('Register'),
title: const Text('Register'), leading: const Icon(Icons.add_box_outlined),
leading: const Icon(Icons.add_box_outlined), selected: _selectedIndex == 3,
selected: _selectedIndex == 3, onTap: () {
onTap: () { Navigator.pushReplacementNamed(context, '/register');
Navigator.pushReplacementNamed(context, '/register'); },
}, ),
), ListTile(
ListTile( title: const Text('Login'),
title: const Text('Login'), leading: const Icon(Icons.login),
leading: const Icon(Icons.login), selected: _selectedIndex == 4,
selected: _selectedIndex == 4, onTap: () {
onTap: () { Navigator.pushReplacementNamed(context, '/login');
Navigator.pushReplacementNamed(context, '/login'); },
}, ),
), ]
] ),
), ],
], ),
), ),
), body: widget.body,
body: widget.body, );
); }
}
} }

View File

@ -14,6 +14,34 @@ class FavoritesPage extends StatefulWidget {
class _FavoritesPage extends State<FavoritesPage> { class _FavoritesPage extends State<FavoritesPage> {
List<Favorite> _favorites = []; List<Favorite> _favorites = [];
void _confirmDeleteFavorite(Favorite favorite) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Remove favorite'),
content: Text('Are you sure you want to remove ${favorite.name} from your favorites list?'),
actions: [
TextButton(onPressed: () => Navigator.pop(context), child: const Text('Cancel')),
TextButton(onPressed: () => _deleteFavorite(favorite), child: const Text('Remove', style: TextStyle(color: Colors.red))),
],
);
}
);
}
void _deleteFavorite(Favorite favorite) async {
Navigator.pop(context);
if (await api.request(context, api.ApiService.app, 'DELETE', '/favorites/${favorite.id}', null) == null) {
return;
}
setState(() {
_favorites = _favorites.where((fav) => fav.id != favorite.id).toList();
});
}
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
@ -57,7 +85,10 @@ class _FavoritesPage extends State<FavoritesPage> {
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const Padding(padding: EdgeInsets.only(top: 3), child: Icon(Icons.star, color: Colors.yellow, size: 36)), const Padding(
padding: EdgeInsets.only(top: 3),
child: Icon(Icons.star, color: Colors.yellow, size: 36)
),
const SizedBox(width: 20), const SizedBox(width: 20),
Expanded( Expanded(
child: Column( child: Column(
@ -69,7 +100,7 @@ class _FavoritesPage extends State<FavoritesPage> {
), ),
), ),
const SizedBox(width: 20), const SizedBox(width: 20),
const Padding(padding: EdgeInsets.only(top: 5), child: Icon(Icons.delete, color: Colors.grey)), IconButton(icon: const Icon(Icons.delete), color: Colors.grey, onPressed: () => _confirmDeleteFavorite(favorite)),
], ],
), ),
)).toList(), )).toList(),