custom-form-input.component.ts 2.6 KB

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