Skip to content

Commit

Permalink
Merge pull request #2318 from graphcommerce-org/chore/deprecate-row-f…
Browse files Browse the repository at this point in the history
…ield-GC9

Terminate row field on Dynamic Rows
  • Loading branch information
paales authored Aug 14, 2024
2 parents 46c81ad + 886837f commit 046314a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-pumpkins-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphcommerce/hygraph-dynamic-rows": minor
---

Terminate row field on DynamicRows model
13 changes: 0 additions & 13 deletions packages/hygraph-dynamic-rows/graphql/DynamicRow.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ fragment DynamicRow on DynamicRow {
id
}
}

row {
__typename
... on Node {
id
}

...RowColumnOne
...RowQuote
...RowLinks
}

rows {
__typename
... on Node {
id
}

...RowColumnOne
...RowQuote
...RowLinks
Expand Down
17 changes: 7 additions & 10 deletions packages/hygraph-dynamic-rows/lib/hygraphDynamicRows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,19 @@ export async function hygraphDynamicRows(
const content = [...(page?.content ?? [])]

dynamicResult?.data.dynamicRows.forEach((dynamicRow) => {
const { placement, target, rows, row } = dynamicRow
if (!rows && !row) return

const rowsToMerge = rows
if (row && rows.length === 0) rowsToMerge.push(row)
const { placement, target, rows } = dynamicRow
if (!rows) return

if (!target) {
if (placement === 'BEFORE') content.unshift(...rowsToMerge)
else content.push(...rowsToMerge)
if (placement === 'BEFORE') content.unshift(...rows)
else content.push(...rows)
return
}

const targetIdx = content.findIndex((c) => c.id === target.id)
if (placement === 'BEFORE') content.splice(targetIdx, 0, ...rowsToMerge)
if (placement === 'AFTER') content.splice(targetIdx + 1, 0, ...rowsToMerge)
if (placement === 'REPLACE') content.splice(targetIdx, 1, ...rowsToMerge)
if (placement === 'BEFORE') content.splice(targetIdx, 0, ...rows)
if (placement === 'AFTER') content.splice(targetIdx + 1, 0, ...rows)
if (placement === 'REPLACE') content.splice(targetIdx, 1, ...rows)
})

if (!content.length) return pageResult
Expand Down

0 comments on commit 046314a

Please sign in to comment.