Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SFD-127: Add tooltip to calculate button when its disabled #92

Merged
merged 8 commits into from
Jul 2, 2024
47 changes: 35 additions & 12 deletions src/app/carbon-estimator-form/carbon-estimator-form.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
formControlName="headCount"
name="headCount"
type="number"
min="1"
required
mgriffin-scottlogic marked this conversation as resolved.
Show resolved Hide resolved
aria-describedby="headCountError" />
@if (headCount?.invalid && (headCount?.dirty || headCount?.touched)) {
<div class="tce-text-error-red tce-flex tce-gap-1" aria-live="polite" id="headCountError">
Expand Down Expand Up @@ -80,7 +80,7 @@
formControlName="numberOfServers"
name="numberOfServers"
type="number"
min="0" />
required />
</div>

<ng-container *ngTemplateOutlet="locationInput; context: formContext.onPremise"></ng-container>
Expand Down Expand Up @@ -202,7 +202,7 @@
name="monthlyActiveUsers"
type="number"
id="monthlyActiveUsers"
min="1"
required
aria-describedby="monthlyActiveUsersError" />
@if (monthlyActiveUsers?.invalid && (monthlyActiveUsers?.dirty || monthlyActiveUsers?.touched)) {
<div class="tce-text-error-red tce-flex tce-gap-1" aria-live="polite" id="monthlyActiveUsersError">
Expand Down Expand Up @@ -250,15 +250,38 @@
</div>
</div>

<div class="tce-flex tce-gap-4 tce-justify-end">
<button class="tce-button-reset tce-px-3 tce-py-2" type="button" (click)="resetForm()">Reset</button>
<button
class="tce-button-calculate tce-px-3 tce-py-2"
[ngClass]="{ 'tce-opacity-50 tce-cursor-not-allowed': !estimatorForm.valid }"
type="submit"
[attr.aria-disabled]="!estimatorForm.valid">
Calculate
</button>
<div>
<div class="tce-flex tce-gap-4 tce-justify-end">
<button class="tce-button-reset tce-px-3 tce-py-2" type="button" (click)="resetForm()">Reset</button>
<button
class="tce-button-calculate tce-px-3 tce-py-2"
[ngClass]="{ 'tce-opacity-50 tce-cursor-not-allowed': estimatorForm.invalid }"
type="submit"
[attr.aria-disabled]="estimatorForm.invalid"
aria-describedby="calculateDisabledMessage">
Calculate
</button>
</div>
@if (estimatorForm.invalid && (estimatorForm.dirty || estimatorForm.touched)) {
<div
class="tce-text-error-red tce-flex tce-gap-1 tce-justify-end tce-mt-2"
aria-live="polite"
id="calculateDisabledMessage">
<span class="material-icons-outlined">error</span>
<p>
Unable to calculate emissions because the following fields are invalid:
@if (headCount?.invalid) {
<a class="tce-underline" href="#headCount">number of employees</a>&nbsp;
}
@if (numberOfServers?.invalid) {
<a class="tce-underline" href="#numberOfServers">number of servers</a>&nbsp;
}
@if (monthlyActiveUsers?.invalid) {
<a class="tce-underline" href="#monthlyActiveUsers">monthly active users</a>
}
</p>
</div>
}
</div>

<ng-template
Expand Down
10 changes: 7 additions & 3 deletions src/app/carbon-estimator-form/carbon-estimator-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ export class CarbonEstimatorFormComponent implements OnInit {
public ngOnInit() {
this.estimatorForm = this.formBuilder.nonNullable.group({
upstream: this.formBuilder.nonNullable.group({
headCount: [defaultValues.upstream.headCount, Validators.required],
headCount: [defaultValues.upstream.headCount, [Validators.required, Validators.min(1)]],
desktopPercentage: [defaultValues.upstream.desktopPercentage],
employeeLocation: [defaultValues.upstream.employeeLocation],
}),
onPremise: this.formBuilder.nonNullable.group({
estimateServerCount: [defaultValues.onPremise.estimateServerCount],
serverLocation: [defaultValues.onPremise.serverLocation as WorldLocation | 'unknown'],
numberOfServers: [defaultValues.onPremise.numberOfServers, Validators.required],
numberOfServers: [defaultValues.onPremise.numberOfServers, [Validators.required, Validators.min(0)]],
}),
cloud: this.formBuilder.nonNullable.group({
noCloudServices: [false],
Expand All @@ -95,7 +95,7 @@ export class CarbonEstimatorFormComponent implements OnInit {
downstream: this.formBuilder.nonNullable.group({
noDownstream: [false],
customerLocation: [defaultValues.downstream.customerLocation],
monthlyActiveUsers: [defaultValues.downstream.monthlyActiveUsers, Validators.required],
monthlyActiveUsers: [defaultValues.downstream.monthlyActiveUsers, [Validators.required, Validators.min(1)]],
mobilePercentage: [defaultValues.downstream.mobilePercentage],
purposeOfSite: [defaultValues.downstream.purposeOfSite],
}),
Expand Down Expand Up @@ -181,6 +181,10 @@ export class CarbonEstimatorFormComponent implements OnInit {
return this.estimatorForm.get('upstream.headCount');
}

public get numberOfServers() {
return this.estimatorForm.get('onPremise.numberOfServers');
}

public get monthlyActiveUsers() {
return this.estimatorForm.get('downstream.monthlyActiveUsers');
}
Expand Down