From cf97051cc9553a3c804e54299beb98b6dbdb5a40 Mon Sep 17 00:00:00 2001 From: Joseph Thomas Date: Sun, 5 Jan 2020 15:48:17 -0800 Subject: [PATCH] fix(sanity-plugin): update plugin for updated sync utils --- packages/sanity-plugin/package.json | 2 +- .../sanity-plugin/src/Provider/Provider.tsx | 1 + .../src/ShopifyTool/Sync/Sync.tsx | 33 ++++------ .../src/sanityDocuments/collection.tsx | 15 ++++- .../src/sanityDocuments/product.tsx | 22 ++++++- .../sanityObjects/shopifyCollectionSource.tsx | 60 +++++++++++++------ .../sanityObjects/shopifyProductSource.tsx | 52 ++++++++++++++++ 7 files changed, 142 insertions(+), 43 deletions(-) diff --git a/packages/sanity-plugin/package.json b/packages/sanity-plugin/package.json index 5eb8ba5..c26cbe2 100644 --- a/packages/sanity-plugin/package.json +++ b/packages/sanity-plugin/package.json @@ -32,7 +32,7 @@ "fix:prettier": "prettier \"src/**/*.ts\" --write --loglevel warn", "fix:lint": "eslint --fix ./src/**/*.ts", "linky": "yalc publish", - "linky:watch": "nodemon -w src --delay 250ms -e ts,tsx -x yalc publish --push", + "linky:watch": "nodemon -w src -w node_modules --delay 250ms -e ts,tsx -x yalc publish --push", "test": "run-s build test:lint test:unit", "test:lint": "eslint ./src/**/*.ts && prettier \"src/**/*.ts\" --list-different", "test:unit": "jest", diff --git a/packages/sanity-plugin/src/Provider/Provider.tsx b/packages/sanity-plugin/src/Provider/Provider.tsx index bdfc785..f68438d 100644 --- a/packages/sanity-plugin/src/Provider/Provider.tsx +++ b/packages/sanity-plugin/src/Provider/Provider.tsx @@ -113,6 +113,7 @@ export class Provider extends React.Component< _type: KEYS_TYPE, ...secrets } + // await this.sanityClient.createIfNotExists(doc) await this.sanityClient .patch(KEYS_ID) diff --git a/packages/sanity-plugin/src/ShopifyTool/Sync/Sync.tsx b/packages/sanity-plugin/src/ShopifyTool/Sync/Sync.tsx index 753d082..7f0baa8 100644 --- a/packages/sanity-plugin/src/ShopifyTool/Sync/Sync.tsx +++ b/packages/sanity-plugin/src/ShopifyTool/Sync/Sync.tsx @@ -1,4 +1,5 @@ import { SyncUtils } from '@sane-shopify/sync-utils' +import { Operation } from '@sane-shopify/types' import * as React from 'react' import { ClientContextValue, Provider } from '../../Provider' @@ -40,33 +41,19 @@ class SyncBase extends React.Component { this.setState(initialState) } + _handleProgress = (op: Operation) => { + console.log('progress', op) + } + _syncProducts = async () => { - this.props.syncingClient.syncProducts({ - onFetchedItems: (nodes) => { - this.setState((prevState) => ({ - fetchedProducts: [...prevState.fetchedProducts, ...nodes] - })) - }, - onProgress: (product) => { - this.setState((prevState) => ({ - productsSynced: [...prevState.productsSynced, product] - })) - } + await this.props.syncingClient.syncProducts({ + onProgress: this._handleProgress }) } _syncCollections = async () => { - this.props.syncingClient.syncCollections({ - onFetchedItems: (nodes) => { - this.setState((prevState) => ({ - fetchedCollections: [...prevState.fetchedProducts, ...nodes] - })) - }, - onProgress: (collection) => { - this.setState((prevState) => ({ - collectionsSynced: [...prevState.collectionsSynced, collection] - })) - } + await this.props.syncingClient.syncCollections({ + onProgress: this._handleProgress }) } @@ -103,7 +90,7 @@ class SyncBase extends React.Component { public syncAll = async () => { await this.reset() this.setState({ syncState: 'syncing' as 'syncing' }) - await this._syncProducts() + // await this._syncProducts() await this._syncCollections() this.setState({ syncState: 'complete' as 'complete' }) } diff --git a/packages/sanity-plugin/src/sanityDocuments/collection.tsx b/packages/sanity-plugin/src/sanityDocuments/collection.tsx index 26a1d83..a057946 100644 --- a/packages/sanity-plugin/src/sanityDocuments/collection.tsx +++ b/packages/sanity-plugin/src/sanityDocuments/collection.tsx @@ -60,8 +60,21 @@ export const createCollectionDocument = ({ { title: 'Products', name: 'products', - type: 'products' + type: 'array', + description: 'Synced from Shopify', + readOnly: true, + of: [ + { + type: 'reference', + to: [ + { + type: 'shopifyProduct' + } + ] + } + ] }, + ...additionalFields ], preview: { diff --git a/packages/sanity-plugin/src/sanityDocuments/product.tsx b/packages/sanity-plugin/src/sanityDocuments/product.tsx index 2c08725..99ee1d5 100644 --- a/packages/sanity-plugin/src/sanityDocuments/product.tsx +++ b/packages/sanity-plugin/src/sanityDocuments/product.tsx @@ -2,7 +2,10 @@ import * as React from 'react' import { unwindEdges } from '@good-idea/unwind-edges' import { SanityDocumentConfig } from '../types' -export const createProductDocument = ({ fields, ...rest }: SanityDocumentConfig = {}) => { +export const createProductDocument = ({ + fields, + ...rest +}: SanityDocumentConfig = {}) => { if (rest && rest.name && rest.name !== 'shopifyProduct') throw new Error('The document name for a product must be "shopifyProduct"') if (rest && rest.type && rest.type !== 'document') @@ -39,6 +42,23 @@ export const createProductDocument = ({ fields, ...rest }: SanityDocumentConfig readOnly: true, type: 'shopifyProductSource' }, + { + title: 'Collections', + name: 'collections', + type: 'array', + readOnly: true, + description: 'Synced from Shopify', + of: [ + { + type: 'reference', + to: [ + { + type: 'shopifyCollection' + } + ] + } + ] + }, ...additionalFields ], preview: { diff --git a/packages/sanity-plugin/src/sanityObjects/shopifyCollectionSource.tsx b/packages/sanity-plugin/src/sanityObjects/shopifyCollectionSource.tsx index 2f44667..a5b25c4 100644 --- a/packages/sanity-plugin/src/sanityObjects/shopifyCollectionSource.tsx +++ b/packages/sanity-plugin/src/sanityObjects/shopifyCollectionSource.tsx @@ -28,28 +28,54 @@ export const shopifyCollectionSource = { name: 'image', type: 'shopifySourceImage', hidden: true - } - ] -} - -export const shopifyCollectionProducts = { - title: 'Products', - name: 'products', - type: 'object', - description: - 'Synced from Shopify. Update collection products in the Shopify dashboard.', - readOnly: true, - fields: [ + }, { title: 'Products', name: 'products', - type: 'array', - of: [ + hidden: true, + type: 'object', + fields: [ + { + name: 'pageInfo', + type: 'object', + fields: [ + { + name: 'hasNextPage', + type: 'boolean' + }, + { + name: 'hasPreviousPage', + type: 'boolean' + } + ] + }, { - type: 'reference', - to: [ + name: 'edges', + type: 'array', + of: [ { - type: 'shopifyProduct' + name: 'edge', + type: 'object', + fields: [ + { + name: 'cursor', + type: 'string' + }, + { + name: 'node', + type: 'object', + fields: [ + { + name: 'handle', + type: 'string' + }, + { + name: 'id', + type: 'string' + } + ] + } + ] } ] } diff --git a/packages/sanity-plugin/src/sanityObjects/shopifyProductSource.tsx b/packages/sanity-plugin/src/sanityObjects/shopifyProductSource.tsx index 49834f6..b268bb6 100644 --- a/packages/sanity-plugin/src/sanityObjects/shopifyProductSource.tsx +++ b/packages/sanity-plugin/src/sanityObjects/shopifyProductSource.tsx @@ -101,6 +101,58 @@ export const shopifyProductSource = { name: 'images', type: 'shopifySourceImages', hidden: true + }, + { + title: 'Collections', + name: 'collections', + // hidden: true, + type: 'object', + fields: [ + { + name: 'pageInfo', + type: 'object', + fields: [ + { + name: 'hasNextPage', + type: 'boolean' + }, + { + name: 'hasPreviousPage', + type: 'boolean' + } + ] + }, + { + name: 'edges', + type: 'array', + of: [ + { + name: 'edge', + type: 'object', + fields: [ + { + name: 'cursor', + type: 'string' + }, + { + name: 'node', + type: 'object', + fields: [ + { + name: 'handle', + type: 'string' + }, + { + name: 'id', + type: 'string' + } + ] + } + ] + } + ] + } + ] } ] }