custom-form-input.component.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { NgModule, Component } from '@angular/core';
  2. import { FormControl } from '@angular/forms';
  3. import {
  4. SharedModule,
  5. FormInputComponent,
  6. registerFormInputComponent,
  7. StringCustomFieldConfig,
  8. DataService,
  9. } from '@vendure/admin-ui/core';
  10. import gql from 'graphql-tag';
  11. @Component({
  12. template: `
  13. <div class="login-option-container">
  14. <div *ngFor="let option of defaultLoginOptions" class="login-option">
  15. <input
  16. type="checkbox"
  17. [id]="option"
  18. [name]="option"
  19. [value]="option"
  20. [checked]="checkedOptions.includes(option)"
  21. (change)="onCheckboxChange($event, option)"
  22. />
  23. <label>{{ option }}</label>
  24. </div>
  25. </div>
  26. `,
  27. // styleUrls: ['./module-styles/checkboxes-form-inputs.component.scss'],
  28. })
  29. export class LoginOptionCheckboxes implements FormInputComponent<StringCustomFieldConfig> {
  30. isListInput = true;
  31. readonly = false;
  32. config: StringCustomFieldConfig;
  33. formControl: FormControl;
  34. data: string[] = [];
  35. defaultLoginOptions: string[] = ['sso', 'password', 'magicLink'];
  36. checkedOptions: string[] = [];
  37. constructor(private dataService: DataService) {}
  38. ngOnInit() {
  39. // this.data = JSON.parse(this.formControl.value);
  40. console.log('formControl', this.formControl);
  41. console.log('this.defaultLoginOptions', this.defaultLoginOptions);
  42. this.checkedOptions = [...(this.formControl.value ?? [])];
  43. }
  44. onCheckboxChange(event: any, option: string) {
  45. this.dataService
  46. .query(gql`
  47. query GetCustomerAuthOptions {
  48. customer {
  49. id
  50. customFields {
  51. org {
  52. authOptions
  53. }
  54. }
  55. }
  56. }
  57. `)
  58. .single$.subscribe(data => {
  59. console.log('data', data);
  60. });
  61. if (this.checkedOptions.includes(option)) {
  62. this.checkedOptions = this.checkedOptions.filter(item => item !== option);
  63. } else {
  64. this.checkedOptions.push(option);
  65. }
  66. console.log('this.checkedOptions', this.checkedOptions);
  67. this.formControl.setValue(this.checkedOptions);
  68. this.formControl.markAsDirty();
  69. }
  70. }