RadzenScheduler OnLoadData Called multiple times

Hello everyone,

I have some problem with this scheduler,

@using Radzen;
@using Modèle;

@inject IConfiguration Configuration
@inject DialogService DialogService


@page "/Permanence"

<RadzenDropDown TValue="Agent" @bind-Value="SelectedAgent" TextProperty="Nom" Data="Agents" Change=@(args => OnChange(args)) />

<RadzenCard>
    <RadzenScheduler @ref="@Scheduler" TItem="RDV" Data="@RDVs" SlotRender="@OnSlotRender" TextProperty="Text"
                     style="height: 650px;" StartProperty="Debut" EndProperty="Fin" LoadData="@OnLoadData"
                     SlotSelect=@OnSlotSelect AppointmentSelect=@OnAppointmentSelect AppointmentRender=@OnAppointmentRender
                     AdditionalSourceCodePages=@(new List<string>() {"AjoutPermanence.razor", "EditRDV.razor"})>
        <RadzenWeekView />
    </RadzenScheduler>
</RadzenCard>

@code
{
    BLL bll = new();

    int IdZone = 2;
    List<RDV> RDVs = new();
    List<Agent> Agents = new();
    List<Permanence> Permanences = new();

    Agent SelectedAgent = new();

    RadzenScheduler<RDV> Scheduler = new();

    protected async override Task OnInitializedAsync()
    {
        bll.Init(Configuration);

        Agents = await bll.GetAgentsByZoneAsync(2);
        SelectedAgent = Agents.First();
        await LoadScheduler();
    }

    async Task OnChange(object args)
    {
        await LoadScheduler();
    }

    protected async Task LoadScheduler()
    {
        Permanences = await bll.GetPermanencesByDateAndAgentAsync(GetFirstDayOfWeek(Scheduler.CurrentDate), GetFirstDayOfWeek(Scheduler.CurrentDate).AddDays(7), SelectedAgent.Id);
        Permanences = Permanences.FindAll(x => x.Bureau.Lieu.Zone.Id == IdZone);
        foreach (Permanence permanence in Permanences)
        {
            permanence.RDVs = await bll.GetRDVsByPermanenceAsync(permanence.Id);
            RDVs.AddRange(permanence.RDVs);
        }
        await Scheduler.Reload();
    }

    async Task OnLoadData(SchedulerLoadDataEventArgs args)
    {
        Console.WriteLine("LoadData");
        Permanences = new();
        List<RDV> tmp = new();
        Permanences = await bll.GetPermanencesByDateAndAgentAsync(args.Start, args.End, SelectedAgent.Id);
        foreach (Permanence permanence in Permanences)
        {
            permanence.RDVs = await bll.GetRDVsByPermanenceAsync(permanence.Id);
            tmp.AddRange(permanence.RDVs);
        }
        RDVs = tmp;
        Console.WriteLine("LoadData FIN");
        await Scheduler.Reload();
    }

    void OnSlotRender(SchedulerSlotRenderEventArgs args)
    {
        if ((args.Start.Hour >= 8  && args.Start.Hour < 12) || (args.Start.Hour >= 14 && args.Start.Hour < 18))
        {
            args.Attributes["style"] = "background: rgba(255,220,40,.2);";
        }
        if ( args.Start.DayOfWeek == DayOfWeek.Saturday || args.Start.DayOfWeek == DayOfWeek.Sunday)
        {
            args.Attributes["style"] = "background: lightgray;";
        }
    }

    async Task OnSlotSelect(SchedulerSlotSelectEventArgs args)
    {

        Permanence permanence = await DialogService.OpenAsync<AjoutPermanence>("Ajouter une permanence", new Dictionary<string, object> { { "idAgent", 2 }, { "idZone", 2 } });

        if (permanence != null)
        {
            RDVs.AddRange(permanence.RDVs);
            await Scheduler.Reload();
        }
    }

    async Task OnAppointmentSelect(SchedulerAppointmentSelectEventArgs<RDV> args)
    {

        await DialogService.OpenAsync<EditRDV>("Edit Appointment", new Dictionary<string, object> { { "parameter", args.Data } });

        await Scheduler.Reload();
    }

    void OnAppointmentRender(SchedulerAppointmentRenderEventArgs<RDV> args)
    {
        if (args.Data.Usager.Id != 0)
        {
            args.Attributes["style"] = "background: green";
        }
        else
        {

        }
        if (args.Data.Permanence.MotifSuspensionPermanence.Id != 0)
        {
            args.Attributes["style"] = "background: gray";
        }
    }

    DateTime GetFirstDayOfWeek(DateTime date)
    {
        int delta = DayOfWeek.Monday - date.DayOfWeek;
        return date.AddDays(delta);
    }

}

OnLoadData Is called a ton of times even when nothing is happening on the screen, and I don't know why.

Sometimes it goes through all the onloadData function sometimes it stops before the second WriteLine.

Do you know what could be happening?

Thanks

Hi @Pierre_Chambon

Looks like an infinite loop.

You have a function OnLoadData that is being called when the data is loaded. At the end of this function, you are reloading the scheduler.

Regards

Paul

2 Likes

Hello!

Thanks for your answer!

The problem is the same if I delete the reload line!

Do you know if it can be due to some other stuff?

Thanks

It would be easier to just debug the code and see the stacktrace. This would tell what is invoking the load event.