0

I'm working on a bot where there are different categories and many sub categories . I am using enum to display and collect inputs. Here I need to display only the subcategories related to the category selected on the previous step how can we achieve this.

here is the code I'm working.

namespace ServiceDesk.Classes
{
    public enum Category
    {
        hardware,
        software,
        email,
        UserAdmin
    };

    public enum Subcategory
    {
        Desktop, KeyBoard, Laptop, Monitor, Mouse, Printer, Scanner, Server, Tablet
    };


    [Serializable]
    public class HardwareQuery
    {
        [Prompt("Choose your {&} ? {||}")]
        public Category? Categ;

        [Prompt("Choose your {&} ? {||}")]
        public Subcategory? SubCateg;

        [Prompt("Please enter {&}")]
        [Pattern(Utility.Phone)]
        public string PhoneNumber { get; set; }

        [Prompt("Please enter {&} ")]
        [Pattern(Utility.Email)]
        public string Email { get; set; }

        [Prompt("Please provide your business need / {&} below")]
        public string Justification { get; set; }

        public static IForm<HardwareQuery> BuildForm()
        {
            return new FormBuilder<HardwareQuery>()
                    .Message("Welcome!")
                    .Build();
        }
    }
}
Alex Ananjev
  • 269
  • 3
  • 16
Jobin Joseph
  • 110
  • 9

1 Answers1

1

You can use the FormBuilder fluid methods to dynamically define your form. You can find docs on doing this here. In a nutshell, what you want to specifically look at is using a FieldReflector which will allow you setup an async delegate to build your dynamic SubCateg list.

Your BuildForm method will end up looking something like this:

 public static IForm<HardwareQuery> BuildForm()
    {
        return new FormBuilder<HardwareQuery>()
              .Message("Welcome!")
              .Field(nameof(Categ))
              .Field(new FieldReflector<HardwareQuery>(nameof(SubCateg))
                  .SetType(null)
                  .SetDefine(async (state, field) =>
                  {
                       //// Define your SubCateg logic here
                      switch (state.Categ)
                      {
                          Category.hardware:
                            break;
                          default:
                              break;
                      }


                      return true;
                  }))
              .Field(nameof(PhoneNumber))
              .Field(nameof(Email))
              .Field(nameof(Justification))
              .Build();
    }
Ed Boykin
  • 831
  • 1
  • 8
  • 17