-
Notifications
You must be signed in to change notification settings - Fork 46
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 #1374 from OneCommunityGlobal/Nathan-reset-permiss…
…ions-to-default Nathan reset permissions to default
- Loading branch information
Showing
10 changed files
with
381 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import axios from 'axios'; | ||
import { ENDPOINTS } from '../utils/URL'; | ||
import * as types from "../constants/rolePermissionPresets"; | ||
|
||
export const fetchPresets = (presets) => { | ||
return { | ||
type: types.RECEIVE_PRESETS, | ||
presets, | ||
}; | ||
}; | ||
|
||
export const postNewPreset = payload => { | ||
return { | ||
type: types.ADD_NEW_PRESET, | ||
payload, | ||
}; | ||
}; | ||
|
||
export const deletePreset = presetId => { | ||
return { | ||
type: types.DELETE_PRESET, | ||
presetId, | ||
}; | ||
}; | ||
|
||
export const updatePreset = payload => { | ||
return { | ||
type: types.UPDATE_PRESET, | ||
payload, | ||
}; | ||
}; | ||
|
||
export const getPresetsByRole = (roleName) => async dispatch => { | ||
const URL = ENDPOINTS.PRESETS_BY_ID(roleName); | ||
const { data } = await axios.get(URL); | ||
return dispatch(fetchPresets(data)); | ||
}; | ||
|
||
export const createNewPreset = newPreset => { | ||
return async dispatch => { | ||
try { | ||
const res = await axios.post(ENDPOINTS.PRESETS(), newPreset); | ||
if (res.status === 201){ | ||
dispatch(postNewPreset(res.data.newPreset)); | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
return status; | ||
}; | ||
}; | ||
|
||
export const updatePresetById = (updatedPreset) => { | ||
return async dispatch => { | ||
try { | ||
const res = await axios.put(ENDPOINTS.PRESETS_BY_ID(updatedPreset._id), updatedPreset); | ||
if (res.status === 200){ | ||
dispatch(updatePreset(updatedPreset)); | ||
} | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
}; | ||
|
||
export const deletePresetById = (presetId) => { | ||
return async dispatch => { | ||
try { | ||
const res = await axios.delete(ENDPOINTS.PRESETS_BY_ID(presetId)); | ||
if (res.status === 200){ | ||
dispatch(deletePreset(presetId)); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
}; |
32 changes: 32 additions & 0 deletions
32
src/components/PermissionsManagement/PermissionsPresetsModal.jsx
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,32 @@ | ||
import React, { useState } from 'react'; | ||
import { FormCheck } from 'react-bootstrap'; | ||
import { Alert, Button, Form, FormGroup, Input, Label } from 'reactstrap'; | ||
import { toast } from 'react-toastify'; | ||
import { connect } from 'react-redux'; | ||
import Preset from './Preset'; | ||
import { boxStyle } from 'styles'; | ||
|
||
|
||
const PermissionsPresetsModal = (props) => { | ||
return ( | ||
<> | ||
{props.presetsList?.length===0 ? 'There are no presets for this role.' : props.presetsList?.map((preset) => ( | ||
<div key={preset._id}> | ||
<Preset | ||
preset={preset} | ||
roleId={props.roleId} | ||
roleName={props.roleName} | ||
onApply={props.onApply} | ||
/> | ||
</div> | ||
))} | ||
</> | ||
); | ||
}; | ||
|
||
const mapStateToProps = state => ({ presetsList: state.rolePreset.presets }); | ||
|
||
// const mapDispatchToProps = dispatch => ({ | ||
// }); | ||
|
||
export default connect(mapStateToProps, null)(PermissionsPresetsModal); |
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,130 @@ | ||
import React, { useState, useRef } from 'react'; | ||
import { FormCheck } from 'react-bootstrap'; | ||
import { Alert, Button, Form, FormGroup, Input, Label, Row } from 'reactstrap'; | ||
import { permissionLabel } from './UserRoleTab'; | ||
import { mainPermissions } from './RolePermissions'; | ||
import { toast } from 'react-toastify'; | ||
import { connect } from 'react-redux'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { faEdit } from '@fortawesome/free-regular-svg-icons'; | ||
import { updateRole } from '../../actions/role'; | ||
import { updatePresetById, deletePresetById } from '../../actions/rolePermissionPresets'; | ||
import { BsFillCaretDownFill, BsFillCaretUpFill } from 'react-icons/bs'; | ||
import { boxStyle } from 'styles'; | ||
import { update } from 'lodash'; | ||
|
||
|
||
const Preset = (props) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const [editing, setEditing] = useState(false); | ||
const ref = useRef(null); | ||
|
||
const onNameClicked = (e) => { | ||
if(editing){ | ||
e.stopPropagation(); | ||
} | ||
} | ||
|
||
const onEditIconClicked = (e) => { | ||
e.stopPropagation(); | ||
setEditing(!editing); | ||
setTimeout(()=>{ref.current.focus()}, 0); | ||
}; | ||
|
||
const onSaveNameChangeClicked = (e) => { | ||
e.stopPropagation(); | ||
var newName = ref.current.textContent; | ||
props.updatePreset({...props.preset, presetName: newName}); | ||
setEditing(false); | ||
}; | ||
|
||
const applyPreset = async (preset) => { | ||
try { | ||
const updatedRole = { | ||
roleId: props.roleId, | ||
roleName: props.roleName, | ||
permissions: preset.permissions | ||
}; | ||
props.updateRole(props.roleId, updatedRole); | ||
props.onApply(preset.permissions); | ||
} catch (error) { | ||
console.log(error.message); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: Row, | ||
cursor: 'pointer', | ||
width: '100%', | ||
borderBottom: 'solid', | ||
borderColor: 'grey', | ||
justifyContent: 'space-between' | ||
}} | ||
onClick={()=>setIsOpen(!isOpen)} | ||
> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
gap: '10px' | ||
}}> | ||
<h3 ref={ref} contentEditable={editing} suppressContentEditableWarning={true} onClick={onNameClicked} >{props.preset.presetName}</h3> | ||
<FontAwesomeIcon | ||
icon={faEdit} | ||
size="lg" | ||
className="user-role-tab__icon edit-icon" | ||
onClick={onEditIconClicked} | ||
/> | ||
{editing && | ||
<Button | ||
color='primary' | ||
onClick={onSaveNameChangeClicked}> | ||
Save Name | ||
</Button>} | ||
{isOpen ? <BsFillCaretUpFill/>:<BsFillCaretDownFill/> } | ||
</div> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
gap: '10px' | ||
}}> | ||
<Button color='danger' onClick={(event)=>{event.stopPropagation(); props.deletePreset(props.preset._id);}}> | ||
Delete | ||
</Button> | ||
<Button | ||
color='primary' | ||
onClick={(event)=>{event.stopPropagation(); applyPreset(props.preset);}}> | ||
Apply | ||
</Button> | ||
</div> | ||
</div> | ||
{isOpen ? Object.keys(permissionLabel).map((permission) => ( | ||
<li className="user-role-tab__permissions" key={permission}> | ||
<p style={{ | ||
color: props.preset.permissions.includes(permission) ? 'green' : 'red' , | ||
fontSize: mainPermissions.includes(permissionLabel[permission]) ? '20px':'', | ||
paddingLeft: mainPermissions.includes(permissionLabel[permission]) ? '0': '50px' | ||
}}> | ||
{permissionLabel[permission]} | ||
</p> | ||
</li> | ||
)):<></>} | ||
</> | ||
); | ||
}; | ||
|
||
|
||
|
||
const mapStateToProps = state => ({ roles: state.role.roles }); | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
updateRole: (roleId, updatedRole) => dispatch(updateRole(roleId, updatedRole)), | ||
deletePreset: (presetId) => dispatch(deletePresetById(presetId)), | ||
updatePreset: (newPreset) => dispatch(updatePresetById(newPreset)) | ||
}); | ||
|
||
export default connect(mapStateToProps, mapDispatchToProps)(Preset); |
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
Oops, something went wrong.