Escali License control 1
EditCustomerModal.razor
Gå til dokumentasjonen til denne filen.
1@using Castle.Core.Internal
2@using Microsoft.AspNetCore.Components
3@using Escali.LicenseControl.Client.ClientModels
4@using Escali.LicenseControl.Client.ViewModels
5@using AutoMapper
6@using Escali.LicenseControl.Client.Utils
7@inject IMapper _mapper
8@inject CustomerViewModel _customerViewService
9@inject PriorityViewModel _priorityViewService
10@inject MainSegmentViewModel _mainSegmentViewService
11@inject SegmentViewModel _segmentViewService
12@inject RegionViewModel _regionViewService
13@implements IDisposable
14@inject StateService _stateService
15@inject UserViewModel _userViewService
16
17@if (_stateService.EditCustomer.Value != null)
18{
19 <EditForm Model="_stateService.EditCustomer.Value" OnValidSubmit="Submit" OnInvalidSubmit="EventCallback.Empty">
20 <StandardModal Header="Kundeinfo" SubmitButtonName="Oppdater kunde" ShowModal="true" OnModalClosed="ResetCustomer">
21
22 <div class="create-modal-container">
23 <ObjectGraphDataAnnotationsValidator />
24
25 <FloatingInput Name="Kundenavn*">
26 <InputText @bind-Value="_stateService.EditCustomer.Value.CustomerName" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
27 </FloatingInput>
28
29 <FloatingInput Name="Org.Nr.*">
30 <InputText @bind-Value="_stateService.EditCustomer.Value.OrganizationNumber" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
31 </FloatingInput>
32
33 <FloatingInput Name="Reskontronummer">
34 <InputText @bind-Value="_stateService.EditCustomer.Value.LedgerNumber" type="text" class="form-control input-text" id="floatingInput" placeholder=" " autocomplete="off" />
35 </FloatingInput>
36
37 @if (_regionViewService.Regions != null)
38 {
39 <FloatingInput Name="Land*">
40 <InputText @bind-Value="CountryInput" type="text" class="form-control input-text " id="floatingInput" placeholder=" " list="countryList" autocomplete="off" />
41 </FloatingInput>
42 <datalist id="countryList">
43 @foreach (var r in _regionViewService.Regions.DistinctBy(r => r.Country).ToList())
44 {
45 <option value="@r.Country">@r.Country</option>
46 }
47 </datalist>
48 }
49
50 @if (_regionViewService.Regions != null && CountryInput != string.Empty)
51 {
52 <FloatingInput Name="Region">
53 <InputText @bind-Value="AreaInput" type="text" class="form-control input-text" id="floatingInput" placeholder=" " list="areaList" autocomplete="off" />
54 </FloatingInput>
55 <datalist id="areaList">
56 @foreach (var r in _regionViewService.Regions.Where(r => r.Country == _stateService.EditCustomer.Value.Region.Country).DistinctBy(r => r.Area).ToList())
57 {
58 <option value="@r.Area">@r.Area</option>
59 }
60 </datalist>
61 }
62
63 <div class="input-container">
64 <InputSelect class="form-select form-select-modal" @bind-Value="_stateService.EditCustomer.Value.PrioritySeq">
65 <option selected disabled>Velg prioritet</option>
66 @if (_priorityViewService.Priorities != null)
67 {
68 @foreach (var p in _priorityViewService.Priorities)
69 {
70 <option value="@p.PrioritySeq">@p.PriorityName.ToString()?.CapitalizeFirstLetter()</option>
71 }
72 }
73 </InputSelect>
74 </div>
75
76 <div class="input-container">
77 <InputSelect class="form-select form-select-modal" @bind-Value="_stateService.EditCustomer.Value.UserSeq">
78 <option selected disabled>Velg kundeansvarlig</option>
79 @if (_userViewService.Users != null)
80 {
81 @foreach (var u in _userViewService.Users)
82 {
83 <option value="@u.UserSeq">@u.UserEmail</option>
84 }
85 }
86 </InputSelect>
87 </div>
88
89 <div class="input-container">
90 <InputSelect class="form-select form-select-modal" @bind-Value="SelectedMainSegmentSeq">
91 <option selected disabled>Velg hovedbransje</option>
92 @if (_mainSegmentViewService.MainSegments != null)
93 {
94 @foreach (var ms in _mainSegmentViewService.MainSegments)
95 {
96 <option value="@ms.MainSegmentSeq">@ms.MainSegmentName</option>
97 }
98 }
99 </InputSelect>
100 </div>
101
102 @if (SelectedMainSegmentSeq != null && !_mainSegmentViewService.MainSegments.IsNullOrEmpty())
103 {
104 <div class="input-container">
105 <InputSelect class="form-select form-select-modal" @bind-Value="@_stateService.EditCustomer.Value.SegmentSeq">
106 <option disabled selected>Velg bransje</option>
107 @if (SelectedMainSegmentSeq != null && !_mainSegmentViewService.MainSegments.SingleOrDefault(s => s.MainSegmentSeq == SelectedMainSegmentSeq).Segments.IsNullOrEmpty() ) {
108 @foreach (var s in _mainSegmentViewService.MainSegments.SingleOrDefault(s => s.MainSegmentSeq == SelectedMainSegmentSeq).Segments)
109 {
110 <option value="@s.SegmentSeq">@s.SegmentName</option>
111 }
112 }
113 </InputSelect>
114 </div>
115 }
116
117 </div>
118
119 </StandardModal>
120 </EditForm>
121}
122
123@code {
124
125 private int? SelectedMainSegmentSeq { get; set; }
126
127 private string CountryInput { get; set; }
128 private string AreaInput { get; set; }
129
130 private async void Submit()
131 {
132 _stateService.EditCustomer.Value!.Region = new RegionClientModel
133 {
134 Country = CountryInput,
135 Area = AreaInput == string.Empty ? null : AreaInput
136 };
137
138 var updatedCustomer = await _customerViewService.UpdateCustomer(_stateService.EditCustomer.Value!);
139
140 if (updatedCustomer != null)
141 {
142 ResetCustomer();
143 }
144 }
145
146 private void ResetCustomer()
147 {
148 _stateService.EditCustomer.ValueChanged -= SetInputFields;
149 _stateService.EditCustomer.Value = null;
150 SelectedMainSegmentSeq = null;
151
152 _stateService.EditCustomer.ValueChanged += SetInputFields;
153 }
154
155 private async void SetInputFields()
156 {
157 // var region = _regionViewService.Regions?.SingleOrDefault(r => r.RegionSeq == _stateService.EditCustomer.Value!.RegionSeq);
158 var region = _stateService.EditCustomer.Value!.Region;
159 CountryInput = region!.Country;
160 AreaInput = region.Area;
161 if (_stateService.EditCustomer.Value.Segment != null && _mainSegmentViewService.MainSegments != null)
162 {
163 SelectedMainSegmentSeq = _mainSegmentViewService.MainSegments.Single(ms => ms.MainSegmentSeq == _stateService.EditCustomer.Value.Segment.MainSegmentSeq).MainSegmentSeq;
164 }
165
166 await InvokeAsync(_regionViewService.GetRegions);
167 }
168
169 protected override void OnInitialized()
170 {
171 _stateService.EditCustomer.ValueChanged += StateHasChanged;
172 _regionViewService.DataChanged += StateHasChanged;
173 _mainSegmentViewService.DataChanged += StateHasChanged;
174 _segmentViewService.DataChanged += StateHasChanged;
175
176 _stateService.EditCustomer.ValueChanged += SetInputFields;
177
178 if (_stateService.EditCustomer.Value != null) SetInputFields();
179 }
180
181 void IDisposable.Dispose()
182 {
183 _stateService.EditCustomer.ValueChanged -= StateHasChanged;
184 _regionViewService.DataChanged -= StateHasChanged;
185 _mainSegmentViewService.DataChanged -= StateHasChanged;
186 _segmentViewService.DataChanged -= StateHasChanged;
187
188 _stateService.EditCustomer.ValueChanged -= SetInputFields;
189 }
190
191}