Skip to content

Commit

Permalink
feat: add Page Blocks docs, fix typos in Block Category docs
Browse files Browse the repository at this point in the history
  • Loading branch information
neatbyte-vnobis committed Jun 22, 2022
1 parent 7b1276a commit 0f5c58d
Showing 1 changed file with 157 additions and 15 deletions.
172 changes: 157 additions & 15 deletions src/pages/docs/page-builder/references/lifecycle-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -952,15 +952,9 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

<Alert type="danger">

Please, be aware that you can change what ever you want on the object before it is stored into the database, so be careful with changing the data.

</Alert>

## Block Categories

### OnBeforeBlockCategoryCreateTopicParams
### OnBeforeBlockCategoryCreate

This event is triggered before new block category is stored into the database.

Expand All @@ -987,7 +981,7 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

### OnAfterBlockCategoryCreateTopicParams
### OnAfterBlockCategoryCreate

This event is triggered after new block category is stored into the database.

Expand All @@ -1007,7 +1001,7 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

### OnBeforeBlockCategoryUpdateTopicParams
### OnBeforeBlockCategoryUpdate

This event is triggered before existing block category is updated and stored.

Expand All @@ -1034,7 +1028,7 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

### OnAfterBlockCategoryUpdateTopicParams
### OnAfterBlockCategoryUpdate

This event is triggered after existing block category is updated and stored.

Expand All @@ -1055,7 +1049,7 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

### OnBeforeBlockCategoryDeleteTopicParams
### OnBeforeBlockCategoryDelete

This event is triggered before block category is deleted from the database.

Expand All @@ -1069,7 +1063,7 @@ This event is triggered before block category is deleted from the database.

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onBeforeBlockCategoryDelete.subscribe(async ({ original, blockCategory }) => {
context.pageBuilder.onBeforeBlockCategoryDelete.subscribe(async ({ blockCategory }) => {
/**
* For example, we do not want to allow certain category with a name "All Advertisement Blocks" to be deleted.
*/
Expand All @@ -1081,7 +1075,7 @@ new ContextPlugin<PbContext>(async (context) => {
})
```

### OnAfterBlockCategoryDeleteTopicParams
### OnAfterBlockCategoryDelete

This event is triggered after block category is deleted from the database.

Expand All @@ -1095,14 +1089,162 @@ This event is triggered after block category is deleted from the database.

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onAfterBlockCategoryDelete.subscribe(async ({ original, blockCategory }) => {
await deleteBlockCategoryFromAnotherSystem({ original, blockCategory })
context.pageBuilder.onAfterBlockCategoryDelete.subscribe(async ({ blockCategory }) => {
await deleteBlockCategoryFromAnotherSystem({ blockCategory })
})
})
```

## Page Blocks

### onBeforePageBlockCreate

This event is triggered before new page block is stored into the database.

#### Event arguments

| Property | Description |
| --------- | ---------------------------------------------- |
| pageBlock | Page Block object which is going to be stored |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onBeforePageBlockCreate.subscribe(async ({ pageBlock }) => {
/**
* For example, all page block names have to be in UPPERCASE.
* You can check name and convert it if needed.
*/
if (pageBlock.name !== pageBlock.name.toUpperCase()) {
pageBlock.name = pageBlock.name.toUpperCase()
}
})
})
```

### onAfterPageBlockCreate

This event is triggered after new page block is stored into the database.

#### Event arguments

| Property | Description |
| --------- | ------------------------------------------- |
| pageBlock | Page Block object which was stored |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onAfterPageBlockCreate.subscribe(async ({ pageBlock }) => {
await storePageBlockToAnotherSystem({ pageBlock })
})
})
```

### onBeforePageBlockUpdate

This event is triggered before existing page block is updated and stored.

#### Event arguments

| Property | Description |
| --------- | ------------------------------------------------------ |
| original | Page Block object which was received from the database |
| pageBlock | Page Block object which is going to be stored |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onBeforePageBlockUpdate.subscribe(async ({ original, pageBlock }) => {
/**
* For example, you do not want to allow block category changes.
*/
if (original.blockCategory === pageBlock.blockCategory) {
return
}
throw new Error(`You are not allowed to change the page block category.`)
})
})
```

### onAfterPageBlockUpdate

This event is triggered after existing page block is updated and stored.

#### Event arguments

| Property | Description |
| --------- | ------------------------------------------------------ |
| original | Page Block object which was received from the database |
| pageBlock | Page Block object which is going to be stored |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onAfterPageBlockUpdate.subscribe(async ({ original, pageBlock }) => {
await storePageBlockToAnotherSystem({ original, pageBlock })
})
})
```

### onBeforePageBlockDelete

This event is triggered before page block is deleted from the database.

#### Event arguments

| Property | Description |
| --------- | ---------------------------------------------- |
| pageBlock | Page Block object which is going to be deleted |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onBeforePageBlockDelete.subscribe(async ({ pageBlock }) => {
/**
* For example, we do not want to allow certain block with a name "Heading" to be deleted.
*/
if (pageBlock.name !== "Heading") {
return
}
throw new Error(`You are not allowed to delete a page block with name "Heading".`)
})
})
```

### onAfterPageBlockDelete

This event is triggered after page block is deleted from the database.

#### Event arguments

| Property | Description |
| --------- | ----------------------------------- |
| pageBlock | Page Block object which was deleted |

#### How to subscribe to this event?

```typescript
new ContextPlugin<PbContext>(async (context) => {
context.pageBuilder.onAfterPageBlockDelete.subscribe(async ({ pageBlock }) => {
await deletePageBlockFromAnotherSystem({ pageBlock })
})
})
```

## Registering Lifecycle Event Subscriptions

<Alert type="danger">

Please, be aware that you can change what ever you want on the object before it is stored into the database, so be careful with changing the data.

</Alert>

For the subscriptions (your code) to be run, you must register it in the `createHandler` in the `api/code/graphql/src/index.ts` file.

```typescript api/code/graphql/src/index.ts
Expand Down

0 comments on commit 0f5c58d

Please sign in to comment.