fix: obscure bug where upload collection has upload field relating to itself
This commit is contained in:
@@ -11,6 +11,7 @@ import { FieldTypes } from '..';
|
|||||||
import AddModal from './Add';
|
import AddModal from './Add';
|
||||||
import SelectExistingModal from './SelectExisting';
|
import SelectExistingModal from './SelectExisting';
|
||||||
import { SanitizedCollectionConfig } from '../../../../../collections/config/types';
|
import { SanitizedCollectionConfig } from '../../../../../collections/config/types';
|
||||||
|
import { useEditDepth, EditDepthContext } from '../../../utilities/EditDepth';
|
||||||
|
|
||||||
import './index.scss';
|
import './index.scss';
|
||||||
|
|
||||||
@@ -58,13 +59,15 @@ const UploadInput: React.FC<UploadInputProps> = (props) => {
|
|||||||
filterOptions,
|
filterOptions,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const { toggleModal } = useModal();
|
const { toggleModal, modalState } = useModal();
|
||||||
|
const editDepth = useEditDepth();
|
||||||
|
|
||||||
const addModalSlug = `${path}-add`;
|
const addModalSlug = `${path}-add-depth-${editDepth}`;
|
||||||
const selectExistingModalSlug = `${path}-select-existing`;
|
const selectExistingModalSlug = `${path}-select-existing-depth-${editDepth}`;
|
||||||
|
|
||||||
const [file, setFile] = useState(undefined);
|
const [file, setFile] = useState(undefined);
|
||||||
const [missingFile, setMissingFile] = useState(false);
|
const [missingFile, setMissingFile] = useState(false);
|
||||||
|
const [modalToRender, setModalToRender] = useState<string>();
|
||||||
|
|
||||||
const classes = [
|
const classes = [
|
||||||
'field-type',
|
'field-type',
|
||||||
@@ -98,6 +101,12 @@ const UploadInput: React.FC<UploadInputProps> = (props) => {
|
|||||||
serverURL,
|
serverURL,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!modalState[addModalSlug]?.isOpen && !modalState[selectExistingModalSlug]?.isOpen) {
|
||||||
|
setModalToRender(undefined);
|
||||||
|
}
|
||||||
|
}, [modalState, addModalSlug, selectExistingModalSlug]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={classes}
|
className={classes}
|
||||||
@@ -132,6 +141,7 @@ const UploadInput: React.FC<UploadInputProps> = (props) => {
|
|||||||
buttonStyle="secondary"
|
buttonStyle="secondary"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
toggleModal(addModalSlug);
|
toggleModal(addModalSlug);
|
||||||
|
setModalToRender(addModalSlug);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Upload new
|
Upload new
|
||||||
@@ -142,33 +152,40 @@ const UploadInput: React.FC<UploadInputProps> = (props) => {
|
|||||||
buttonStyle="secondary"
|
buttonStyle="secondary"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
toggleModal(selectExistingModalSlug);
|
toggleModal(selectExistingModalSlug);
|
||||||
|
setModalToRender(selectExistingModalSlug);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
Choose from existing
|
Choose from existing
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<AddModal
|
<EditDepthContext.Provider value={editDepth + 1}>
|
||||||
{...{
|
{modalToRender === addModalSlug && (
|
||||||
collection,
|
<AddModal
|
||||||
slug: addModalSlug,
|
{...{
|
||||||
fieldTypes,
|
collection,
|
||||||
setValue: (e) => {
|
slug: addModalSlug,
|
||||||
setMissingFile(false);
|
fieldTypes,
|
||||||
onChange(e);
|
setValue: (e) => {
|
||||||
},
|
setMissingFile(false);
|
||||||
}}
|
onChange(e);
|
||||||
/>
|
},
|
||||||
<SelectExistingModal
|
}}
|
||||||
{...{
|
/>
|
||||||
collection,
|
)}
|
||||||
slug: selectExistingModalSlug,
|
{modalToRender === selectExistingModalSlug && (
|
||||||
setValue: onChange,
|
<SelectExistingModal
|
||||||
addModalSlug,
|
{...{
|
||||||
filterOptions,
|
collection,
|
||||||
path,
|
slug: selectExistingModalSlug,
|
||||||
}}
|
setValue: onChange,
|
||||||
/>
|
addModalSlug,
|
||||||
|
filterOptions,
|
||||||
|
path,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</EditDepthContext.Provider>
|
||||||
<FieldDescription
|
<FieldDescription
|
||||||
value={file}
|
value={file}
|
||||||
description={description}
|
description={description}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { CollectionConfig } from '../../../../src/collections/config/types';
|
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { CollectionConfig } from '../../../../src/collections/config/types';
|
||||||
|
|
||||||
const Uploads: CollectionConfig = {
|
const Uploads: CollectionConfig = {
|
||||||
slug: 'uploads',
|
slug: 'uploads',
|
||||||
@@ -11,6 +11,11 @@ const Uploads: CollectionConfig = {
|
|||||||
type: 'text',
|
type: 'text',
|
||||||
name: 'text',
|
name: 'text',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
type: 'upload',
|
||||||
|
name: 'media',
|
||||||
|
relationTo: 'uploads',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user