1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 06:53:23 +00:00

Can create a new choice for the survey

This commit is contained in:
Pierre HUBERT 2020-05-18 18:43:57 +02:00
parent 5086f45cc1
commit e0d69ab504
2 changed files with 52 additions and 3 deletions

View File

@ -34,7 +34,18 @@ class SurveyHelper {
.isOK;
}
/// Create a new choice in a survey
///
/// Throws in case of failure
static Future<void> createNewChoice(int postID, String newChoice) async =>
await APIRequest.withLogin("surveys/create_new_choice")
.addInt("postID", postID)
.addString("choice", newChoice)
.execWithThrow();
/// Prevent new choices from being created on a survey
///
/// Throws in case of failure
static Future<void> blockNewChoicesCreation(int postID) async =>
await APIRequest.withLogin("surveys/block_new_choices_creation")
.addInt("postID", postID)

View File

@ -44,6 +44,9 @@ class _SurveyWidgetState extends SafeState<SurveyWidget> {
),
_buildUserResponse(),
// Offer the user to create a new choice on the survey, if possible
survey.allowNewChoicesCreation ? _buildNewChoiceLink() : Container(),
// Offer to block the creation of new responses, if possible
survey.canBlockNewChoicesCreation
? _buildBlockNewResponsesLink()
@ -116,9 +119,20 @@ class _SurveyWidgetState extends SafeState<SurveyWidget> {
);
}
Widget _buildBlockNewResponsesLink() => InkWell(
Widget _buildNewChoiceLink() => Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: _createNewChoices,
child: Text(tr("Create a new choice")),
),
);
Widget _buildBlockNewResponsesLink() => Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: _blockNewChoices,
child: Text(tr("Block the creation of new responses")),
),
);
Widget _buildChart() {
@ -146,6 +160,30 @@ class _SurveyWidgetState extends SafeState<SurveyWidget> {
});
}
/// Create a new choice
void _createNewChoices() async {
try {
final newChoice = await askUserString(
context: context,
title: tr("New choice"),
message: tr("Please specify the new choice for the survey"),
defaultValue: "",
hint: tr("New choice..."),
maxLength: 50,
);
if (newChoice == null || newChoice.isEmpty) return;
await SurveyHelper.createNewChoice(survey.postID, newChoice);
// TODO : reload survey
} catch (e, s) {
print("Could not create new survey choice! $e\n$s");
showSimpleSnack(
context, tr("Could not create a new choice for this survey!"));
}
}
/// Block the creation of new choices on this survey
void _blockNewChoices() async {
try {