import React, { memo, useEffect, useState, useReducer } from 'react';
import PropTypes from 'prop-types';
import { get } from 'lodash';
import {
BackHeader,
getQueryParameters,
LoadingIndicatorPage,
LiLink,
PluginHeader,
PopUpWarning,
request,
templateObject,
} from 'strapi-helper-plugin';
import pluginId from '../../pluginId';
import Container from '../../components/Container';
import { LinkWrapper } from './components';
import init from './init';
import reducer, { initialState } from './reducer';
const getRequestUrl = path => `/${pluginId}/explorer/${path}`;
function EditView({
currentEnvironment,
emitEvent,
layouts,
location: { search },
history: { push },
match: {
params: { slug, id },
},
plugins,
}) {
const [showWarningCancel, setWarningCancel] = useState(false);
const [showWarningDelete, setWarningDelete] = useState(false);
const [isSubmitting, setIsSubmitting] = useState(false);
const [reducerState, dispatch] = useReducer(reducer, initialState, () =>
init(initialState)
);
const layout = get(layouts, [slug], {});
const isCreatingEntry = id === 'create';
const source = getQueryParameters(search, 'source');
const shouldShowLoader = !isCreatingEntry && isLoading;
useEffect(() => {
const fetchData = async () => {
try {
const data = await request(getRequestUrl(`${slug}/${id}`), {
method: 'GET',
params: { source },
});
dispatch({
type: 'GET_DATA_SUCCEEDED',
data,
});
} catch (err) {
strapi.notification.error(`${pluginId}.error.record.fetch`);
}
};
if (!isCreatingEntry) {
fetchData();
}
}, [id, isCreatingEntry, slug, source]);
if (shouldShowLoader) {
return ;
}
const state = reducerState.toJS();
const { initialData, isLoading } = state;
const toggleWarningCancel = () => setWarningCancel(prevState => !prevState);
const toggleWarningDelete = () => setWarningDelete(prevState => !prevState);
const redirectURL = search.split('redirectUrl=')[1];
const redirectToPreviousPage = () => push(redirectURL);
const handleConfirmDelete = async () => {
toggleWarningDelete();
setIsSubmitting(true);
try {
await request(getRequestUrl(`${slug}/${id}`), {
method: 'DELETE',
params: { source },
});
strapi.notification.success(`${pluginId}.success.record.delete`);
redirectToPreviousPage();
} catch (err) {
setIsSubmitting(false);
strapi.notification.error(`${pluginId}.error.record.delete`);
}
};
const handleSubmit = e => {
e.preventDefault();
};
const displayedFieldNameInHeader = get(
layout,
['settings', 'mainField'],
'id'
);
const pluginHeaderTitle = isCreatingEntry
? { id: `${pluginId}.containers.Edit.pluginHeader.title.new` }
: templateObject({ mainField: displayedFieldNameInHeader }, initialData)
.mainField;
/**
* Retrieve external links from injected components
* @type {Array} List of external links to display
*/
const retrieveLinksContainerComponent = () => {
const componentToInject = Object.keys(plugins).reduce((acc, current) => {
// Retrieve injected compos from plugin
// if compo can be injected in left.links area push the compo in the array
const currentPlugin = plugins[current];
const injectedComponents = get(currentPlugin, 'injectedComponents', []);
const compos = injectedComponents
.filter(compo => {
return (
compo.plugin === `${pluginId}.editPage` &&
compo.area === 'right.links'
);
})
.map(compo => {
const Component = compo.component;
return (
slug}
getSource={() => source}
getContentTypeBuilderBaseUrl={() =>
'/plugins/content-type-builder/models/'
}
{...compo.props}
key={compo.key}
onClick={() => {
emitEvent('willEditContentTypeFromEditView');
}}
/>
);
});
return [...acc, ...compos];
}, []);
return componentToInject;
};
return (
<>
redirectToPreviousPage()} />
{
dispatch({
type: 'RESET_FORM',
});
toggleWarningCancel();
}}
/>
>
);
}
EditView.propTypes = {
currentEnvironment: PropTypes.string.isRequired,
emitEvent: PropTypes.func.isRequired,
history: PropTypes.shape({
push: PropTypes.func.isRequired,
}),
layouts: PropTypes.object,
location: PropTypes.shape({
search: PropTypes.string,
}),
match: PropTypes.shape({
params: PropTypes.shape({
id: PropTypes.string.isRequired,
slug: PropTypes.string.isRequired,
}),
}),
plugins: PropTypes.object,
};
export default memo(EditView);