part of 'widgets.dart';

enum PopupAction {
  home,
  logout
}

final _extraAction = RM.inject(
  () => PopupAction.home
);

class PopupActionsButton extends StatelessWidget {
  const PopupActionsButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return On.data(
      () {
        return PopupMenuButton<PopupAction>(
          onSelected: (action) async {
            _extraAction.state = action;
            if (action == PopupAction.logout) {
              String? token = user.state!.data!.token;
              int? userId = user.state!.data!.userId;
              user.auth.signOut(
                param: (_) {
                  return LoginParam(
                    token: token,
                    userId: userId,
                  );
                },
                onError: (error, refresh) {
                  showToastWidget(
                    error.message,
                    duration: 'long'
                  );
                }
              );
              return;
            }
          },
          itemBuilder: (BuildContext context) {
            return <PopupMenuItem<PopupAction>>[
              PopupMenuItem<PopupAction>(
                key: Key('__logout__'),
                value: PopupAction.logout,
                child: Text("Logout"),
              )
            ];
          },
        );
      },
    ).listenTo(_extraAction);
  }
}
