Skip displaying form fields based on user confirmation

妖精的绣舞 提交于 2019-12-13 20:07:58

问题


I have some 10 properties in a class and based on those properties I have a form that asks for user input. I want to know if there's any mechanism that after initial 4-5 questions I ask user if he/she wants to ans more and if reply is yes then next set of fields/questions are asked.

I tried doing it with SetDefine but issue with SetDefine is that its called with each field so it asks the user to confirm with each fiels but I want it to only confirm with 1st optional field and based on the answer either skip all or get all.

        public async Task StartAsync(IDialogContext context)
    {
        await context.PostAsync($"Welcome to the Order helper!");

        var orderFormDialog = FormDialog.FromForm(BuildOrderAdvanceStepSearchForm, FormOptions.PromptInStart);

        context.Call(orderFormDialog, ResumeAfterOrdersFormDialog);
    }

    private IForm<OrderSearchQuery> BuildOrderAdvanceStepSearchForm()
    {
        return new FormBuilder<OrderSearchQuery>()
            .Field(nameof(OrderSearchQuery.ItemNumber))
            .Field(nameof(OrderSearchQuery.Draft))
            .Field(nameof(OrderSearchQuery.Dispatched))
            .Field(nameof(OrderSearchQuery.InTransit))
            .Field(nameof(OrderSearchQuery.Delivered))
            //.Confirm("Do you want to search with more options?.")
            //.Field(nameof(OrderSearchQuery.AddField1))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField1))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField2))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField3))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField4))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Field(new FieldReflector<OrderSearchQuery>(nameof(OrderSearchQuery.AddField5))
                .SetDefine(OrderAdvanceStepConfirmation))
            .Build();
    }

    private static async Task<bool> OrderAdvanceStepConfirmation(OrderSearchQuery state, Field<OrderSearchQuery> field)
    {
        field.SetPrompt(new PromptAttribute($"Do you want to search with more options?."));
        return true;
    }

    private async Task ResumeAfterordersFormDialog(IDialogContext context, IAwaitable<OrderSearchQuery> result)
    {
        try
        {
            var searchQuery = await result;

            await context.PostAsync($"Ok. Searching for orders...");

            var count = 100;

            if (count > 1)
            {
                await context.PostAsync($"I found total of 100 orders");

                await context.PostAsync($"To get order details, you will need to provide more info...");

            }
            else
            {
                await context.PostAsync($"I found the order you were looking for...");

                await context.PostAsync($"Now I can provide you information related to Consumer Package, Multi-Pack, Shelf Tray & Unit Load for this order.");
            }

        }
        catch (FormCanceledException ex)
        {
            string reply;

            if (ex.InnerException == null)
            {
                reply = "You have canceled the operation. Quitting from the order Search";
            }
            else
            {
                reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
            }

            await context.PostAsync(reply);
        }
        finally
        {
            //context.Done<object>(null);
        }
    }

回答1:


I want it to only confirm with 1st optional field and based on the answer either skip all or get all.

You can use SetNext of FieldReflector.

For example create a enum for confirmation for example like this:

public enum Confirmation
{
    Yes,
    No
}

public Confirmation? Corfirm { get; set; }

And then you can build the Form like this:

return new FormBuilder<OrderSearchQuery>()
    .Field(nameof(ItemNumber))
    .Field(nameof(Draft))
    .Field(new FieldReflector<OrderSearchQuery>(nameof(Corfirm))
           .SetNext((value, state) =>
           {
               var selection = (Confirmation)value;
               if (selection == Confirmation.Yes)
               {
                   //go to step 1
                   return new NextStep();
               }
               else
               {
                   //skip the following steps
                   state.Stpe1 = null;
                   state.Step2 = null;
                   return new NextStep(StepDirection.Complete);
               }
           })
    )
    .Field(nameof(Stpe1))
    .Field(nameof(Step2)).Build();


来源:https://stackoverflow.com/questions/47760851/skip-displaying-form-fields-based-on-user-confirmation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!