-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #373 from graasp/366/publishItemRefactor
feat: add co-editor settings
- Loading branch information
Showing
12 changed files
with
1,197 additions
and
947 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,77 @@ | ||
import { | ||
buildCoEditorSettingsRadioButtonId, | ||
buildPublishButtonId, | ||
CO_EDITOR_SETTINGS_RADIO_GROUP_ID, | ||
} from '../../../../src/config/selectors'; | ||
import { buildItemPath } from '../../../../src/config/paths'; | ||
import { | ||
ITEM_WITH_CATEGORIES, | ||
} from '../../../fixtures/categories'; | ||
import { PUBLISHED_ITEM } from '../../../fixtures/items'; | ||
import { DEFAULT_TAGS } from '../../../fixtures/itemTags'; | ||
import { MEMBERS, SIGNED_OUT_MEMBER } from '../../../fixtures/members'; | ||
import { EDIT_TAG_REQUEST_TIMEOUT } from '../../../support/constants'; | ||
import { DISPLAY_CO_EDITORS_OPTIONS } from '../../../../src/config/constants'; | ||
|
||
const openPublishItemTab = (id) => { | ||
cy.get(`#${buildPublishButtonId(id)}`).click(); | ||
}; | ||
|
||
const visitItemPage = (item) => { | ||
cy.setUpApi({ items: [item], tags: DEFAULT_TAGS }); | ||
cy.visit(buildItemPath(item.id)); | ||
openPublishItemTab(item.id); | ||
}; | ||
|
||
describe('Co-editor Setting', () => { | ||
it('Display choices', () => { | ||
const item = ITEM_WITH_CATEGORIES; | ||
visitItemPage(item); | ||
|
||
Object.values(DISPLAY_CO_EDITORS_OPTIONS).forEach((option) => { | ||
const displayTags = cy.get(`#${buildCoEditorSettingsRadioButtonId(option.value)}`); | ||
displayTags.should('be.visible'); | ||
}); | ||
}); | ||
|
||
it('Change choice', () => { | ||
const item = ITEM_WITH_CATEGORIES; | ||
visitItemPage(item); | ||
|
||
const newOptionValue = DISPLAY_CO_EDITORS_OPTIONS.NO.value; | ||
cy.get(`#${buildCoEditorSettingsRadioButtonId(newOptionValue)}`).click(); | ||
cy.wait('@editItem', { timeout: EDIT_TAG_REQUEST_TIMEOUT }).then((data) => { | ||
const { | ||
request: { url, body }, | ||
} = data; | ||
expect(url.split('/')).contains(item.id); | ||
expect(body.settings.displayCoEditors).equals(newOptionValue); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Co-editor setting permissions', () => { | ||
it('User signed out cannot edit co-editor setting', () => { | ||
const item = PUBLISHED_ITEM; | ||
cy.setUpApi({ | ||
items: [item], | ||
currentMember: SIGNED_OUT_MEMBER, | ||
tags: DEFAULT_TAGS, | ||
}); | ||
cy.visit(buildItemPath(item.id)); | ||
openPublishItemTab(item.id); | ||
cy.get(`#${CO_EDITOR_SETTINGS_RADIO_GROUP_ID}`).should('not.exist'); | ||
}); | ||
|
||
it('Read-only user cannot edit co-editor setting', () => { | ||
const item = PUBLISHED_ITEM; | ||
cy.setUpApi({ | ||
items: [item], | ||
currentMember: MEMBERS.BOB, | ||
tags: DEFAULT_TAGS, | ||
}); | ||
cy.visit(buildItemPath(item.id)); | ||
openPublishItemTab(item.id); | ||
cy.get(`#${CO_EDITOR_SETTINGS_RADIO_GROUP_ID}`).should('not.exist'); | ||
}); | ||
}); |
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,105 @@ | ||
import React, { useContext, useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Loader } from '@graasp/ui'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { | ||
Typography, | ||
makeStyles, | ||
Radio, | ||
RadioGroup, | ||
FormControlLabel, | ||
} from '@material-ui/core'; | ||
import { MUTATION_KEYS } from '@graasp/query-client'; | ||
import { useMutation } from '../../../config/queryClient'; | ||
import { CurrentUserContext } from '../../context/CurrentUserContext'; | ||
import { DISPLAY_CO_EDITORS_OPTIONS } from '../../../config/constants'; | ||
import { | ||
buildCoEditorSettingsRadioButtonId, | ||
CO_EDITOR_SETTINGS_RADIO_GROUP_ID, | ||
} from '../../../config/selectors'; | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
title: { | ||
marginTop: theme.spacing(2), | ||
}, | ||
button: { | ||
marginTop: theme.spacing(1), | ||
marginLeft: theme.spacing(2), | ||
}, | ||
})); | ||
|
||
const { EDIT_ITEM } = MUTATION_KEYS; | ||
|
||
const CoEditorSettings = ({ item }) => { | ||
const { t } = useTranslation(); | ||
const classes = useStyles(); | ||
const { mutate: updateDisplayCoEditors } = useMutation(EDIT_ITEM); | ||
|
||
// user | ||
const { isLoading: isMemberLoading } = useContext(CurrentUserContext); | ||
|
||
// current item | ||
const itemId = item?.get('id'); | ||
const settings = item?.get('settings'); | ||
const itemName = item?.get('name'); | ||
|
||
// by default, co editors will not be displayed | ||
const [optionValue, setOptionValue] = useState( | ||
DISPLAY_CO_EDITORS_OPTIONS.NO.value, | ||
); | ||
|
||
useEffect(() => { | ||
if (settings?.displayCoEditors) { | ||
setOptionValue(settings.displayCoEditors); | ||
} | ||
}, [settings]); | ||
|
||
if (isMemberLoading) { | ||
return <Loader />; | ||
} | ||
|
||
const handleChange = (event) => { | ||
// value from radio button is string, convert to boolean | ||
const newValue = event.target.value === 'true'; | ||
setOptionValue(newValue); | ||
updateDisplayCoEditors({ | ||
id: itemId, | ||
name: itemName, | ||
settings: { displayCoEditors: newValue }, | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Typography variant="h6" className={classes.title}> | ||
{t('Co-Editors')} | ||
</Typography> | ||
<Typography variant="body1"> | ||
{t( | ||
'Do you want to display co-editors on the publication page? All users with edit or admin permissions will be displayed.', | ||
)} | ||
</Typography> | ||
<RadioGroup | ||
id={CO_EDITOR_SETTINGS_RADIO_GROUP_ID} | ||
name={t('Display co-editors?')} | ||
value={optionValue} | ||
onChange={handleChange} | ||
> | ||
{Object.values(DISPLAY_CO_EDITORS_OPTIONS).map((option) => ( | ||
<FormControlLabel | ||
id={buildCoEditorSettingsRadioButtonId(option.value)} | ||
value={option.value} | ||
control={<Radio color="primary" />} | ||
label={t(option.label)} | ||
/> | ||
))} | ||
</RadioGroup> | ||
</> | ||
); | ||
}; | ||
|
||
CoEditorSettings.propTypes = { | ||
item: PropTypes.instanceOf(Map).isRequired, | ||
}; | ||
|
||
export default CoEditorSettings; |
Oops, something went wrong.