diff --git a/lib/helpers/survey_helper.dart b/lib/helpers/survey_helper.dart index 55aecc3..f2fbb13 100644 --- a/lib/helpers/survey_helper.dart +++ b/lib/helpers/survey_helper.dart @@ -34,7 +34,18 @@ class SurveyHelper { .isOK; } + /// Create a new choice in a survey + /// + /// Throws in case of failure + static Future 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 blockNewChoicesCreation(int postID) async => await APIRequest.withLogin("surveys/block_new_choices_creation") .addInt("postID", postID) diff --git a/lib/ui/widgets/survey_widget.dart b/lib/ui/widgets/survey_widget.dart index 6b1c6c2..b299927 100644 --- a/lib/ui/widgets/survey_widget.dart +++ b/lib/ui/widgets/survey_widget.dart @@ -44,6 +44,9 @@ class _SurveyWidgetState extends SafeState { ), _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 { ); } - Widget _buildBlockNewResponsesLink() => InkWell( - onTap: _blockNewChoices, - child: Text(tr("Block the creation of new responses")), + 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 { }); } + /// 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 {