Skip to content

Commit

Permalink
Merge branch 'refs/heads/feat/form-changes'
Browse files Browse the repository at this point in the history
  • Loading branch information
Clashsoft committed Aug 17, 2024
2 parents 34dfbbd + aa3a35d commit 127c5c1
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {AuthService} from '../../shared/services/auth.service';
export class CleanEnergyHubComponent implements OnInit {
auditId?: number;
progress?: PercentageCompletion;
typeSchema: SchemaSection[] = [];
typeSchema?: SchemaSection[];
formData?: PreAuditData;

serverUrl = environment.url;
Expand Down
2 changes: 1 addition & 1 deletion src/app/audit/grants/grants.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {switchMap, tap} from 'rxjs';
export class GrantsComponent implements OnInit {
auditId?: number;
progress?: PercentageCompletion;
typeSchema: SchemaSection[] = [];
typeSchema?: SchemaSection[];
formData?: PreAuditData;

constructor(
Expand Down
2 changes: 1 addition & 1 deletion src/app/audit/preaudit-form/preaudit-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {ToastService} from '@mean-stream/ngbx';
export class PreauditFormComponent implements OnInit {
auditId?: number;
progress?: PercentageCompletion;
typeSchema: SchemaSection[] = [];
typeSchema?: SchemaSection[];
formData?: PreAuditData;

constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class EquipmentDetailComponent implements OnInit {
equipmentId?: number;
equipment?: Equipment;
progress?: PercentageCompletion;
typeSchema: SchemaSection[] = [];
typeSchema?: SchemaSection[];
formData?: ZoneData;

constructor(
Expand Down
7 changes: 5 additions & 2 deletions src/app/shared/form/form-choices.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {Pipe, PipeTransform} from '@angular/core';
name: 'formChoices',
})
export class FormChoicesPipe implements PipeTransform {
transform(value: string): [string, string][] {
return value.split(',').map((v) => v.split(':', 2) as [string, string]);
transform(value: string | string[]): string[] {
if (typeof value === 'string') {
return value.split(',').map((v) => v.substring(v.indexOf(':') + 1));
}
return value;
}
}
23 changes: 17 additions & 6 deletions src/app/shared/form/form-element/form-element.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ <h2 class="mb-3">
[attr.aria-describedby]="id + '-help'"
[disabled]="!!element.disabled"
>
@for (entry of element.values | formChoices; track entry[0]) {
<option [value]="entry[1]">
{{ entry[1] }}
@for (entry of (element.values ?? []) | formChoices; track entry) {
<option [value]="entry">
{{ entry }}
</option>
}
</select>
Expand All @@ -36,7 +36,7 @@ <h2 class="mb-3">
(change)="setDirty()"
[attr.aria-describedby]="id + '-help'"
[disabled]="!!element.disabled"
[placeholder]="element.defaultValue"
[placeholder]="element.defaultValue ?? ''"
></textarea>
}
@case ("checkbox") {
Expand Down Expand Up @@ -64,9 +64,20 @@ <h2 class="mb-3">
(change)="setDirty()"
[attr.aria-describedby]="id + '-help'"
[disabled]="!!element.disabled"
[placeholder]="element.defaultValue"
[placeholder]="element.defaultValue ?? ''"
>
}
@case ("radio") {
@for (entry of (element.values ?? []) | formChoices; track entry) {
<div class="form-check">
<input class="form-check-input" type="radio" [name]="id" [id]="id + '-' + $index"
[checked]="formData.data[element.key] === entry" (change)="formData.data[element.key] = entry; setDirty()">
<label class="form-check-label" [for]="id + '-' + $index">
{{ entry }}
</label>
</div>
}
}
@default {
<input
[type]="element.dataType"
Expand All @@ -76,7 +87,7 @@ <h2 class="mb-3">
(change)="setDirty()"
[attr.aria-describedby]="id + '-help'"
[disabled]="!!element.disabled"
[placeholder]="element.defaultValue"
[placeholder]="element.defaultValue ?? ''"
>
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/app/shared/form/form/form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ export class FormComponent implements OnInit {
const initialValue = globalThis.localStorage?.getItem(id);
if (initialValue) {
this.formData.data[element.key] = this.coerce(element, initialValue);
} else if (element.isDateNow) {
this.formData.data[element.key] = new Date();
} else if (element.disabled && element.defaultValue) {
this.formData.data[element.key] = element.defaultValue;
} else {
const currentValue = this.formData.data[element.key];
const defaultValue = element.defaultValue ?? (element.isDateNow ? new Date() : undefined);
if (defaultValue !== undefined && (currentValue === undefined || currentValue === null)) {
this.formData.data[element.key] = defaultValue;
}
}

for (const subElement of element.inputList ?? []) {
Expand Down
10 changes: 7 additions & 3 deletions src/app/shared/model/schema.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ export interface CopySpec {
export interface SchemaElement {
key: string;
hint: 'rq' | string;
type: 'textBox' | 'select' | 'checkbox' | 'textArea' | 'date';
type: 'textBox' | 'select' | 'checkbox' | 'textArea' | 'date' | 'radio';
title: string;
/** for type=select, this is a comma-separated string of colon-separated key value pairs */
values: string;
/**
* An array of options for type=select.
* In older schemas, this is a comma-separated string of colon-separated key value pairs.
* @see FormChoicesPipe
*/
values?: string | string[];
dataType: 'text' | 'number' | 'date';
validations?: SchemaRequirement[];
isDateNow?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/app/zone/zone-form/zone-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class ZoneFormComponent implements OnInit {
auditId?: number;
zoneId?: number;
progress?: PercentageCompletion;
typeSchema: SchemaSection[] = [];
typeSchema?: SchemaSection[];
formData?: ZoneData;

constructor(
Expand Down

0 comments on commit 127c5c1

Please sign in to comment.