-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '3494-media-field-revamp' of github.com:huridocs/uwazi i…
…nto 3494-media-field-revamp
- Loading branch information
Showing
7 changed files
with
167 additions
and
71 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
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,56 @@ | ||
import React, { useMemo, useState } from 'react'; | ||
import { ObjectId } from 'mongodb'; | ||
|
||
import { AttachmentSchema } from 'shared/types/commonTypes'; | ||
import { RenderAttachment } from 'app/Attachments/components/RenderAttachment'; | ||
import { Translate } from 'app/I18N'; | ||
import { Icon } from 'app/UI'; | ||
import { MediaModal } from 'app/Metadata/components/MediaModal'; | ||
|
||
interface MediaFieldProps { | ||
attachments: AttachmentSchema[]; | ||
value: string | ObjectId | null; | ||
onChange: (val: string | ObjectId | null) => void; | ||
} | ||
|
||
const MediaField = ({ attachments, value, onChange }: MediaFieldProps) => { | ||
const [openModal, setOpenModal] = useState(false); | ||
|
||
const selectedImage = useMemo(() => attachments.find(a => a._id === value), [attachments, value]); | ||
|
||
const handleCloseMediaModal = () => { | ||
setOpenModal(false); | ||
}; | ||
|
||
const handleImageRemove = () => { | ||
onChange(null); | ||
}; | ||
|
||
return ( | ||
<div className="search__filter--selected__media"> | ||
{!!selectedImage && <RenderAttachment attachment={selectedImage} />} | ||
|
||
<div className="search__filter--selected__media-toolbar"> | ||
<button type="button" onClick={() => setOpenModal(true)} className="btn btn-success"> | ||
<Icon icon="plus" /> <Translate>Select supporting file</Translate> | ||
</button> | ||
|
||
{!!selectedImage && ( | ||
<button type="button" onClick={handleImageRemove} className="btn btn-danger "> | ||
<Icon icon="trash-alt" /> | ||
</button> | ||
)} | ||
</div> | ||
|
||
<MediaModal | ||
isOpen={openModal} | ||
onClose={handleCloseMediaModal} | ||
onChange={onChange} | ||
selectedId={value} | ||
attachments={attachments} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default MediaField; |
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
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,50 @@ | ||
import React from 'react'; | ||
import thunk from 'redux-thunk'; | ||
import { shallow, ShallowWrapper } from 'enzyme'; | ||
import ReactModal from 'react-modal'; | ||
import configureMockStore from 'redux-mock-store'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import { MediaModal, MediaModalProps } from '../MediaModal'; | ||
|
||
const mockStore = configureMockStore([thunk]); | ||
const store = mockStore({}); | ||
|
||
describe('Attachments Modal', () => { | ||
let component: ShallowWrapper; | ||
let props: MediaModalProps; | ||
|
||
beforeEach(() => { | ||
props = { | ||
onClose: jasmine.createSpy('onClose'), | ||
onChange: jasmine.createSpy('onChange'), | ||
isOpen: true, | ||
attachments: [], | ||
selectedId: '1', | ||
}; | ||
}); | ||
|
||
const render = (otherProps = {}) => { | ||
component = shallow( | ||
<Provider store={store}> | ||
<MediaModal {...props} {...otherProps} /> | ||
</Provider> | ||
).dive(); | ||
}; | ||
|
||
it('Should pass isOpen props to Media modal.', () => { | ||
render({ isOpen: false }); | ||
expect(component.find(ReactModal).props().isOpen).toBe(false); | ||
render({ isOpen: true }); | ||
expect(component.find(ReactModal).props().isOpen).toBe(true); | ||
}); | ||
|
||
it('Should call onClose', () => { | ||
render(); | ||
|
||
const closeButton = component.find('.attachments-modal__close'); | ||
closeButton.simulate('click'); | ||
|
||
expect(props.onClose).toHaveBeenCalled(); | ||
}); | ||
}); |
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