-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from davishoang96/save-questions
Save questions and anwers of the selected quiz
- Loading branch information
Showing
11 changed files
with
573 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
@page "/add-question/{quizId:int}" | ||
@using QuizApp.Api | ||
@using QuizApp.Common.DTO | ||
@using System.Collections.ObjectModel | ||
@inject IQuizApiClient apiClient | ||
@inject DialogService DialogService | ||
@inject NotificationService NotificationService | ||
@inject NavigationManager NavigationManager | ||
|
||
<h3> | ||
Add Questions & Answers | ||
</h3> | ||
|
||
<RadzenStack AlignItems="AlignItems.Start" JustifyContent="JustifyContent.Left"> | ||
|
||
<RadzenButton Click="AddNewQuestion">Add question</RadzenButton> | ||
|
||
<RadzenDataGrid ColumnWidth="200px" AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@Questions" TItem="QuestionDTO"> | ||
<Columns> | ||
<RadzenDataGridColumn Property="Title" Title="Question" Width="300px" /> | ||
<RadzenDataGridColumn Property="Answers" Title="Answers"> | ||
<Template Context="data"> | ||
<ul> | ||
@foreach (var answer in data.Answers) | ||
{ | ||
<li> | ||
@answer.Text | ||
<span class="badge bg-@(answer.IsCorrect ? "success" : "secondary")"> | ||
@(answer.IsCorrect ? "Correct" : "Incorrect") | ||
</span> | ||
</li> | ||
} | ||
</ul> | ||
</Template> | ||
</RadzenDataGridColumn> | ||
</Columns> | ||
</RadzenDataGrid> | ||
|
||
<RadzenButton Click="SaveQuestion">Save</RadzenButton> | ||
|
||
</RadzenStack> | ||
|
||
@code { | ||
[Parameter] public int QuizId { get; set; } | ||
|
||
private ObservableCollection<QuestionDTO> Questions = new ObservableCollection<QuestionDTO>(); | ||
|
||
private async Task SaveQuestion() | ||
{ | ||
var result = await apiClient.SaveOrUpdateQuestionAsync(new Common.Request.SaveOrUpdateQuestionRequest | ||
{ | ||
QuizId = QuizId, | ||
QuestionDTOs = Questions, | ||
}); | ||
|
||
if(result > 0) | ||
{ | ||
NotificationService.Notify(NotificationSeverity.Success, "Save questions successfully"); | ||
} | ||
} | ||
|
||
private async Task AddNewQuestion() | ||
{ | ||
DialogService.OnClose += AddNewQuestionClosed; | ||
await DialogService.OpenAsync<AddQuestionDialog>("Add New Question"); | ||
} | ||
|
||
private void AddNewQuestionClosed(dynamic obj) | ||
{ | ||
var questions = obj as QuestionDTO; | ||
if (questions is not null) | ||
{ | ||
Questions.Add(questions); | ||
StateHasChanged(); | ||
} | ||
} | ||
|
||
protected override async Task OnInitializedAsync() | ||
{ | ||
try | ||
{ | ||
var questionDTOs = await apiClient.GetQuestionsByQuizIdEndpointAsync(QuizId); | ||
Questions = new ObservableCollection<QuestionDTO>(questionDTOs); | ||
} | ||
catch(Exception ex) | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@using QuizApp.Common.DTO | ||
@using System.Collections.ObjectModel | ||
@inject DialogService dialogService | ||
|
||
<RadzenStack> | ||
<RadzenTextArea Rows="1" @bind-Value="@TheQuestion.Title" Placeholder="Title"></RadzenTextArea> | ||
<RadzenDataGrid AllowFiltering="false" AllowPaging="false" AllowSorting="false" Data="@Answers" TItem="AnswerDTO"> | ||
<Columns> | ||
<RadzenDataGridColumn Width="70%" Property="Text" Title="Answer(s)"> | ||
<Template Context="data"> | ||
<RadzenTextBox Style="width: 100%" @bind-Value="data.Text" /> | ||
</Template> | ||
</RadzenDataGridColumn> | ||
<RadzenDataGridColumn TextAlign="TextAlign.Center" Width="15%" Property="IsCorrect" Title="Correct"> | ||
<Template Context="data"> | ||
<RadzenCheckBox @bind-Value="data.IsCorrect" /> | ||
</Template> | ||
</RadzenDataGridColumn> | ||
<RadzenDataGridColumn Width="15%" TextAlign="TextAlign.Center"> | ||
<Template Context="data"> | ||
<RadzenButton Icon="delete" ButtonStyle="ButtonStyle.Danger" Click="(() => RemoveAnswer(data))" /> | ||
</Template> | ||
</RadzenDataGridColumn> | ||
</Columns> | ||
</RadzenDataGrid> | ||
<RadzenButton Click="AddAnswer">Add Answer</RadzenButton> | ||
<RadzenButton Click="Save">Save</RadzenButton> | ||
</RadzenStack> | ||
|
||
@code { | ||
private QuestionDTO TheQuestion = new QuestionDTO(); | ||
private ObservableCollection<AnswerDTO> Answers { get; set; } = new ObservableCollection<AnswerDTO>(); | ||
|
||
private async Task AddAnswer() | ||
{ | ||
Answers.Add(new AnswerDTO { Text = string.Empty, IsCorrect = false }); | ||
} | ||
|
||
private void RemoveAnswer(AnswerDTO answer) | ||
{ | ||
Answers.Remove(answer); | ||
} | ||
|
||
private void Save() | ||
{ | ||
TheQuestion.Answers = Answers; | ||
dialogService.Close(TheQuestion); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace QuizApp.Common.Request; | ||
|
||
public class GetQuestionByQuizIdRequest | ||
{ | ||
public int QuizId { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using QuizApp.Common.DTO; | ||
|
||
namespace QuizApp.Common.Request; | ||
|
||
public class SaveOrUpdateQuestionRequest | ||
{ | ||
public int QuizId { get; set; } | ||
public int QuestionId { get; set; } | ||
public IEnumerable<QuestionDTO> QuestionDTOs { get; set; } | ||
} |
Oops, something went wrong.