@inject QuizEditorContext quizContext @code { [Parameter, EditorRequired] public LocalQuizQuestion Question { get; set; } = default!; [Parameter, EditorRequired] public Action UpdateQuestion { get; set; } = (_) => {}; protected override void OnParametersSet() { if(questionType == string.Empty) questionType = Question.QuestionType; if(text == string.Empty) text = Question.Text; if(answers.Count() == 0) answers = Question.Answers; base.OnParametersSet(); } private string text { get; set; } = string.Empty; private string questionType { get; set; } = string.Empty; private IEnumerable answers { get; set; } = Enumerable.Empty(); private void handleTypeUpdate(string type) { if(quizContext.Quiz != null) { questionType = type; var newQuestion = Question with { QuestionType = questionType }; UpdateQuestion(newQuestion); } } private void addAnswer() { if(quizContext.Quiz != null) { var newAnswer = new LocalQuizQuestionAnswer { Id = Guid.NewGuid().ToString() }; answers = answers.Append(newAnswer); UpdateQuestion(Question with { Answers = answers }); } } private void removeAnswer() { if(quizContext.Quiz != null) { var newAnswer = new LocalQuizQuestionAnswer { Id = Guid.NewGuid().ToString() }; answers = answers.Take(Question.Answers.Count() - 1); UpdateQuestion(Question with { Answers = answers }); } } private void saveAnswer(LocalQuizQuestionAnswer newAnswer) { if(questionType == QuestionType.MULTIPLE_CHOICE && newAnswer.Correct) { answers = answers.Select(a => a.Id == newAnswer.Id ? newAnswer : a with { Correct = false } ).ToArray(); } else { answers = answers.Select(a => a.Id == newAnswer.Id ? newAnswer : a ).ToArray(); } UpdateQuestion(Question with { Answers = answers }); } private void handleTextUpdate(ChangeEventArgs e) { var newText = e.Value?.ToString() ?? ""; UpdateQuestion(Question with { Text = newText }); } }