Skip to content

Commit 16f1979

Browse files
MudCsvMapper: Allow default value, change ui, can add expected headers, choose whether to include unmapper data, major refactor (#98)
Co-authored-by: Mehmet Can Karagöz <[email protected]>
1 parent 621ba8d commit 16f1979

File tree

4 files changed

+322
-139
lines changed

4 files changed

+322
-139
lines changed

CodeBeam.MudExtensions/CodeBeam.MudExtensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
</ItemGroup>
2222

2323
<ItemGroup>
24+
<PackageReference Include="CsvHelper" Version="30.0.1" />
2425
<PackageReference Include="MudBlazor" Version="6.1.8" />
2526
</ItemGroup>
2627

CodeBeam.MudExtensions/Components/CsvMapper/MudCsvMapper.razor

Lines changed: 86 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,60 +33,108 @@
3333
<MudFab HtmlTag="Button"
3434
Color="Color.Secondary"
3535
Label="@LocalizedStrings.ChooseFile"
36-
Icon="@Icons.Outlined.CloudDownload" Size="Size.Large"
37-
for="@context" />@LocalizedStrings.OrDragAndDrop
36+
Icon="@Icons.Outlined.CloudDownload" Size="Size.Large"/>@LocalizedStrings.OrDragAndDrop
3837
</h3>
3938
</MudPaper>
4039
<MudPaper Elevation="0" class="d-flex justify-center align-content-center mud-transparent m2">
41-
@foreach (var file in FileNames)
40+
@if (FileNames.Count > 0)
4241
{
43-
<MudChip Color="Color.Dark" Text="@file" />
42+
<MudChip Color="Color.Dark" Text="@FileNames.LastOrDefault()"/>
4443
}
4544
</MudPaper>
4645
</MudPaper>
4746
</ButtonTemplate>
4847
</MudFileUpload>
4948
</MudStack>
5049

51-
<MudDropContainer T="MudCsvHeader" @ref="DropContainer" Items="@MudCsvHeaders" ItemsSelector="@((item,column) => item.MappedField == column)" ItemDropped="TaskUpdated">
50+
<MudDropContainer T="MudCsvHeader" Items="@MudCsvHeaders" ItemsSelector="@ItemSelector" ItemDropped="OnDrop">
5251
<ChildContent>
5352
@if (MudCsvHeaders.Count > 0)
5453
{
5554
<h2 class="d-flex" style="width:100%">CSV File Headers</h2>
5655
<div class="d-flex flex-column flex-grow-1 my-2">
5756
<MudDropZone T="MudCsvHeader" Identifier="File" DraggingClass="mud-alert-text-info"
58-
ItemDraggingClass="mud-alert-text-info" style="min-height:40px"
59-
Class="rounded-lg border-2 border-dashed mud-border-lines-default pa-2 flex-row">
57+
ItemDraggingClass="mud-alert-text-info" style="min-height:40px"
58+
Class="rounded-lg border-2 border-dashed mud-border-lines-default pa-2 flex-row">
6059
</MudDropZone>
6160
</div>
6261
}
63-
<div class="d-flex flex-wrap justify-space-between">
64-
@if (MudFieldHeaders.Count == 0)
62+
<h2 class="d-flex" style="width:100%">@LocalizedStrings.ExpectedHeaders</h2>
63+
<div class="d-flex flex-wrap justify-space-between" style="margin-top: 15px; row-gap: 15px;">
64+
@if (ExpectedHeaders.Count == 0)
6565
{
6666
<MudText Typo="Typo.overline" class="--mud-palette-error">@LocalizedStrings.DefineHeaders</MudText>
6767
}
68-
<h2 class="d-flex" style="width:100%">@LocalizedStrings.ExpectedHeaders</h2>
6968

70-
@foreach (var item in MudFieldHeaders)
69+
@foreach (var item in ExpectedHeaders)
7170
{
72-
<MudDropZone T="MudCsvHeader" Identifier="@item.Name" DraggingClass="mud-alert-text-info"
73-
CanDrop="@((x) => (item.FieldCount == 0))"
74-
ItemDraggingClass="mud-alert-text-error" Class="rounded-lg border-2 border-dashed mud-border-lines-default pa-2 my-1">
75-
<MudText Typo="Typo.subtitle1">
76-
<b>@item.Name</b>
77-
@if (item.Required)
71+
var required = item.Required;
72+
var matched = item.MatchedFieldCount > 0;
73+
bool confirmed = _defaultValueHeaders.ContainsKey(item.Name) && _defaultValueHeaders[item.Name].Confirmed;
74+
string warning;
75+
if (item.AllowDefaultValue)
76+
{
77+
warning = "This field is required. You must either provide a default value or map a field from the imported csv.";
78+
}
79+
else
80+
{
81+
warning = "This field is required. You must map a field from the imported csv.";
82+
}
83+
84+
<MudBadge Icon="@Icons.Material.Filled.Warning" Color="Color.Error" Overlap="true" Visible="@(required && !confirmed && !matched)">
85+
<MudDropZone T="MudCsvHeader" Identifier="@item.Name" DraggingClass="mud-alert-text-info"
86+
CanDrop="@((x) => (item.MatchedFieldCount == 0))"
87+
ItemDraggingClass="mud-alert-text-error" Class="rounded-lg border-2 border-dashed mud-border-lines-default pa-2 my-1"
88+
Style="@((required && !matched && !confirmed ? item.RequiredCss : "") + _expectedHeaderDropZoneWidth)">
89+
<MudText Typo="Typo.subtitle1">
90+
<b>@item.Name</b>
91+
@if (required)
92+
{
93+
<b>*</b>
94+
}
95+
@if (!matched && item.CreatingDefaultValue)
96+
{
97+
<MudIconButton Style="padding-left: 8px;" Icon="@Icons.Outlined.ArrowBackIosNew" Color="Color.Secondary" OnClick="@(() => item.CreatingDefaultValue = !item.CreatingDefaultValue)"></MudIconButton>
98+
}
99+
else if(!matched && item.AllowDefaultValue)
100+
{
101+
<MudIconButton Style="padding-left: 8px;" Icon="@Icons.Outlined.Add" Color="Color.Secondary" OnClick="@(() => item.CreatingDefaultValue = !item.CreatingDefaultValue)"></MudIconButton>
102+
}
103+
</MudText>
104+
@if (!matched && !confirmed)
78105
{
79-
<b>*</b>
106+
<MudPaper Elevation="0" Class="pa-2 ma-2 d-flex flex-column mud-background-gray rounded-lg">
107+
<MudText Typo="Typo.overline">@LocalizedStrings.DragHere</MudText>
108+
</MudPaper>
80109
}
81-
</MudText>
82-
@if (item.FieldCount == 0)
83-
{
84-
<MudPaper Elevation="0" Class="pa-2 ma-2 d-flex flex-column mud-background-gray rounded-lg">
85-
<MudText Typo="Typo.overline">@LocalizedStrings.DragHere</MudText>
86-
</MudPaper>
87-
}
88-
</MudDropZone>
110+
@if (!matched && item.CreatingDefaultValue)
111+
{
112+
<MudTextField Disabled="_defaultValueHeaders[item.Name].Confirmed" @bind-Value="_defaultValueHeaders[item.Name].DefaultValue" Required="@item.Required" RequiredError="@_requiredDefaultValueMessage" Style="margin-top: 10px" Label="@("Default Value" )" Variant="Variant.Text"></MudTextField>
113+
<MudButton OnClick="@(() => SubmitDefaultValue(item.Name))" ButtonType="ButtonType.Submit">@(_defaultValueHeaders[item.Name].Confirmed ? "Edit" : "Confirm")</MudButton>
114+
}
115+
116+
</MudDropZone>
117+
</MudBadge>
89118
}
119+
120+
<MudPaper Elevation="0" Width="180px">
121+
@if (AllowCreateExpectedHeaders && _addSectionOpen)
122+
{
123+
<MudPaper Elevation="0" Width="180px" Class="rounded-lg mud-background-gray pa-2 my-1">
124+
<EditForm Model="@_model" OnSubmit="OnSubmit">
125+
<MudIconButton Style="padding-left: 8px;" Icon="@Icons.Outlined.ArrowBackIosNew" Color="Color.Secondary" OnClick="@(() => _addSectionOpen = false)"></MudIconButton>
126+
<MudTextField @bind-Value="_model.Name" Label="Expected Header" Required="true" For="@(() => _model.Name)"></MudTextField>
127+
<MudSwitch Color="Color.Primary" T="bool" @bind-Checked="_model.Required">Required</MudSwitch>
128+
<MudSwitch Color="Color.Primary" T="bool" @bind-Checked="_model.AllowDefaultValue">Allow Default Value</MudSwitch>
129+
<MudButton ButtonType="ButtonType.Submit" Size="Size.Small" Color="Color.Primary" FullWidth="true">Add Header</MudButton>
130+
</EditForm>
131+
</MudPaper>
132+
}
133+
else if(AllowCreateExpectedHeaders)
134+
{
135+
<MudButton OnClick="OpenAddSection" Variant="Variant.Outlined" StartIcon="@Icons.Material.Filled.Add" Color="Color.Primary" Class="rounded-lg py-2" FullWidth="true">Create Header</MudButton>
136+
}
137+
</MudPaper>
90138
</div>
91139

92140
</ChildContent>
@@ -95,6 +143,16 @@
95143
</ItemRenderer>
96144

97145
</MudDropContainer>
98-
99-
<MudButton Class="my-2" Color="Color.Primary" StartIcon="@Icons.Filled.CloudDownload" Variant="Variant.Filled" OnClick="@(() => Upload())" Disabled="!_valid">@LocalizedStrings.Import</MudButton>
146+
@if (ShowIncludeUnmappedData)
147+
{
148+
<MudSwitch @bind-Checked="_includeUnmappedData" Color="Color.Info">Include unmapped data</MudSwitch>
149+
}
150+
@if (!_importedComplete)
151+
{
152+
<MudButton Class="my-2" Color="Color.Primary" StartIcon="@Icons.Filled.CloudDownload" Variant="Variant.Filled" OnClick="@OnImport" Disabled="!_valid">@LocalizedStrings.Import</MudButton>
153+
}
154+
else
155+
{
156+
<MudButton Class="my-2" Color="Color.Secondary" StartIcon="@Icons.Filled.RestartAlt" Variant="Variant.Filled" OnClick="@ReloadPage" Disabled="!_valid">Reset</MudButton>
157+
}
100158
</div>

0 commit comments

Comments
 (0)