-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ implement add time record functionality
- Loading branch information
Showing
8 changed files
with
163 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,41 @@ | ||
@using Timely.Models | ||
@using Humanizer | ||
@using Microsoft.EntityFrameworkCore | ||
@using Timely.Services.Data | ||
<MudPaper Elevation="0" Class="rounded-lg bg-[#f3f2fc] p-3"> | ||
<MudStack Row="true"> | ||
<MudPaper Elevation="0" Class="flex flex-col p-0 flex-shrink-0 w-20 h-20 rounded-lg bg-[--mud-palette-primary]"> | ||
<MudStack Spacing="0" Class="m-1 m-auto"> | ||
<MudText Class="text-3xl md:text-4xl font-bold text-white mx-auto">@Shift.ShiftStart.ToString("dd")</MudText> | ||
<MudText Class="text-xl font-medium text-white mx-auto">@Shift.ShiftStart.ToString("MMMM")</MudText> | ||
<MudText Class="text-3xl md:text-4xl font-bold text-white mx-auto">@Shift.Date.ToString("dd")</MudText> | ||
<MudText Class="text-xl font-medium text-white mx-auto">@Shift.Date.ToString("MMMM")</MudText> | ||
</MudStack> | ||
</MudPaper> | ||
<MudStack Spacing="2"> | ||
<MudText Class="text-2xl md:text-4xl font-bold text-[--mud-palette-primary]">@((Shift.ShiftEnd - Shift.ShiftStart).ToString("hh\\:mm\\:ss")) Shift</MudText> | ||
<MudText Class="text-2xl md:text-4xl font-bold text-[--mud-palette-primary]">@((Shift.ShiftEnd.Value - Shift.ShiftStart.Value).ToString("hh\\:mm\\:ss")) Shift</MudText> | ||
Check warning on line 14 in Timely/Components/TimeRecord.razor GitHub Actions / deploy-to-github-pages
|
||
<MudChipSet ReadOnly="true"> | ||
<MudChip Color="Color.Primary" Size="Size.Small" Class="p-2"> | ||
<MudText Class="text-md font-medium mx-auto">@Shift.ShiftStart.ToString("t")</MudText> | ||
<MudText Class="text-md font-medium mx-auto">@((DateTime.Today + Shift.ShiftStart.Value).ToString("t"))</MudText> | ||
</MudChip> | ||
- | ||
<MudChip Color="Color.Primary" Size="Size.Small" Class="p-2"> | ||
<MudText Class="text-md font-medium mx-auto">@Shift.ShiftEnd.ToString("t")</MudText> | ||
<MudText Class="text-md font-medium mx-auto">@((DateTime.Today + Shift.ShiftEnd.Value).ToString("t"))</MudText> | ||
</MudChip> | ||
</MudChipSet> | ||
</MudStack> | ||
<MudButton OnClick="@Delete" StartIcon="@Icons.Material.Rounded.Delete" Color="Color.Primary" Variant="Variant.Text" DisableElevation="true"> | ||
</MudButton> | ||
</MudStack> | ||
</MudPaper> | ||
|
||
@code { | ||
|
||
[Parameter] public Shift Shift { get; set; } | ||
|
||
private async Task Delete() | ||
{ | ||
await using var db = new AppDbContext(); | ||
await db.Shifts.Where(shift => shift.Id == Shift.Id).ExecuteDeleteAsync(); | ||
await db.SaveToCacheAsync(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@using Timely.Models | ||
<MudDialog> | ||
<TitleContent> | ||
<MudText Typo="Typo.h6"> | ||
<MudIcon Icon="@Icons.Material.Filled.AddAlarm" Class="mr-3 mb-n1"/> | ||
Add Record | ||
</MudText> | ||
</TitleContent> | ||
<DialogContent> | ||
<MudDatePicker bind-Date="@_date" Label="Date"></MudDatePicker> | ||
<MudTimePicker AmPm="true" @bind-Time="@Shift.ShiftStart" Label="Shift Start"/> | ||
<MudTimePicker AmPm="true" @bind-Time="@Shift.ShiftEnd" Label="Shift End"/> | ||
</DialogContent> | ||
<DialogActions> | ||
<MudButton OnClick="Cancel">Cancel</MudButton> | ||
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="AddRecord">Add</MudButton> | ||
</DialogActions> | ||
</MudDialog> | ||
|
||
@code { | ||
|
||
private DateTime? _date = DateTime.Today; | ||
|
||
[CascadingParameter] MudDialogInstance MudDialog { get; set; } | ||
[Parameter] public Shift Shift { get; set; } = new(); | ||
|
||
private void Cancel() | ||
{ | ||
MudDialog.Cancel(); | ||
} | ||
|
||
private void AddRecord() | ||
{ | ||
Shift.Date = _date is not null ? DateTime.Today : _date!.Value; | ||
MudDialog.Close(DialogResult.Ok(Shift)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using BlazorDB; | ||
using Microsoft.EntityFrameworkCore; | ||
using Timely.Models; | ||
|
||
namespace Timely.Services.Data; | ||
|
||
public class AppDbContext : BlazorDBContext | ||
{ | ||
public DbSet<Shift> Shifts { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,97 @@ | ||
using Blazored.LocalStorage; | ||
using Microsoft.EntityFrameworkCore; | ||
using Timely.Models; | ||
|
||
namespace Timely.Services.Data; | ||
|
||
public class ShiftManager | ||
{ | ||
private Shift? _currentShift; | ||
private LinkedList<Shift>? _timeRecords; | ||
private ILocalStorageService _localStorage; | ||
|
||
public ShiftManager(ILocalStorageService localStorage) | ||
{ | ||
_localStorage = localStorage; | ||
_timeRecords = new LinkedList<Shift>(); | ||
} | ||
|
||
public async Task GetDataFromLocalStorage() | ||
{ | ||
_currentShift = await _localStorage.GetItemAsync<Shift?>("current-shift"); | ||
|
||
var timeRecords = await _localStorage.GetItemAsync<LinkedList<Shift>?>("time-records"); | ||
_timeRecords = timeRecords ?? new LinkedList<Shift>(Enumerable.Empty<Shift>()); | ||
|
||
await UpdateTimeRecords(); | ||
} | ||
private Shift? _activeShift; | ||
|
||
public async Task UpdateTimeRecords() | ||
public ShiftManager() | ||
{ | ||
await _localStorage.SetItemAsync("time-records", _timeRecords); | ||
using var db = new AppDbContext(); | ||
db.Database.EnsureCreated(); | ||
} | ||
|
||
public async Task AddTimeRecord(Shift shift) | ||
{ | ||
_timeRecords.AddFirst(shift); | ||
await UpdateTimeRecords(); | ||
await GetDataFromLocalStorage(); | ||
await using var db = new AppDbContext(); | ||
await db.Shifts.AddAsync(shift); | ||
await db.SaveToCacheAsync(); | ||
} | ||
|
||
public async Task<LinkedList<Shift>?> GetTimeRecords() | ||
public async Task<IEnumerable<Shift>?> GetTimeRecords() | ||
{ | ||
return await _localStorage.GetItemAsync<LinkedList<Shift>?>("time-records"); | ||
await using var db = new AppDbContext(); | ||
return await db.Shifts.Where(shift => !shift.Active).ToListAsync(); | ||
} | ||
|
||
public async Task ClearTimeRecords() | ||
{ | ||
await _localStorage.RemoveItemAsync("time-records"); | ||
await GetDataFromLocalStorage(); | ||
await using var db = new AppDbContext(); | ||
await db.Shifts.AsQueryable().ExecuteDeleteAsync(); | ||
await db.SaveToCacheAsync(); | ||
} | ||
|
||
public async Task<Shift?> GetCurrentShift() | ||
{ | ||
return await _localStorage.GetItemAsync<Shift>("current-shift"); | ||
if (_activeShift is not null) return _activeShift; | ||
|
||
await using var db = new AppDbContext(); | ||
_activeShift = await db.Shifts.FirstOrDefaultAsync(shift => shift.Active); | ||
|
||
return _activeShift; | ||
} | ||
|
||
public async Task AddShift(TimeSpan start, TimeSpan end, DateTime? date = null) | ||
{ | ||
|
||
var shift = new Shift() | ||
{ | ||
ShiftStart = start, | ||
ShiftEnd = end, | ||
Date = date ?? DateTime.Today | ||
}; | ||
|
||
await using var db = new AppDbContext(); | ||
await db.Shifts.AddAsync(shift); | ||
await db.SaveToCacheAsync(); | ||
|
||
} | ||
|
||
public async Task StartShift() | ||
{ | ||
_currentShift ??= new Shift() | ||
if (_activeShift is not null) return; | ||
|
||
var shift = new Shift() | ||
{ | ||
ShiftStart = DateTime.Now | ||
Date = DateTime.Today, | ||
ShiftStart = DateTime.Now.TimeOfDay, | ||
Active = true | ||
}; | ||
|
||
await _localStorage.SetItemAsync("current-shift",_currentShift); | ||
await using var db = new AppDbContext(); | ||
var entry = await db.Shifts.AddAsync(shift); | ||
await db.SaveToCacheAsync(); | ||
|
||
_activeShift = entry.Entity; | ||
|
||
} | ||
|
||
public async Task EndShift() | ||
{ | ||
_currentShift.ShiftEnd = DateTime.Now; | ||
await GetCurrentShift(); | ||
|
||
await using var db = new AppDbContext(); | ||
if (_activeShift is null) return; | ||
|
||
_activeShift.Active = false; | ||
_activeShift.ShiftEnd = DateTime.Now.TimeOfDay; | ||
|
||
await AddTimeRecord(_currentShift); | ||
db.Shifts.Update(_activeShift); | ||
await db.SaveToCacheAsync(); | ||
|
||
await _localStorage.RemoveItemAsync("current-shift"); | ||
await GetDataFromLocalStorage(); | ||
_activeShift = null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters