Skip to content

Commit

Permalink
fix(sanity-plugin): update plugin for updated sync utils
Browse files Browse the repository at this point in the history
  • Loading branch information
good-idea committed Jan 5, 2020
1 parent 2d3961d commit cf97051
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 43 deletions.
2 changes: 1 addition & 1 deletion packages/sanity-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/sanity-plugin/src/Provider/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 10 additions & 23 deletions packages/sanity-plugin/src/ShopifyTool/Sync/Sync.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -40,33 +41,19 @@ class SyncBase extends React.Component<Props, State> {
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
})
}

Expand Down Expand Up @@ -103,7 +90,7 @@ class SyncBase extends React.Component<Props, State> {
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' })
}
Expand Down
15 changes: 14 additions & 1 deletion packages/sanity-plugin/src/sanityDocuments/collection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
22 changes: 21 additions & 1 deletion packages/sanity-plugin/src/sanityDocuments/product.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
}
]
}
]
}
Expand Down
52 changes: 52 additions & 0 deletions packages/sanity-plugin/src/sanityObjects/shopifyProductSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
]
}
]
}
]
}
]
}
]
}

0 comments on commit cf97051

Please sign in to comment.