Skip to content

Commit

Permalink
feat!: add hooks (#164)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: callbacks onLocaleChange and beforeLocaleChange was replaced by hooks
  • Loading branch information
mercs600 committed Oct 22, 2021
1 parent b22bdf5 commit 0c2b79e
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 40 deletions.
1 change: 1 addition & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = {
title: 'Configuration',
collapsable: false,
children: [
'configuration/hooks',
'configuration/i18n',
'configuration/domains',
'configuration/static-generate',
Expand Down
30 changes: 30 additions & 0 deletions docs/configuration/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Hooks
You can use hooks to extend default behavior or override data.

## Available hooks

### Before context is setup by router middleware

```js
$nuxt.$typo3.hook('context', (context, response) => {})
```

### Before redirect in router middleware
```js
$nuxt.$typo3.hook('redirect', (context, redirectData) => {})
```

### Before locale change
```js
$nuxt.$typo3.hook('beforeLocaleChange', (newLocale, oldLocale) => {})
```

### On locale change
```js
$nuxt.$typo3.hook('localeChange', (newLocale, oldLocale) => {})
```

### Before initialData is setup in the store
```js
$nuxt.$typo3.hook('initialData', (response) => {})
```
20 changes: 12 additions & 8 deletions docs/configuration/i18n.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ Display current language

Switch language by localeCode

#### \$typo3.i18n.onLocaleChange(oldLocale, newLocale)
## React to locale change

Callback on locale change.
You can use `$nuxt.$typo3.hook()` to detect locale change.

#### \$typo3.i18n.beforeLocaleChange(oldLocale, newLocale)

Callback before locale change.
```js
app.$typo3.hook('localeChange', (newLocale, oldLocale) => {})
```

```js
app.$typo3.hook('beforeLocaleChange', (newLocale, oldLocale) => {})
```
## Third-party i18n plugin example

This example shows how to connect third-party i18n plugin to your application. For example we use standard [nuxt-i18n](https://nuxt-community.github.io/nuxt-i18n/) plugin which use [vue-i18n](https://github.com/kazupon/vue-i18n).
Expand Down Expand Up @@ -129,11 +132,12 @@ Create `plugins/i18n.js`
```js
export default ({ app }) => {
app.i18n.locale = app.$typo3.i18n.locale

app.$typo3.i18n.onLocaleChange = (oldLocale, newLocale) => {
app.$typo3.hook('onLocaleChange', (newLocale, oldLocale) => {
app.i18n.locale = newLocale
}
})
}

```
Edit `nuxt.config.js`:
Expand Down
4 changes: 2 additions & 2 deletions example/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ module.exports = {
/*
** Add components/layouts overrides
*/
plugins: ['~/plugins/components', '~/plugins/layouts', '~/plugins/i18n'],
plugins: ['~/plugins/components', '~/plugins/layouts', '~/plugins/i18n', '~/plugins/hooks'],
/*
** Register required modules
*/
Expand All @@ -51,8 +51,8 @@ module.exports = {
],

i18n: {
strategy: 'no_prefix',
localeConfig,
strategy: 'no_prefix', // because route strategy is handled on nuxt-typo3 side
vueI18n: {
fallbackLocale: 'en',
messages: {
Expand Down
6 changes: 6 additions & 0 deletions example/plugins/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default ({ app }) => {
app.$typo3.hook('beforeRedirect', (context, data) => {
data.redirectUrl = '/'
return data
})
}
5 changes: 2 additions & 3 deletions example/plugins/i18n.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export default ({ app }) => {
app.i18n.locale = app.$typo3.i18n.locale

app.$typo3.i18n.onLocaleChange = (oldLocale, newLocale) => {
app.$typo3.hook('onLocaleChange', (newLocale, oldLocale) => {
app.i18n.locale = newLocale
}
})
}
7 changes: 5 additions & 2 deletions lib/lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { getBackendLayout, getFrontendLayout } from '~typo3/lib/layouts'
* @param {Object} response - page response
* @returns {Object} context
*/
function setContext (context, response) {
async function setContext (context, response) {
const { data } = response
const { store } = context
const { store, app } = context

if (data) {
const { appearance } = data
Expand All @@ -28,6 +28,8 @@ function setContext (context, response) {

context.pageContent = data
store.commit(SET_PAGE, data)

await app.$typo3.callHook('context', context, data)
}

return context
Expand Down Expand Up @@ -81,6 +83,7 @@ async function contextMiddleware (context) {

if (data && data.redirectUrl) {
const { redirectUrl, statusCode } = data
await app.$typo3.callHook('redirect', context, data)
return redirect(context, redirectUrl, statusCode)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ function setLocale (
localeCode,
updateInitialData = true
) {
app.$typo3.i18n.beforeLocaleChange(store.state.typo3.locale, localeCode)
app.$typo3.callHook('beforeLocaleChange', localeCode, store.state.typo3.locale)
return new Promise((resolve, reject) => {
if (localeCode) {
if (updateInitialData) {
store
.dispatch('getInitialData', { path: params.pathMatch })
.then((response) => {
app.$typo3.i18n.onLocaleChange(store.state.typo3.locale, localeCode)
app.$typo3.callHook('localeChange', localeCode, store.state.typo3.locale)
store.commit(SET_LOCALE, localeCode)
resolve(response)
})
.catch((error) => {
reject(error)
})
} else {
app.$typo3.i18n.onLocaleChange(store.state.typo3.locale, localeCode)
app.$typo3.callHook('localeChange', localeCode, store.state.typo3.locale)
store.commit(SET_LOCALE, localeCode)
resolve({
localeCode
Expand Down
2 changes: 0 additions & 2 deletions lib/middleware/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ middleware.typo3i18n = function ({ app, store }) {
app.$typo3.i18n.setLocale(localeCode)
}
}

export default function () {}
4 changes: 1 addition & 3 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export default {
layouts: {},
i18n: {
locales: ['en'],
defaultLocale: 'en',
onLocaleChange: () => null,
beforeLocaleChange: () => null
defaultLocale: 'en'
},
registerComponents: true,
registerLayouts: true,
Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import defu from 'defu'
import Hookable from 'hable'
import api from '~typo3/plugins/api'
import i18n from '~typo3/plugins/i18n'
import domains from '~typo3/plugins/domains'
Expand All @@ -10,11 +11,14 @@ export default function (context, inject) {
moduleOptions = defu(runtimeConfig, moduleOptions)
}

const hooks = new Hookable()

const _options = {
api: api(context, moduleOptions),
i18n: i18n(context, moduleOptions),
domains: domains(context, moduleOptions),
options: moduleOptions
options: moduleOptions,
...hooks
}
inject('typo3', _options)
}
5 changes: 2 additions & 3 deletions lib/plugins/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ export default (context, options) => {
getLocaleCodePath: () => getLocaleCodePath(context),
getLocaleByPath: () => getLocaleByPath(context),
setLocale: (localeCode, updateInitialData) =>
setLocale(context, localeCode, updateInitialData),
onLocaleChange: () => null,
beforeLocaleChange: () => null
setLocale(context, localeCode, updateInitialData)

}
}
12 changes: 7 additions & 5 deletions lib/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
/**
* Called during SSR init
*/
async nuxtServerInit ({ commit, dispatch, state }, { app }) {
async nuxtServerInit ({ dispatch }, { app }) {
if (app.$typo3.domains) {
app.$typo3.domains.setDomain(app.$typo3.domains.getDomain())
}
Expand All @@ -18,7 +18,7 @@ export default {
* @return {void}
*/
getInitialData (
{ commit, state },
{ commit },
params = {
path: this.$router.currentRoute.params.pathMatch
}
Expand All @@ -31,9 +31,11 @@ export default {
}
return this.$typo3.api
.getInitialData(params)
.then((response) => {
commit(SET_INITIAL, response.data)
Promise.resolve(response.data)
.then(async (response) => {
const { data } = response
await this.$typo3.callHook('initialData', data)
commit(SET_INITIAL, data)
Promise.resolve(data)
})
.catch(error => Promise.reject(error))
}
Expand Down
8 changes: 0 additions & 8 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export namespace TYPO3 {
locales: string[],
/** default/fallback locale */
defaultLocale: string,
/** register your callback method on locale change */
onLocaleChange(oldLocale: string, newLocale: string): void,
/** register your callback method before locale change */
beforeLocaleChange(oldLocale: string, newLocale: string): void
}

interface Endpoints {
Expand Down Expand Up @@ -74,10 +70,6 @@ export namespace TYPO3 {
getLocaleByPath(): string,
/** Set current locale and get initialData */
setLocale(localeCode: string, updateInitialData: boolean): Promise<void>,
/** Call method on locale change */
onLocaleChange(oldLocale: string, newLocale: string): void,
/** Call method before locale change */
beforeLocaleChange(oldLocale: string, newLocale: string): void
}

interface Domains {
Expand Down

0 comments on commit 0c2b79e

Please sign in to comment.