From 18476aa845e4c20711c646d4f73cd8fef38807d3 Mon Sep 17 00:00:00 2001 From: Dylan Leard Date: Mon, 8 Jun 2020 16:27:01 -0700 Subject: [PATCH] feat: clean-up ui functions, close modal on save --- .../Products/LinkedProductModal.tsx | 5 +- .../Products/ProductRowItemContainer.tsx | 50 +++++++++---------- .../updateLinkedProductMutation.ts | 6 ++- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/app/containers/Products/LinkedProductModal.tsx b/app/containers/Products/LinkedProductModal.tsx index 09b6192c2e..57c03bc26c 100644 --- a/app/containers/Products/LinkedProductModal.tsx +++ b/app/containers/Products/LinkedProductModal.tsx @@ -136,7 +136,10 @@ export const LinkedProductModalComponent: React.FunctionComponent = ({ > Save - diff --git a/app/containers/Products/ProductRowItemContainer.tsx b/app/containers/Products/ProductRowItemContainer.tsx index 4e7dffd3a8..ce429795ae 100644 --- a/app/containers/Products/ProductRowItemContainer.tsx +++ b/app/containers/Products/ProductRowItemContainer.tsx @@ -17,7 +17,7 @@ import { faShareAlt } from '@fortawesome/free-solid-svg-icons'; import createLinkedProductMutation from 'mutations/linked_product/createLinkedProductMutation'; -// Import updateLinkedProductMutation from 'mutations/linked_product/updateLinkedProductMutation'; +import updateLinkedProductMutation from 'mutations/linked_product/updateLinkedProductMutation'; import InnerModal from './InnerProductBenchmarkModal'; import LinkedProductModal from './LinkedProductModal'; @@ -147,7 +147,7 @@ export const ProductRowItemComponent: React.FunctionComponent = ({ const createLinkedProduct = async (newLink: number) => { const variables = { input: { - LinkedProduct: { + linkedProduct: { productId: product.rowId, linkedProductId: newLink, isDeleted: false @@ -163,26 +163,27 @@ export const ProductRowItemComponent: React.FunctionComponent = ({ console.log(response); }; - // TODO: GET ID OF PRODUCT LINK (add to db function?) - const removeLinkedProduct = async (removeLink: number) => { - console.log(removeLink); - // Const variables = { - // input: { - // id: removeLink.id - // linkedProduct: { - // productId: product.rowId, - // linkedProductId: removeLink, - // isDeleted: true - // } - // } - // }; + const removeLinkedProduct = async (removeLink: { + productRowId: number; + linkId: number; + }) => { + const variables = { + input: { + rowId: removeLink.linkId, + linkedProductPatch: { + productId: product.rowId, + linkedProductId: removeLink.productRowId, + isDeleted: true + } + } + }; - // const response = await updateLinkedProductMutation( - // relay.environment, - // variables - // ); - // handleUpdateProductCount((productCount += 1)); - // console.log(response); + const response = await updateLinkedProductMutation( + relay.environment, + variables + ); + handleUpdateProductCount((productCount += 1)); + console.log(response); }; const linkData = []; @@ -195,7 +196,6 @@ export const ProductRowItemComponent: React.FunctionComponent = ({ }); const saveLinkedProducts = async (newData: IChangeEvent) => { - console.log(newData); const previousLinks = []; const newLinks = []; @@ -208,18 +208,18 @@ export const ProductRowItemComponent: React.FunctionComponent = ({ newLinks.forEach(async (id) => { if (!previousLinks.includes(id)) { - console.log('ADD:', id); const response = await createLinkedProduct(id); console.log(response); } }); + previousLinks.forEach(async (id, index) => { if (!newLinks.includes(id)) { - console.log('REMOVE:', id); - const response = await removeLinkedProduct(newData[index]); + const response = await removeLinkedProduct(linkData[index]); console.log(response); } }); + setLinkProductModalShow(false); }; /** Modals **/ diff --git a/app/mutations/linked_product/updateLinkedProductMutation.ts b/app/mutations/linked_product/updateLinkedProductMutation.ts index 4e0692a90a..88a830c493 100644 --- a/app/mutations/linked_product/updateLinkedProductMutation.ts +++ b/app/mutations/linked_product/updateLinkedProductMutation.ts @@ -7,8 +7,10 @@ import { import BaseMutation from 'mutations/BaseMutation'; const mutation = graphql` - mutation updateLinkedProductMutation($input: UpdateLinkedProductInput!) { - updateLinkedProduct(input: $input) { + mutation updateLinkedProductMutation( + $input: UpdateLinkedProductByRowIdInput! + ) { + updateLinkedProductByRowId(input: $input) { linkedProduct { id productId