-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from Pinelab-studio/feat/primary-collection
Select Primary Collection via Admin UI
- Loading branch information
Showing
11 changed files
with
213 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@pinelab/vendure-plugin-primary-collection", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Adds a primary collection to all Products by extending vendure's graphql api", | ||
"author": "Surafel Melese Tariku <[email protected]>", | ||
"homepage": "https://pinelab-plugins.com/", | ||
|
13 changes: 13 additions & 0 deletions
13
packages/vendure-plugin-primary-collection/src/api/primary-collection.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Parent, ResolveField, Resolver } from '@nestjs/graphql'; | ||
import { Collection, Ctx, RequestContext, Product } from '@vendure/core'; | ||
|
||
@Resolver('Product') | ||
export class PrimaryCollectionResolver { | ||
@ResolveField() | ||
async primaryCollection( | ||
@Ctx() ctx: RequestContext, | ||
@Parent() product: Product | ||
): Promise<Collection | null> { | ||
return (product.customFields as any).primaryCollection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
packages/vendure-plugin-primary-collection/src/primary-collection.resolver.ts
This file was deleted.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
packages/vendure-plugin-primary-collection/src/ui/select-primary-collection.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Component, ChangeDetectorRef } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { OnInit } from '@angular/core'; | ||
import { | ||
IntCustomFieldConfig, | ||
FormInputComponent, | ||
CollectionFragment, | ||
COLLECTION_FRAGMENT, | ||
} from '@vendure/admin-ui/core'; | ||
import { DataService } from '@vendure/admin-ui/core'; | ||
import { gql } from 'graphql-tag'; | ||
import { ActivatedRoute, Params } from '@angular/router'; | ||
@Component({ | ||
template: ` | ||
<select | ||
[formControl]="formControl" | ||
[vdrDisabled]="readonly" | ||
[compareWith]="compareFn" | ||
> | ||
<option *ngFor="let option of productsCollections" [ngValue]="option"> | ||
{{ option.name }} | ||
</option> | ||
</select> | ||
`, | ||
}) | ||
export class SelectPrimaryCollectionComponent | ||
implements FormInputComponent<IntCustomFieldConfig>, OnInit | ||
{ | ||
readonly!: boolean; | ||
config!: IntCustomFieldConfig; | ||
formControl!: FormControl; | ||
productsCollections!: CollectionFragment[]; | ||
productsCollectionsAreLoading = true; | ||
id!: string; | ||
constructor( | ||
private dataService: DataService, | ||
private cdr: ChangeDetectorRef, | ||
private activatedRoute: ActivatedRoute | ||
) {} | ||
ngOnInit(): void { | ||
console.log(this.formControl.value, 'value'); | ||
this.formControl.parent?.parent?.statusChanges.subscribe((s) => { | ||
if ( | ||
this.formControl.pristine && | ||
!this.formControl.value && | ||
(this.productsCollections.length || this.productsCollectionsAreLoading) | ||
) { | ||
this.formControl.parent?.parent?.markAsPristine(); | ||
} | ||
}); | ||
this.activatedRoute.params.subscribe((params: Params) => { | ||
this.id = params.id; | ||
this.dataService.collection.getCollectionContents(params.id); | ||
this.dataService | ||
.query( | ||
gql` | ||
query ProductsCollection($id: ID) { | ||
product(id: $id) { | ||
collections { | ||
...Collection | ||
} | ||
} | ||
} | ||
${COLLECTION_FRAGMENT} | ||
`, | ||
{ id: params.id } | ||
) | ||
.single$.subscribe((d: any) => { | ||
this.productsCollections = d.product.collections; | ||
this.productsCollectionsAreLoading = false; | ||
this.cdr.markForCheck(); | ||
}); | ||
}); | ||
} | ||
|
||
compareFn(a: any, b: any) { | ||
return a?.id === b?.id; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/vendure-plugin-primary-collection/src/ui/shared.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { | ||
SharedModule, | ||
registerFormInputComponent, | ||
} from '@vendure/admin-ui/core'; | ||
import { SelectPrimaryCollectionComponent } from './select-primary-collection.component'; | ||
|
||
@NgModule({ | ||
imports: [SharedModule], | ||
declarations: [SelectPrimaryCollectionComponent], | ||
providers: [ | ||
registerFormInputComponent( | ||
'select-primary-collection', | ||
SelectPrimaryCollectionComponent | ||
), | ||
], | ||
}) | ||
export class SharedExtensionModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { CustomProductFields } from '@vendure/core/dist/entity/custom-entity-fields'; | ||
import { Collection } from '@vendure/core'; | ||
declare module '@vendure/core/dist/entity/custom-entity-fields' { | ||
interface CustomProductFields { | ||
primaryCollection: Collection; | ||
} | ||
} |