diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js index 59cf901c8..c04229cac 100644 --- a/app/assets/javascripts/main.js +++ b/app/assets/javascripts/main.js @@ -2,6 +2,9 @@ $(() => $("time.timeago").timeago()); $(() => GOVUK.stickAtTopWhenScrolling.init()); +var showHideContent = new GOVUK.ShowHideContent(); +showHideContent.init(); + $(() => GOVUK.modules.start()); $(() => $('.error-message').eq(0).parent('label').next('input').trigger('focus')); diff --git a/app/main/forms.py b/app/main/forms.py index 9f968cd6a..746809b8c 100644 --- a/app/main/forms.py +++ b/app/main/forms.py @@ -10,6 +10,7 @@ from notifications_utils.columns import Columns from notifications_utils.formatters import strip_whitespace from notifications_utils.recipients import ( InvalidPhoneError, + normalise_phone_number, validate_phone_number, ) from wtforms import ( @@ -592,13 +593,41 @@ class ProviderForm(StripWhitespaceForm): priority = IntegerField('Priority', [validators.NumberRange(min=1, max=100, message="Must be between 1 and 100")]) -class ServiceContactLinkForm(StripWhitespaceForm): - url = StringField( - "URL", - validators=[DataRequired(message='Can’t be empty'), - URL(message='Must be a valid URL')] +class ServiceContactDetailsForm(StripWhitespaceForm): + contact_details_type = RadioField( + 'Type of contact details', + choices=[ + ('url', 'Link'), + ('email_address', 'Email address'), + ('phone_number', 'Phone number'), + ], + validators=[DataRequired()] ) + url = StringField("URL") + email_address = EmailField("Email address") + phone_number = StringField("Phone number") + + def validate(self): + + if self.contact_details_type.data == 'url': + self.url.validators = [DataRequired(), URL(message='Must be a valid URL')] + + elif self.contact_details_type.data == 'email_address': + self.email_address.validators = [DataRequired(), Length(min=5, max=255), ValidEmail()] + + elif self.contact_details_type.data == 'phone_number': + # we can't use the existing phone number validation functions here since we want to allow landlines + def valid_phone_number(self, num): + try: + normalise_phone_number(num.data) + return True + except InvalidPhoneError: + raise ValidationError('Must be a valid phone number') + self.phone_number.validators = [DataRequired(), Length(min=5, max=20), valid_phone_number] + + return super().validate() + class ServiceReplyToEmailForm(StripWhitespaceForm): email_address = email_address(label='Email reply to address', gov_user=False) diff --git a/app/main/views/service_settings.py b/app/main/views/service_settings.py index 539ea21cd..a180fdcba 100644 --- a/app/main/views/service_settings.py +++ b/app/main/views/service_settings.py @@ -34,7 +34,7 @@ from app.main.forms import ( OrganisationTypeForm, RenameServiceForm, RequestToGoLiveForm, - ServiceContactLinkForm, + ServiceContactDetailsForm, ServiceDataRetentionEditForm, ServiceDataRetentionForm, ServiceEditInboundNumberForm, @@ -337,7 +337,7 @@ def service_switch_can_send_precompiled_letter(service_id): @login_required @user_is_platform_admin def service_switch_can_upload_document(service_id): - form = ServiceContactLinkForm() + form = ServiceContactDetailsForm() # If turning the permission off, or turning it on and the service already has a contact_link, # don't show the form to add the link @@ -346,9 +346,11 @@ def service_switch_can_upload_document(service_id): return redirect(url_for('.service_settings', service_id=service_id)) if form.validate_on_submit(): + contact_type = form.contact_details_type.data + service_api_client.update_service( current_service.id, - contact_link=form.url.data + contact_link=form.data[contact_type] ) switch_service_permissions(service_id, 'upload_document') return redirect(url_for('.service_settings', service_id=service_id)) @@ -397,15 +399,22 @@ def resume_service(service_id): @login_required @user_has_permissions('manage_service') def service_set_contact_link(service_id): - form = ServiceContactLinkForm() + form = ServiceContactDetailsForm() if request.method == 'GET': - form.url.data = current_service.get('contact_link') + contact_details = current_service.get('contact_link') + contact_type = check_contact_details_type(contact_details) + field_to_update = getattr(form, contact_type) + + form.contact_details_type.data = contact_type + field_to_update.data = contact_details if form.validate_on_submit(): + contact_type = form.contact_details_type.data + service_api_client.update_service( current_service.id, - contact_link=form.url.data + contact_link=form.data[contact_type] ) return redirect(url_for('.service_settings', service_id=current_service.id)) @@ -1058,3 +1067,12 @@ def convert_dictionary_to_wtforms_choices_format(dictionary, value, label): return [ (item[value], item[label]) for item in dictionary ] + + +def check_contact_details_type(contact_details): + if contact_details.startswith('http'): + return 'url' + elif '@' in contact_details: + return 'email_address' + else: + return 'phone_number' diff --git a/app/templates/components/radios.html b/app/templates/components/radios.html index fe05defc0..26c9e512f 100644 --- a/app/templates/components/radios.html +++ b/app/templates/components/radios.html @@ -32,8 +32,8 @@ {% endmacro %} -{% macro radio(option, disable=[], option_hints={}) %} -
- When you send users a document to download, you need to include a link to your service + When you send users a document to download, you need to include the contact details for your service on the download page. This is so users can contact you if there’s a problem (for example, if the link to download the document has expired).
-