-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make preview-button configurable from strapi dashboard
in the strapi-dashboard `/config/plugins.ts` ```js 'preview-button': { enabled: true, config: { domain: env('STRAPI_FRONTEND_URL'), token: env('PREVIEW_SECRET_TOKEN'), slug: 'products', }, },
- Loading branch information
1 parent
6ce976e
commit 924faa7
Showing
21 changed files
with
218 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,37 @@ | ||
# Strapi plugin preview-button | ||
|
||
A quick description of preview-button. | ||
## Usage | ||
|
||
Create plugins file or update the existing file `./config/plugins.ts` or `.js` | ||
|
||
**Typescript**: | ||
|
||
```ts | ||
export default ({ env }) => ({ | ||
'preview-button': { | ||
enabled: true, | ||
config: { | ||
domain: env('STRAPI_FRONTEND_URL'), | ||
token: env('PREVIEW_SECRET_TOKEN'), | ||
slug: 'products', | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
**Javascript**: | ||
|
||
```js | ||
module.exports = ({ env }) => ({ | ||
'preview-button': { | ||
enabled: true, | ||
config: { | ||
domain: env('STRAPI_FRONTEND_URL'), | ||
token: env('PREVIEW_SECRET_TOKEN'), | ||
slug: 'products', | ||
}, | ||
}, | ||
}); | ||
``` | ||
|
||
After the plugin integration, you have to build the Strapi dashboard by using `strapi build && strapi develop` or you can use the `strapi develop --watch-admin` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { pluginId } from './utils'; | ||
|
||
export const RESOLVE_CONFIG = `${pluginId}/resolve-config`; |
50 changes: 50 additions & 0 deletions
50
packages/preview-button/admin/src/hooks/use-plugin-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { useSelector, useDispatch } from 'react-redux'; | ||
import { request, useNotification } from '@strapi/helper-plugin'; | ||
|
||
import { RESOLVE_CONFIG } from '../constants'; | ||
import { pluginId } from '../utils'; | ||
|
||
const usePluginConfig = () => { | ||
const dispatch = useDispatch(); | ||
const toggleNotification = useNotification(); | ||
const ctx = useSelector((state) => state[`${pluginId}_config`]); | ||
|
||
useEffect(() => { | ||
// Do nothing if we have already loaded the config data. | ||
if (!ctx?.isLoading && !!ctx?.config) { | ||
return; | ||
} | ||
|
||
const abortController = new AbortController(); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const endpoint = `/${pluginId}/config`; | ||
const data = await request(endpoint, { | ||
method: 'GET', | ||
signal: abortController.signal, | ||
}); | ||
return data ?? {}; | ||
} catch (err) { | ||
console.error(err); | ||
if (!abortController.signal.aborted) { | ||
toggleNotification({ | ||
type: 'warning', | ||
message: { id: 'notification.error' }, | ||
}); | ||
|
||
return err; | ||
} | ||
} | ||
}; | ||
fetchData().then((data) => dispatch({ type: RESOLVE_CONFIG, data })); | ||
|
||
return () => abortController.abort(); | ||
}, [dispatch, toggleNotification]); | ||
|
||
return { config: ctx?.config, isLoading: ctx?.isLoading }; | ||
}; | ||
|
||
export default usePluginConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import produce from 'immer'; | ||
|
||
import { RESOLVE_CONFIG } from '../constants'; | ||
|
||
const initialState = { | ||
isLoading: true, | ||
config: {}, | ||
}; | ||
|
||
const configReducer = produce((state = initialState, action) => { | ||
switch (action.type) { | ||
case RESOLVE_CONFIG: { | ||
state.isLoading = false; | ||
state.config = action.data; | ||
break; | ||
} | ||
|
||
default: | ||
return state; | ||
} | ||
|
||
return state; | ||
}); | ||
|
||
export default configReducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { pluginId } from '../utils'; | ||
import config from './config'; | ||
|
||
const reducers = { | ||
[`${pluginId}_config`]: config, | ||
}; | ||
|
||
export default reducers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as pluginId } from './plugin-id'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import pluginPkg from '../../../package.json'; | ||
|
||
export default pluginPkg.strapi.name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
'use strict'; | ||
|
||
const { ValidationError } = require('@strapi/utils').errors; | ||
|
||
module.exports = { | ||
default: {}, | ||
validator() {}, | ||
validator: (config) => { | ||
if (!config) { | ||
return; | ||
} | ||
if (!config?.domain) { | ||
throw new ValidationError('Missing domain prop.'); | ||
} | ||
if (!config?.token) { | ||
throw new ValidationError('Missing token prop.'); | ||
} | ||
if (!config?.slug) { | ||
throw new ValidationError('Missing slug prop.'); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict'; | ||
|
||
const myController = require('./my-controller'); | ||
const previewButton = require('./preview-button'); | ||
|
||
module.exports = { | ||
myController, | ||
'preview-button': previewButton, | ||
}; |
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
packages/preview-button/server/controllers/preview-button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
const { getService } = require('../util'); | ||
|
||
module.exports = { | ||
async config(ctx) { | ||
try { | ||
const config = await getService('plugin').getConfig(); | ||
ctx.body = config; | ||
} catch (error) { | ||
ctx.badRequest('Something went wrong with the Preview button config'); | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
type: 'admin', | ||
routes: [ | ||
{ | ||
method: 'GET', | ||
path: '/config', | ||
handler: 'preview-button.config', | ||
config: { | ||
policies: ['admin::isAuthenticatedAdmin'], | ||
}, | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,7 @@ | ||
module.exports = [ | ||
{ | ||
method: 'GET', | ||
path: '/', | ||
handler: 'myController.index', | ||
config: { | ||
policies: [], | ||
}, | ||
}, | ||
]; | ||
'use strict'; | ||
|
||
const admin = require('./admin-api'); | ||
|
||
module.exports = { | ||
'admin-api': admin, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
'use strict'; | ||
|
||
const myService = require('./my-service'); | ||
const plugin = require('./plugin'); | ||
|
||
module.exports = { | ||
myService, | ||
plugin, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
const config = require('../config'); | ||
const { pluginId } = require('../util'); | ||
|
||
module.exports = ({ strapi }) => ({ | ||
async getConfig() { | ||
const data = await strapi.config.get(`plugin.${pluginId}`, config.default); | ||
return data; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict'; | ||
|
||
const pluginId = require('./plugin-id'); | ||
|
||
const getService = (name) => strapi.plugin(pluginId).service(name); | ||
|
||
module.exports = getService; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const getService = require('./get-service'); | ||
const pluginId = require('./plugin-id'); | ||
|
||
module.exports = { | ||
getService, | ||
pluginId, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
const pluginPkg = require('../../package.json'); | ||
|
||
module.exports = pluginPkg.strapi.name; |