Skip to content

Commit

Permalink
Cleanup contact form with URL
Browse files Browse the repository at this point in the history
Parent component still have the responsability to explicitely open the
modal
Rely on data router param to open or not the modal
  • Loading branch information
Chocobozzz committed Jun 15, 2021
1 parent 268ec22 commit 6223e24
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<div class="about-instance-title">
<h1 i18n class="title">About {{ instanceName }}</h1>

<a routerLink="contact" i18n *ngIf="isContactFormEnabled" class="contact-admin">Contact administrator</a>
<a routerLink="/about/contact" i18n *ngIf="isContactFormEnabled" class="contact-admin">Contact administrator</a>
</div>

<div class="instance-badges" *ngIf="categories.length !== 0 || languages.length !== 0">
Expand Down Expand Up @@ -218,4 +218,4 @@ <h2 i18n class="middle-title">STATISTICS</h2>
</div>
</div>

<my-contact-admin-modal></my-contact-admin-modal>
<my-contact-admin-modal #contactAdminModal></my-contact-admin-modal>
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,10 @@
}

.contact-admin {
@include peertube-button;
@include peertube-button-link;
@include orange-button;

height: fit-content;

&:hover,
&:active {
text-decoration: none;
}
}
}

Expand Down Expand Up @@ -58,6 +53,10 @@
font-size: 15px;
}

.short-description {
margin-top: 10px;
}

.short-description .dedicated-to-nsfw {
margin-top: 20px;
font-weight: $font-semibold;
Expand Down
14 changes: 11 additions & 3 deletions client/src/app/+about/about-instance/about-instance.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { ViewportScroller } from '@angular/common'
import { AfterViewChecked, Component, ElementRef, OnInit, ViewChild } from '@angular/core'
import { ActivatedRoute } from '@angular/router'
import { ContactAdminModalComponent } from '@app/+about/about-instance/contact-admin-modal.component'
import { Notifier } from '@app/core'
import { CustomMarkupService } from '@app/shared/shared-custom-markup'
import { InstanceService } from '@app/shared/shared-instance'
import { About, ServerConfig } from '@shared/models'
import { ServerConfig } from '@shared/models'
import { copyToClipboard } from '../../../root-helpers/utils'
import { ResolverData } from './about-instance.resolver'
import { ContactAdminModalComponent } from './contact-admin-modal.component'

@Component({
selector: 'my-about-instance',
Expand All @@ -16,6 +15,7 @@ import { ResolverData } from './about-instance.resolver'
})
export class AboutInstanceComponent implements OnInit, AfterViewChecked {
@ViewChild('descriptionWrapper') descriptionWrapper: ElementRef<HTMLInputElement>
@ViewChild('contactAdminModal', { static: true }) contactAdminModal: ContactAdminModalComponent

shortDescription = ''
descriptionContent: string
Expand Down Expand Up @@ -62,6 +62,14 @@ export class AboutInstanceComponent implements OnInit, AfterViewChecked {
async ngOnInit () {
const { about, languages, categories, serverConfig }: ResolverData = this.route.snapshot.data.instanceData

this.route.data.subscribe(data => {
if (!data?.isContact) return

const prefill = this.route.snapshot.queryParams

this.contactAdminModal.show(prefill)
})

this.serverConfig = serverConfig

this.languages = languages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h1 i18n class="modal-title">Contact {{ instanceName }} administrator</h1>

<div class="modal-body">

<form novalidate [formGroup]="form" (ngSubmit)="sendForm()">
<form *ngIf="isContactFormEnabled()" novalidate [formGroup]="form" (ngSubmit)="sendForm()">
<div class="form-group">
<label i18n for="fromName">Your name</label>
<input
Expand Down Expand Up @@ -53,5 +53,7 @@ <h1 i18n class="modal-title">Contact {{ instanceName }} administrator</h1>
</div>
</form>

<div *ngIf="!isContactFormEnabled()" class="alert alert-error" i18n>The contact form is not enabled on this instance.</div>

</div>
</ng-template>
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
@import 'variables';
@import 'mixins';

.modal-body {
text-align: left;
}

input[type=text] {
@include peertube-input-text(340px);
display: block;

@media screen and (max-width: #{map-get($container-max-widths, sm)}) {
width: 100%;
}
display: block;
}

textarea {
@include peertube-textarea(100%, 200px);
}

@media screen and (max-width: breakpoint(md)) {
.modal-body {
text-align: left;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Component, OnInit, OnDestroy, ViewChild } from '@angular/core'
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router'
import { Subject } from 'rxjs'
import { takeUntil, filter } from 'rxjs/operators'
import { Component, OnInit, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
import { Notifier, ServerService } from '@app/core'
import {
BODY_VALIDATOR,
Expand All @@ -16,30 +14,31 @@ import { NgbModalRef } from '@ng-bootstrap/ng-bootstrap/modal/modal-ref'
import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes'
import { ServerConfig } from '@shared/models'

type Prefill = {
subject?: string
body?: string
}

@Component({
selector: 'my-contact-admin-modal',
templateUrl: './contact-admin-modal.component.html',
styleUrls: [ './contact-admin-modal.component.scss' ]
})
export class ContactAdminModalComponent extends FormReactive implements OnInit, OnDestroy {
export class ContactAdminModalComponent extends FormReactive implements OnInit {
@ViewChild('modal', { static: true }) modal: NgbModal

error: string
destroy = new Subject<any>()

subject: string

private openedModal: NgbModalRef
private serverConfig: ServerConfig

constructor (
protected formValidatorService: FormValidatorService,
private router: Router,
private modalService: NgbModal,
private instanceService: InstanceService,
private serverService: ServerService,
private notifier: Notifier,
private route: ActivatedRoute,
private router: Router
private notifier: Notifier
) {
super()
}
Expand All @@ -48,10 +47,6 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
return this.serverConfig.instance.name
}

get isContactFormEnabled () {
return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
}

ngOnInit () {
this.serverConfig = this.serverService.getTmpConfig()
this.serverService.getConfig()
Expand All @@ -63,46 +58,17 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
subject: SUBJECT_VALIDATOR,
body: BODY_VALIDATOR
})

// Direct access
if (/^\/about\/instance\/contact/.test(this.router.url)) {
this.show()
this.prefillForm()
}

// Router access
this.router.events
.pipe(
takeUntil(this.destroy),
filter(event => event instanceof NavigationEnd)
)
.subscribe((event: NavigationEnd) => {
if (/^\/about\/instance\/contact/.test(event.url)) {
this.show()
this.prefillForm()
}
})
}

ngOnDestroy () {
this.destroy.next()
isContactFormEnabled () {
return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled
}

show () {
// If contactForm not enabled redirect to 404
if (!this.isContactFormEnabled) {
return this.router.navigate([ '/404' ], { state: { type: 'other', obj: { status: 404 } }, skipLocationChange: true })
}

// Open modal
show (prefill: Prefill = {}) {
this.openedModal = this.modalService.open(this.modal, { centered: true, keyboard: false })

// Go back to /about/instance after the modal is closed
this.openedModal.result.then(() => {
this.router.navigateByUrl('/about/instance')
}, () => {
this.router.navigateByUrl('/about/instance')
})
this.openedModal.shown.subscribe(() => this.prefillForm(prefill))
this.openedModal.result.finally(() => this.router.navigateByUrl('/about/instance'))
}

hide () {
Expand Down Expand Up @@ -134,15 +100,13 @@ export class ContactAdminModalComponent extends FormReactive implements OnInit,
)
}

private prefillForm () {
const { subject, body } = this.route.snapshot.queryParams

if (subject) {
this.form.get('subject').setValue(subject)
private prefillForm (prefill: Prefill) {
if (prefill.subject) {
this.form.get('subject').setValue(prefill.subject)
}

if (body) {
this.form.get('body').setValue(body)
if (prefill.body) {
this.form.get('body').setValue(prefill.body)
}
}
}
19 changes: 13 additions & 6 deletions client/src/app/+about/about-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@ const aboutRoutes: Routes = [
},
resolve: {
instanceData: AboutInstanceResolver
}
},
{
path: 'contact',
component: AboutInstanceComponent,
data: {
meta: {
title: $localize`Contact`
},
isContact: true
},
children: [
{
path: 'contact',
component: ContactAdminModalComponent
}
]
resolve: {
instanceData: AboutInstanceResolver
}
},
{
path: 'peertube',
Expand Down

0 comments on commit 6223e24

Please sign in to comment.