mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 08:52:26 +00:00
Data manager and hooks
This commit is contained in:
parent
d39c538138
commit
3023c70e30
@ -0,0 +1,99 @@
|
|||||||
|
import React, { useEffect, useReducer, memo } from 'react';
|
||||||
|
import PropTypes from 'prop-types';
|
||||||
|
//import { request } from 'strapi-helper-plugin';
|
||||||
|
import WebhooksDataManagerContext from '../../contexts/WebhooksDataManager';
|
||||||
|
import init from './init';
|
||||||
|
import reducer, { initialState } from './reducer';
|
||||||
|
|
||||||
|
const WebhooksDataManagerProvider = ({ children }) => {
|
||||||
|
const [reducerState, dispatch] = useReducer(reducer, initialState, init);
|
||||||
|
const { webhooks } = reducerState.toJS();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchData = async () => {
|
||||||
|
try {
|
||||||
|
// const { data } = await request(`/admin/webhooks`, {
|
||||||
|
// method: 'GET',
|
||||||
|
// });
|
||||||
|
|
||||||
|
const list = [
|
||||||
|
{
|
||||||
|
id: 0,
|
||||||
|
name: 'gatsby',
|
||||||
|
isEnabled: false,
|
||||||
|
url: 'http://thisisanexample.com/1234867874',
|
||||||
|
headers: {
|
||||||
|
Authorisation: 'x-secret',
|
||||||
|
},
|
||||||
|
hooks: ['createEntry', 'editEntry', 'deleteEntry', 'createMedia'],
|
||||||
|
links: [
|
||||||
|
{
|
||||||
|
icon: 'pencil',
|
||||||
|
onClick: () => {
|
||||||
|
console.log('edit');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
icon: 'trash',
|
||||||
|
onClick: () => {
|
||||||
|
console.log('delete');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
name: 'gatsby',
|
||||||
|
isEnabled: false,
|
||||||
|
url: 'http://thisisanexample.com/1234867874',
|
||||||
|
headers: {
|
||||||
|
Authorisation: 'x-secret',
|
||||||
|
},
|
||||||
|
hooks: ['createEntry', 'editEntry', 'deleteEntry', 'createMedia'],
|
||||||
|
links: [
|
||||||
|
{
|
||||||
|
icon: 'pencil',
|
||||||
|
onClick: () => {
|
||||||
|
console.log('edit');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
icon: 'trash',
|
||||||
|
onClick: () => {
|
||||||
|
console.log('delete');
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: 'GET_DATA_SUCCEEDED',
|
||||||
|
data: list,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code !== 20) {
|
||||||
|
strapi.notification.error('notification.error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<WebhooksDataManagerContext.Provider
|
||||||
|
value={{
|
||||||
|
webhooks,
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</WebhooksDataManagerContext.Provider>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
WebhooksDataManagerProvider.propTypes = {
|
||||||
|
children: PropTypes.node.isRequired,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default memo(WebhooksDataManagerProvider);
|
@ -0,0 +1,5 @@
|
|||||||
|
function init(initialState) {
|
||||||
|
return initialState;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default init;
|
@ -0,0 +1,17 @@
|
|||||||
|
import { fromJS } from 'immutable';
|
||||||
|
|
||||||
|
const initialState = fromJS({
|
||||||
|
webhooks: [],
|
||||||
|
});
|
||||||
|
|
||||||
|
const reducer = (state, action) => {
|
||||||
|
switch (action.type) {
|
||||||
|
case 'GET_DATA_SUCCEEDED':
|
||||||
|
return state.update('webhooks', () => fromJS(action.data));
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default reducer;
|
||||||
|
export { initialState };
|
@ -0,0 +1,5 @@
|
|||||||
|
import { createContext } from 'react';
|
||||||
|
|
||||||
|
const WebhooksContext = createContext();
|
||||||
|
|
||||||
|
export default WebhooksContext;
|
@ -0,0 +1,5 @@
|
|||||||
|
import { createContext } from 'react';
|
||||||
|
|
||||||
|
const WebhooksDataManagerContext = createContext();
|
||||||
|
|
||||||
|
export default WebhooksDataManagerContext;
|
6
packages/strapi-admin/admin/src/hooks/useDataManager.js
Normal file
6
packages/strapi-admin/admin/src/hooks/useDataManager.js
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import { useContext } from 'react';
|
||||||
|
import WebhooksDataManagerContext from '../contexts/WebhooksDataManager';
|
||||||
|
|
||||||
|
const useDataManager = () => useContext(WebhooksDataManagerContext);
|
||||||
|
|
||||||
|
export default useDataManager;
|
Loading…
x
Reference in New Issue
Block a user