aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Modules/Tango.FSE.UsersAndRoles/ViewModels/OrganizationSelectionViewVM.cs
blob: 9a84cca7d965891a114f48418509063f5ddb03b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.Commands;
using Tango.FSE.Common;
using Tango.FSE.Common.AutoComplete;
using Tango.FSE.UsersAndRoles.Navigation;
using Tango.FSE.UsersAndRoles.Views;

namespace Tango.FSE.UsersAndRoles.ViewModels
{
    public class OrganizationSelectionViewVM : UsersAndRolesViewModel
    {
        /// <summary>
        /// Gets or sets the organizations completion source.
        /// </summary>
        public AutoCompleteSource<Organization> Organizations { get; set; }

        private Organization _selectedOrganization;
        /// <summary>
        /// Gets or sets the selected organization.
        /// </summary>
        public Organization SelectedOrganization
        {
            get { return _selectedOrganization; }
            set { _selectedOrganization = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        /// <summary>
        /// Navigates to the organization users list with the specified organization.
        /// </summary>
        public RelayCommand ManageOrganizationCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrganizationSelectionViewVM"/> class.
        /// </summary>
        public OrganizationSelectionViewVM()
        {
            Organizations = new AutoCompleteSource<Organization>(AutoCompleteOrganizations);
            ManageOrganizationCommand = new RelayCommand(ManageSelectedOrganization, () => SelectedOrganization != null);
        }

        private void ManageSelectedOrganization()
        {
            if (SelectedOrganization == null)
            {
                NotificationProvider.ShowError("No organization selected.");
                return;
            }

            ModularNavigationManager.NavigateTo(UsersAndRolesView.OrganizationUsersView, new OrganizationUsersViewVM.NavigationObject()
            {
                Organization = SelectedOrganization
            });
        }

        private List<Organization> AutoCompleteOrganizations(string key)
        {
            key = key ?? String.Empty;

            try
            {
                return Services.OrganizationsService.GetCurrentUserOrganizations().Result.Where(x => x.Name.ToLower().StartsWith(key.ToLower())).Take(4).ToList();
            }
            catch (Exception ex)
            {
                LogManager.Log(ex, "Error on auto complete organizations filter.");
                return new List<Organization>();
            }
        }

        public async override void OnNavigatedTo()
        {
            base.OnNavigatedTo();

            if (SelectedOrganization == null)
            {
                await Task.Delay(200);
                SelectedOrganization = AuthenticationProvider.CurrentUser.Organization;
            }
        }

        public override Task<bool> OnApplicationLogout()
        {
            SelectedOrganization = null;
            return base.OnApplicationLogout();
        }
    }
}