-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathProfile.razor
More file actions
112 lines (97 loc) · 3.33 KB
/
Copy pathProfile.razor
File metadata and controls
112 lines (97 loc) · 3.33 KB
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
@using LinkDotNet.Blog.Domain
@using LinkDotNet.Blog.Infrastructure.Persistence
@using LinkDotNet.Blog.Web.Features.Services
@inject IRepository<ProfileInformationEntry> Repository
@inject ISortOrderCalculator SortOrderCalculator
<div class="profile-card">
<div class="profile-name">
<span>@ProfileInformation.Name</span>
<br/>
<span>@ProfileInformation.Heading</span>
</div>
<div class="profile-image">
<img src="@ProfileInformation.ProfilePictureUrl" alt="Profile Picture" />
</div>
<ul class="profile-keypoints"
ondragover="event.preventDefault();">
@foreach (var entry in profileInformationEntries)
{
@if (ShowAdminActions)
{
<li
class="item-draggable"
draggable="true"
@ondrag="@(() => currentDragItem = entry)"
@ondrop="@(() => HandleDrop(entry))">
<button type="button" class="btn btn-default" aria-label="Delete Item" @onclick="() => ShowDeleteDialog(entry.Content)">
<i class="bin2" aria-hidden="true"></i>
</button>
@MarkdownConverter.ToMarkupString(entry.Content)
</li>
}
else
{
<li>@MarkdownConverter.ToMarkupString(entry.Content)</li>
}
}
@if (ShowAdminActions)
{
<AddProfileShortItem ValueAdded="@AddValue"></AddProfileShortItem>
}
</ul>
</div>
<ConfirmDialog @ref="Dialog" Content="Do you really want to delete this entry?" Title="Delete"
OnYesPressed="DeleteItem"></ConfirmDialog>
@code {
[Parameter]
public bool ShowAdminActions { get; set; }
[Parameter, EditorRequired]
public required ProfileInformation ProfileInformation { get; set; }
private List<ProfileInformationEntry> profileInformationEntries = [];
private ConfirmDialog Dialog { get; set; } = default!;
private string? currentDeleteKey;
private ProfileInformationEntry? currentDragItem;
protected override async Task OnInitializedAsync()
{
profileInformationEntries = (await Repository.GetAllAsync(orderBy: d => d.SortOrder, descending: false)).ToList();
}
private void ShowDeleteDialog(string key)
{
currentDeleteKey = key;
Dialog.Open();
}
private async Task DeleteItem()
{
var entryToDelete = profileInformationEntries.Single(p => p.Content == currentDeleteKey);
profileInformationEntries.Remove(entryToDelete);
await Repository.DeleteAsync(entryToDelete.Id);
}
private async Task AddValue(string toAdd)
{
var sortOrder = GetSortOrder();
var newEntry = ProfileInformationEntry.Create(toAdd, sortOrder);
profileInformationEntries.Add(newEntry);
await Repository.StoreAsync(newEntry);
}
private int GetSortOrder()
{
if (profileInformationEntries.Any())
{
return profileInformationEntries.Max(p => p.SortOrder) + 1000;
}
return 1000;
}
private async Task HandleDrop(ProfileInformationEntry dropTarget)
{
if (currentDragItem is null || dropTarget == currentDragItem)
{
return;
}
var newSortOrder = SortOrderCalculator.GetSortOrder(dropTarget, profileInformationEntries);
currentDragItem.SortOrder = newSortOrder;
await Repository.StoreAsync(currentDragItem);
currentDragItem = null;
profileInformationEntries.Sort((a, b) => a.SortOrder.CompareTo(b.SortOrder));
StateHasChanged();
}
}