forked from SteveSandersonMS/CarChecker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleNoteEditor.razor
125 lines (102 loc) · 3.68 KB
/
VehicleNoteEditor.razor
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@inject IJSRuntime JS
@inject IStringLocalizer<App> Localize
@using BlazorInputFile
<Overlay @ref="overlay" OverlayStyle="Overlay.Style.FullScreen" CssClass="vehicle-note-editor-overlay" OnCloseRequested="Dismiss">
<button class="overlay-close" @onclick="Dismiss">✖</button>
<EditForm EditContext="editContext" OnValidSubmit="SaveAsync">
<DataAnnotationsValidator />
<div class="form-field">
<label>@Localize["Location:"]</label>
<InputSelect @bind-Value="@formData.Location">
@foreach (VehiclePart part in Enum.GetValues(typeof(VehiclePart)))
{
<option value="@part">@Localize[part.DisplayName()]</option>
}
</InputSelect>
<ValidationMessage For="() => formData.Location" />
</div>
<div class="form-field">
<label>@Localize["Description:"]</label>
<InputTextArea @bind-Value="@formData.Text" />
<ValidationMessage For="() => formData.Text" />
</div>
<div class="form-field">
<label>@Localize["Photo:"]</label>
<InputFile id="image-picker" OnChange="HandlePhotoSelected" accept="image/*" />
<label class="text-button" for="image-picker">
<img src="@formData.PhotoUrl" class="vehicle-note-photo" />
@Localize["Take new"]
</label>
<DamageDetection Image="@formData.PhotoUrl" />
</div>
<div class="toolbar">
@if (isExistingItem)
{
<button type="button" class="toolbar-item" @onclick="DeleteAsync">@Localize["Delete"]</button>
}
<button type="submit" class="toolbar-item toolbar-item-end toolbar-item-bg">@Localize["Save"]</button>
</div>
</EditForm>
</Overlay>
@code {
[Parameter] public EventCallback OnCommittedEdit { get; set; }
EditContext editContext;
Overlay overlay;
InspectionNote formData;
Vehicle vehicle;
InspectionNote vehicleNote;
bool isExistingItem;
public VehicleNoteEditor()
{
formData = new InspectionNote();
editContext = new EditContext(formData);
}
public void Show(Vehicle vehicle, InspectionNote noteToEdit)
{
this.vehicle = vehicle;
this.vehicleNote = noteToEdit;
isExistingItem = vehicle.Notes.Contains(noteToEdit);
formData.CopyFrom(noteToEdit);
editContext.MarkAsUnmodified();
overlay.Show();
}
Task CommitEdit()
{
overlay.Hide();
return OnCommittedEdit.InvokeAsync(null);
}
Task DeleteAsync()
{
vehicle.Notes.Remove(vehicleNote);
return CommitEdit();
}
Task SaveAsync()
{
if (!isExistingItem)
{
vehicle.Notes.Add(vehicleNote);
}
vehicleNote.CopyFrom(formData);
return CommitEdit();
}
async Task Dismiss()
{
await Task.Yield(); // In case this event triggers other things too (e.g., form edits), let that happen first
if (!editContext.IsModified() || await JS.InvokeAsync<bool>("confirm", Localize["Discard changes?"].Value))
{
overlay.Hide();
}
}
async Task HandlePhotoSelected(IFileListEntry[] files)
{
var sourceFile = files.FirstOrDefault();
if (sourceFile != null)
{
// Convert to reasonably-sized JPEG
var imageFile = await sourceFile.ToImageFileAsync("image/jpeg", 800, 600);
// Represent it as a data URL we can display
var bytes = await imageFile.ReadAllAsync();
formData.PhotoUrl = bytes.ToDataUrl("image/jpeg");
}
}
}