|
|
@@ -0,0 +1,269 @@
|
|
|
+
|
|
|
+// create-activity-dialog.component.timport { Component, Inject } from '@angular/core';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import {
|
|
|
+ FormBuilder,
|
|
|
+ FormGroup,
|
|
|
+ FormArray,
|
|
|
+ Validators,
|
|
|
+ ReactiveFormsModule,
|
|
|
+} from '@angular/forms';
|
|
|
+import {
|
|
|
+ MatDialogRef,
|
|
|
+ MAT_DIALOG_DATA,
|
|
|
+ MatDialogModule,
|
|
|
+} from '@angular/material/dialog';
|
|
|
+import { MatFormFieldModule } from '@angular/material/form-field';
|
|
|
+import { MatInputModule } from '@angular/material/input';
|
|
|
+import { MatButtonModule } from '@angular/material/button';
|
|
|
+import { MatDatepickerModule } from '@angular/material/datepicker';
|
|
|
+import { MatNativeDateModule } from '@angular/material/core';
|
|
|
+import { MatIconModule } from '@angular/material/icon';
|
|
|
+import { MatSelectModule } from '@angular/material/select';
|
|
|
+import { HttpClient, HttpClientModule } from '@angular/common/http';
|
|
|
+import { webConfig } from '../../config'; // adjust path if needed
|
|
|
+import { MatExpansionModule } from '@angular/material/expansion';
|
|
|
+import { Component, Inject } from '@angular/core';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ selector: 'app-create-activity-dialog',
|
|
|
+ standalone: true,
|
|
|
+ imports: [
|
|
|
+ CommonModule,
|
|
|
+ ReactiveFormsModule,
|
|
|
+ HttpClientModule,
|
|
|
+ MatDialogModule,
|
|
|
+ MatFormFieldModule,
|
|
|
+ MatInputModule,
|
|
|
+ MatButtonModule,
|
|
|
+ MatDatepickerModule,
|
|
|
+ MatNativeDateModule,
|
|
|
+ MatIconModule,
|
|
|
+ MatSelectModule,
|
|
|
+ MatExpansionModule,
|
|
|
+ ],
|
|
|
+ templateUrl: './create-activity-dialog.component.html',
|
|
|
+ styleUrls: ['./create-activity-dialog.component.css'],
|
|
|
+})
|
|
|
+export class CreateActivityDialogComponent {
|
|
|
+ form: FormGroup;
|
|
|
+ saving = false;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private fb: FormBuilder,
|
|
|
+ private http: HttpClient,
|
|
|
+ private dialogRef: MatDialogRef<CreateActivityDialogComponent>,
|
|
|
+ @Inject(MAT_DIALOG_DATA) public data: any
|
|
|
+ ) {
|
|
|
+ this.form = this.fb.group({
|
|
|
+ name: ['', Validators.required],
|
|
|
+ type: ['actual', Validators.required],
|
|
|
+ dateStart: [new Date(), Validators.required],
|
|
|
+ dateEnd: [new Date(), Validators.required],
|
|
|
+ duration: this.fb.group({
|
|
|
+ quantity: [0, Validators.required],
|
|
|
+ uom: ['hours', Validators.required],
|
|
|
+ }),
|
|
|
+ resources: this.fb.array([]),
|
|
|
+ outputs: this.fb.array([]),
|
|
|
+ targets: this.fb.array([]),
|
|
|
+ });
|
|
|
+ }
|
|
|
+ ngOnInit() {
|
|
|
+ if (this.data) {
|
|
|
+ this.form.patchValue({
|
|
|
+ name: this.data.name,
|
|
|
+ type: this.data.type,
|
|
|
+ dateStart: this.data.dateStart,
|
|
|
+ dateEnd: this.data.dateEnd,
|
|
|
+ });
|
|
|
+
|
|
|
+ // ✅ Clear existing arrays just in case
|
|
|
+ this.resources.clear();
|
|
|
+ this.outputs.clear();
|
|
|
+ this.targets.clear();
|
|
|
+
|
|
|
+ // ✅ Populate resources
|
|
|
+ if (this.data.resources && Array.isArray(this.data.resources)) {
|
|
|
+ this.data.resources.forEach((r: any) => {
|
|
|
+ this.resources.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: [r.type, Validators.required],
|
|
|
+ name: [r.name, Validators.required],
|
|
|
+ quantity: [r.value?.quantity || 0, Validators.required],
|
|
|
+ uom: [r.value?.uom || '', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // ✅ Populate outputs
|
|
|
+ if (this.data.outputs && Array.isArray(this.data.outputs)) {
|
|
|
+ this.data.outputs.forEach((o: any) => {
|
|
|
+ this.outputs.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: [o.type, Validators.required],
|
|
|
+ name: [o.name, Validators.required],
|
|
|
+ quantity: [o.value?.quantity || 0, Validators.required],
|
|
|
+ uom: [o.value?.uom || '', Validators.required],
|
|
|
+ weight: [o.weightValue?.weight || 0, Validators.required],
|
|
|
+ weightUom: [o.weightValue?.uom || '', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // ✅ Populate targets
|
|
|
+ if (this.data.targets && Array.isArray(this.data.targets)) {
|
|
|
+ this.data.targets.forEach((t: any) => {
|
|
|
+ this.targets.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: [t.type, Validators.required],
|
|
|
+ name: [t.name, Validators.required],
|
|
|
+ quantity: [t.value?.quantity || 0, Validators.required],
|
|
|
+ uom: [t.value?.uom || '', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ get resources(): FormArray {
|
|
|
+ return this.form.get('resources') as FormArray;
|
|
|
+ }
|
|
|
+ get outputs(): FormArray {
|
|
|
+ return this.form.get('outputs') as FormArray;
|
|
|
+ }
|
|
|
+ get targets(): FormArray {
|
|
|
+ return this.form.get('targets') as FormArray;
|
|
|
+ }
|
|
|
+
|
|
|
+ addResource() {
|
|
|
+ this.resources.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: ['worker', Validators.required],
|
|
|
+ name: ['', Validators.required],
|
|
|
+ quantity: [1, Validators.required],
|
|
|
+ uom: ['hour', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ addOutput() {
|
|
|
+ this.outputs.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: ['ffb harvested', Validators.required],
|
|
|
+ name: ['', Validators.required],
|
|
|
+ quantity: [0, Validators.required],
|
|
|
+ uom: ['bunch', Validators.required],
|
|
|
+ weight: [0, Validators.required],
|
|
|
+ weightUom: ['kg', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ addTarget() {
|
|
|
+ this.targets.push(
|
|
|
+ this.fb.group({
|
|
|
+ type: ['location', Validators.required],
|
|
|
+ name: ['', Validators.required],
|
|
|
+ quantity: [0, Validators.required],
|
|
|
+ uom: ['acres', Validators.required],
|
|
|
+ })
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ removeTarget(i: number) {
|
|
|
+ this.targets.removeAt(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ removeOutput(i: number) {
|
|
|
+ this.outputs.removeAt(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ removeResource(i: number) {
|
|
|
+ this.resources.removeAt(i);
|
|
|
+ }
|
|
|
+
|
|
|
+ removeItem(array: FormArray, index: number) {
|
|
|
+ array.removeAt(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ onSubmit() {
|
|
|
+ if (this.form.invalid) return;
|
|
|
+
|
|
|
+ const formValue = this.form.value;
|
|
|
+
|
|
|
+ const activityData = {
|
|
|
+ name: formValue.name,
|
|
|
+ type: formValue.type,
|
|
|
+ dateStart: new Date(formValue.dateStart).toISOString(),
|
|
|
+ dateEnd: new Date(formValue.dateEnd).toISOString(),
|
|
|
+
|
|
|
+ duration: {
|
|
|
+ value: {
|
|
|
+ quantity: formValue.durationQuantity || 0,
|
|
|
+ uom: formValue.durationUom || 'hours',
|
|
|
+ },
|
|
|
+ },
|
|
|
+
|
|
|
+ resources: formValue.resources.map((r: any) => ({
|
|
|
+ type: r.type,
|
|
|
+ name: r.name,
|
|
|
+ value: {
|
|
|
+ quantity: r.quantity,
|
|
|
+ uom: r.uom,
|
|
|
+ },
|
|
|
+ })),
|
|
|
+
|
|
|
+ outputs: formValue.outputs.map((o: any) => ({
|
|
|
+ type: o.type,
|
|
|
+ name: o.name,
|
|
|
+ value: {
|
|
|
+ quantity: o.quantity,
|
|
|
+ uom: o.uom,
|
|
|
+ },
|
|
|
+ weightValue: {
|
|
|
+ weight: o.weight,
|
|
|
+ uom: o.weightUom,
|
|
|
+ },
|
|
|
+ })),
|
|
|
+
|
|
|
+ targets: formValue.targets.map((t: any) => ({
|
|
|
+ type: t.type,
|
|
|
+ name: t.name,
|
|
|
+ value: {
|
|
|
+ quantity: t.quantity,
|
|
|
+ uom: t.uom,
|
|
|
+ },
|
|
|
+ })),
|
|
|
+ };
|
|
|
+
|
|
|
+ console.log('Final payload sent:', activityData);
|
|
|
+
|
|
|
+ const isEditMode = !!this.data;
|
|
|
+ const url = isEditMode
|
|
|
+ ? `${webConfig.exposedUrl}/api/activity/${this.data._id}`
|
|
|
+ : `${webConfig.exposedUrl}/api/activity`;
|
|
|
+ const httpMethod = isEditMode ? 'put' : 'post';
|
|
|
+
|
|
|
+ this.http[httpMethod](url, activityData).subscribe({
|
|
|
+ next: () => {
|
|
|
+ console.log(isEditMode ? 'Activity updated.' : 'Activity created.');
|
|
|
+ this.dialogRef.close('refresh');
|
|
|
+ },
|
|
|
+ error: (err) => {
|
|
|
+ console.error(
|
|
|
+ isEditMode ? 'Failed to update activity:' : 'Failed to create activity:',
|
|
|
+ err
|
|
|
+ );
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ cancel() {
|
|
|
+ this.dialogRef.close();
|
|
|
+ }
|
|
|
+}
|