Skip to content

Commit

Permalink
Added DateSelector component to story book
Browse files Browse the repository at this point in the history
  • Loading branch information
dyatlov-a committed Oct 1, 2024
1 parent a0a02c9 commit 168e906
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 45 deletions.
10 changes: 6 additions & 4 deletions src/Inc.TeamAssistant.Stories/Components/Button.stories.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
<ArgType For="c => c.ButtonType" Control="ControlType.Select" />
<Story Name="Default" Layout="typeof(DarkLayout)">
<Arguments>
<Arg For="c => c.Content" Value='ToRenderFragment("Default")' />
<Arg For="c => c.Content" Value="ToRenderFragment(_text)" />
</Arguments>
<Template>
<Button @attributes="context.Args"></Button>
</Template>
</Story>
<Story Name="Small" Layout="typeof(DarkLayout)">
<Arguments>
<Arg For="c => c.Content" Value='ToRenderFragment("Small")' />
<Arg For="c => c.Content" Value="ToRenderFragment(_text)" />
<Arg For="c => c.ButtonMode" Value="ButtonMode.Small" />
</Arguments>
<Template>
Expand All @@ -22,7 +22,7 @@
</Story>
<Story Name="Dark" Layout="typeof(LightLayout)">
<Arguments>
<Arg For="c => c.Content" Value='ToRenderFragment("Dark")' />
<Arg For="c => c.Content" Value="ToRenderFragment(_text)" />
<Arg For="c => c.IsDark" Value="true" />
</Arguments>
<Template>
Expand All @@ -31,7 +31,7 @@
</Story>
<Story Name="Disabled" Layout="typeof(DarkLayout)">
<Arguments>
<Arg For="c => c.Content" Value='ToRenderFragment("Disabled")' />
<Arg For="c => c.Content" Value="ToRenderFragment(_text)" />
<Arg For="c => c.IsDisabled" Value="true" />
</Arguments>
<Template>
Expand All @@ -41,5 +41,7 @@
</Stories>

@code {
private readonly string _text = "Button";

private RenderFragment ToRenderFragment(string value) => b => b.AddContent(0, value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@attribute [Stories("Components/DateSelector")]

<Stories TComponent="DateSelector" Layout="typeof(LightLayout)">
<Story Name="Dates">
<Template>
<DateSelector Date="_date" Items="_dates" OnSelected="SelectDate"/>
</Template>
</Story>
<Story Name="Weeks">
<Template>
<DateSelector Date="_week" Items="_weeks" OnSelected="SelectWeek"/>
</Template>
</Story>
<Story Name="Months">
<Template>
<DateSelector Date="_month" Items="_months" OnSelected="SelectMonth"/>
</Template>
</Story>
</Stories>

@code {
private DateOnly? _date = new(2024, 09, 25);
private DateOnly? _week = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-28).Date);
private DateOnly? _month = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-6).Date);

private readonly IReadOnlyDictionary<string, DateOnly> _dates = new Dictionary<string, DateOnly>
{
["24-09-25"] = new(2024, 09, 25),
["24-09-11"] = new(2024, 09, 11),
["24-08-28"] = new(2024, 08, 28)
};

private readonly IReadOnlyDictionary<string, DateOnly> _weeks = new Dictionary<string, DateOnly>
{
["2 week"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-14).Date),
["4 weeks"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-28).Date),
["12 weeks"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-84).Date)
};

private readonly IReadOnlyDictionary<string, DateOnly> _months = new Dictionary<string, DateOnly>
{
["1 month"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-1).Date),
["3 month"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-3).Date),
["6 month"] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-6).Date)
};

private void SelectDate(DateOnly date) => _date = date;
private void SelectWeek(DateOnly date) => _week = date;
private void SelectMonth(DateOnly date) => _month = date;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BlazingStory" Version="1.0.0-preview.41" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.8" />
Expand Down
19 changes: 14 additions & 5 deletions src/Inc.TeamAssistant.WebUI/Components/DateSelector.razor
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
<div class="date-selector">
@foreach (var item in Items)
{
<span class="date-selector__item @CssClass(item)" @onclick="() => Selected(item)">
@item.Title
<span class="date-selector__item @CssClass(item.Value)" @onclick="() => Selected(item.Value)">
@item.Key
</span>
}
</div>

@code {
/// <summary>
/// Available for select dates.
/// </summary>
[Parameter, EditorRequired]
public IReadOnlyCollection<SelectItem<DateOnly>> Items { get; set; } = Array.Empty<SelectItem<DateOnly>>();
public IReadOnlyDictionary<string, DateOnly> Items { get; set; } = new Dictionary<string, DateOnly>();

/// <summary>
/// Selected date
/// </summary>
[Parameter, EditorRequired]
public DateOnly? Date { get; set; }

/// <summary>
/// On selected event
/// </summary>
[Parameter, EditorRequired]
public EventCallback<DateOnly> OnSelected { get; set; }

private string CssClass(SelectItem<DateOnly> item) => Date == item.Value
private string CssClass(DateOnly value) => Date == value
? "date-selector__item_selected"
: string.Empty;

private void Selected(SelectItem<DateOnly> item) => OnSelected.InvokeAsync(item.Value);
private void Selected(DateOnly value) => OnSelected.InvokeAsync(value);
}
40 changes: 14 additions & 26 deletions src/Inc.TeamAssistant.WebUI/Components/DateSelectorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,23 @@ public DateSelectorFactory(ResourcesManager resources)
_resources = resources ?? throw new ArgumentNullException(nameof(resources));
}

public IReadOnlyCollection<SelectItem<DateOnly>> CreateWeeks()
public IReadOnlyDictionary<string, DateOnly> CreateWeeks()
{
return
[
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_TwoWeeks],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-14).Date)),
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_FourWeeks],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-28).Date)),
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_TwelveWeeks],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-84).Date))
];
return new Dictionary<string, DateOnly>
{
[_resources[Messages.Dashboard_TwoWeeks]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-14).Date),
[_resources[Messages.Dashboard_FourWeeks]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-28).Date),
[_resources[Messages.Dashboard_TwelveWeeks]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddDays(-84).Date)
};
}

public IReadOnlyCollection<SelectItem<DateOnly>> CreateMonths()
public IReadOnlyDictionary<string, DateOnly> CreateMonths()
{
return
[
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_OneMonth],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-1).Date)),
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_ThreeMonths],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-3).Date)),
new SelectItem<DateOnly>(
_resources[Messages.Dashboard_SixMonths],
DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-6).Date))
];
return new Dictionary<string, DateOnly>
{
[_resources[Messages.Dashboard_OneMonth]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-1).Date),
[_resources[Messages.Dashboard_ThreeMonths]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-3).Date),
[_resources[Messages.Dashboard_SixMonths]] = DateOnly.FromDateTime(DateTimeOffset.UtcNow.AddMonths(-6).Date)
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
private readonly PaginationState _pagination = new();

private LoadingState _state = LoadingState.Done();
private IReadOnlyCollection<SelectItem<DateOnly>> DateItems => DateSelectorFactory.CreateMonths();
private IReadOnlyDictionary<string, DateOnly> DateItems => DateSelectorFactory.CreateMonths();
private IReadOnlyCollection<AssessmentHistoryDto> _items = Array.Empty<AssessmentHistoryDto>();
private IQueryable<AssessmentHistoryDto> HistoryItems => _items.AsQueryable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@

private LoadingState _state = LoadingState.Done();
private readonly RandomCoffeeHistoryWidgetFormModel _formModel = new();
private IReadOnlyCollection<SelectItem<DateOnly>> DateItems => _formModel.HistoryItems
.Select(i => new SelectItem<DateOnly>(i.Created.ToString("yy-MM-dd"), i.Created))
.ToArray();

private IReadOnlyDictionary<string, DateOnly> DateItems
=> _formModel.HistoryItems.ToDictionary(i => i.Created.ToString("yy-MM-dd"), i => i.Created);

protected override Task OnParametersSetAsync() => Load();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
private DateOnly? _date;

private LoadingState _state = LoadingState.Done();
private IReadOnlyCollection<SelectItem<DateOnly>> DateItems => DateSelectorFactory.CreateWeeks();
private IReadOnlyDictionary<string, DateOnly> DateItems => DateSelectorFactory.CreateWeeks();
private IReadOnlyCollection<ReviewAverageStatsDto> _items = Array.Empty<ReviewAverageStatsDto>();

protected override async Task OnParametersSetAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
private DateOnly? _date;
private readonly PaginationState _pagination = new() { ItemsPerPage = 8 };
private LoadingState _state = LoadingState.Done();
private IReadOnlyCollection<SelectItem<DateOnly>> DateItems => DateSelectorFactory.CreateWeeks();
private IReadOnlyDictionary<string, DateOnly> DateItems => DateSelectorFactory.CreateWeeks();
private IReadOnlyCollection<TaskForReviewDto> _tasks = Array.Empty<TaskForReviewDto>();
private IQueryable<TaskForReviewDto> Tasks => _tasks.AsQueryable();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
private DateOnly? _date;

private LoadingState _state = LoadingState.Done();
private IReadOnlyCollection<SelectItem<DateOnly>> DateItems => DateSelectorFactory.CreateWeeks();
private IReadOnlyDictionary<string, DateOnly> DateItems => DateSelectorFactory.CreateWeeks();
private readonly ReviewTotalStatsWidgetFormModel _formModel = new();

protected override async Task OnParametersSetAsync()
Expand Down

0 comments on commit 168e906

Please sign in to comment.