Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.0 kB
2
Indexable
Never
@page "/tasks"
@rendermode InteractiveAuto
@inject ITaskManagement TaskManagerRepo
@inject NavigationManager NavManager
@attribute [StreamRendering]
<PageTitle>Task Manager</PageTitle>

<h1>Task Manager</h1>
<DxButton Click="@AddingATask" Text="+Add"/>
<DxButton Click="@UpdateTaskStatus" Text="Update"/>
@if (TaskLists == null)
{
    <p><em>Loading...</em></p>
}
else
if(TaskLists.Count == 0)
{
    <p>No Items Found</p>  
}
else
{
    <DxGrid Data="@TaskLists" 
        ColumnResizeMode="GridColumnResizeMode.NextColumn" 
        TextWrapEnabled="false" 
        CustomizeElement="Grid_CustomizeElement"
        ShowSearchBox="true" 
        FilterMenuButtonDisplayMode="GridFilterMenuButtonDisplayMode.Always" >
        <Columns>
            <DxGridDataColumn FieldName="@nameof(TaskList.TaskTitle)" Caption="TITLE"/>
            <DxGridDataColumn FieldName="@nameof(TaskList.TaskDescription)" Caption="DESCRIPTION"/>
            <DxGridDataColumn FieldName="@nameof(TaskList.StartDate)" Caption="START DATE"/>
            <DxGridDataColumn FieldName="@nameof(TaskList.EndDate)" Caption="END DATE" />
            <DxGridDataColumn FieldName="@nameof(TaskList.StatusOfTask)" Caption="STATUS">
                <CellDisplayTemplate>
                    @GetStatus(context.Value)
                </CellDisplayTemplate>
                </DxGridDataColumn>
            <DxGridDataColumn FilterMenuButtonDisplayMode="GridFilterMenuButtonDisplayMode.Never" FieldName="@nameof(TaskList.TaskId)" Caption="BUTTONS" Width="280">
                <CellDisplayTemplate>
                    <DxButton Text="Edit" Click="@(()=>EditingTask((int)context.Value))" RenderStyle="ButtonRenderStyle.None"/>
                    @if((((uint)(context.DataItem as TaskList).TaskStatusCode)) !=1)
                    {
                        <DxButton Text="DONE" Click="@(()=>MarkAsCompleted((int)context.Value))" RenderStyle="ButtonRenderStyle.Primary" />
                    }
                    else
                    {
                        <DxButton Text="DONE" RenderStyle="ButtonRenderStyle.Success" Enabled="false" />
                    }
                    <DxButton Text="Delete" RenderStyle="ButtonRenderStyle.None" Click="@(()=>DeletingTask((int)context.Value))"/>                       
                </CellDisplayTemplate>
            </DxGridDataColumn>
        </Columns>
    </DxGrid>   
}
@code 
{
    private List<TaskList> TaskLists = new List<TaskList>();  
    private string searchQuery;
    protected override async Task OnInitializedAsync()
    {
        await GetAll();
    }
    private async Task GetAll()
    {
        TaskLists = await TaskManagerRepo.GetAllTask();
    }
    private async void AddingATask()
    {
        NavManager.NavigateTo("/add-task");
    }

    private async Task EditingTask(int ID)
    {
        NavManager.NavigateTo($"/edit-delete/{ID}");

    }
    private async Task Search()
    {
        if (!string.IsNullOrWhiteSpace(searchQuery))
        {
            try
            {
                TaskLists = await TaskManagerRepo.SearchTask(searchQuery);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error searching tasks: {ex.Message}");
            }
        }
        else
        {
            await GetAll();
        }
    }
    private async Task UpdateTaskStatus()
    {
        try
        {
            await TaskManagerRepo.PatchTaskStatus();
            await GetAll();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    private async Task MarkAsCompleted(int id)
    {
        try
        {
            await TaskManagerRepo.TaskDone(id);
            await GetAll();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    private string GetStatus(object dataItem)
    {
        return ((StatusOfTask)dataItem).Task_Status;
    }
    void Grid_CustomizeElement(GridCustomizeElementEventArgs e)
    {
        if(e.ElementType==GridElementType.DataRow)
        {
            var DateNow = DateTime.UtcNow;
            var endingDate = Convert.ToDateTime(e.Grid.GetRowValue(e.VisibleIndex,nameof(TaskList.EndDate)));
            var TaskCode = Convert.ToInt32(e.Grid.GetRowValue(e.VisibleIndex,nameof(TaskList.TaskStatusCode)));
            if(endingDate < DateNow && TaskCode != 1)
            {
                e.Style = "color:#207A33; background-color:grey";
            }
            if(TaskCode ==1)
            {
                e.Style = "color:#207A33; background-color:aqua";
            }
        }
    }
    private async Task DeletingTask(int id)
    {
        var result = await TaskManagerRepo.DeleteTask(id);
        if(result)
        {
            NavManager.NavigateTo("/tasks",forceLoad:true);
        }
    }
}
Leave a Comment