Browse Source

docs: Improve detail view guide example

Michael Bromley 2 years ago
parent
commit
ea03d523de

+ 53 - 11
docs/docs/guides/extending-the-admin-ui/creating-detail-views/index.md

@@ -33,17 +33,39 @@ import { TypedBaseDetailComponent, LanguageCode, SharedModule } from '@vendure/a
 // This is the TypedDocumentNode & type generated by GraphQL Code Generator
 // This is the TypedDocumentNode & type generated by GraphQL Code Generator
 import { graphql } from '../../gql';
 import { graphql } from '../../gql';
 
 
+export const reviewDetailFragment = graphql(`
+  fragment ReviewDetail on ProductReview {
+    id
+    createdAt
+    updatedAt
+    title
+    rating
+    text
+    authorName
+    productId
+  }
+`);
+
 export const getReviewDetailDocument = graphql(`
 export const getReviewDetailDocument = graphql(`
   query GetReviewDetail($id: ID!) {
   query GetReviewDetail($id: ID!) {
     review(id: $id) {
     review(id: $id) {
-      id
-      createdAt
-      updatedAt
-      title
-      rating
-      text
-      authorName
-      productId
+      ...ReviewDetail
+    }
+  }
+`);
+
+export const createReviewDocument = graphql(`
+  mutation CreateReview($input: CreateProductReviewInput!) {
+    createProductReview(input: $input) {
+      ...ReviewDetail
+    }
+  }
+`);
+
+export const updateReviewDocument = graphql(`
+  mutation UpdateReview($input: UpdateProductReviewInput!) {
+    updateProductReview(input: $input) {
+      ...ReviewDetail
     }
     }
   }
   }
 `);
 `);
@@ -63,7 +85,7 @@ export class ReviewDetailComponent extends TypedBaseDetailComponent<typeof getRe
         authorName: [''],
         authorName: [''],
     });
     });
 
 
-    constructor(private formBuilder: FormBuilder) {
+    constructor(private formBuilder: FormBuilder, private notificationService: NotificationService) {
         super();
         super();
     }
     }
 
 
@@ -76,11 +98,31 @@ export class ReviewDetailComponent extends TypedBaseDetailComponent<typeof getRe
     }
     }
 
 
     create() {
     create() {
-        // Logic to save a Review
+        const { title, rating, authorName } = this.detailForm.value;
+        if (!title || rating == null || !authorName) {
+            return;
+        }
+        this.dataService
+            .mutate(createReviewDocument, {
+                input: { title, rating, authorName },
+            })
+            .subscribe(({ createProductReview }) => {
+                if (createProductReview.id) {
+                    this.notificationService.success('Review created');
+                    this.router.navigate(['extensions', 'reviews', createProductReview.id]);
+                }
+            });
     }
     }
 
 
     update() {
     update() {
-        // Logic to update a Review
+        const { title, rating, authorName } = this.detailForm.value;
+        this.dataService
+            .mutate(updateOrganizationDocument, {
+                input: { id: this.id, title, rating, authorName },
+            })
+            .subscribe(() => {
+                this.notificationService.success('Review updated');
+            });
     }
     }
 
 
     protected setFormValues(entity: NonNullable<ResultOf<typeof getReviewDetailDocument>['review']>, languageCode: LanguageCode): void {
     protected setFormValues(entity: NonNullable<ResultOf<typeof getReviewDetailDocument>['review']>, languageCode: LanguageCode): void {

+ 6 - 0
docs/docs/guides/how-to/codegen/index.md

@@ -167,6 +167,12 @@ const config: CodegenConfig = {
         'apps/marketplace/src/plugins/marketplace/ui/gql/': {
         'apps/marketplace/src/plugins/marketplace/ui/gql/': {
             preset: 'client',
             preset: 'client',
             documents: 'apps/marketplace/src/plugins/marketplace/ui/**/*.ts',
             documents: 'apps/marketplace/src/plugins/marketplace/ui/**/*.ts',
+            // This disables the "fragment masking" feature. Fragment masking
+            // can improve component isolation but introduces some additional
+            // complexity that we will avoid for now.
+            presetConfig: {
+                fragmentMasking: false,
+            },
         },
         },
         // highlight-end
         // highlight-end
         'apps/marketplace/src/plugins/marketplace/gql/generated.ts': {
         'apps/marketplace/src/plugins/marketplace/gql/generated.ts': {