Skip to content

Commit

Permalink
refactored reducer for purity and restrict Edit Mode access
Browse files Browse the repository at this point in the history
  • Loading branch information
Om Chauhan authored and Om Chauhan committed Dec 4, 2024
1 parent eeb502f commit bcf80c0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ const slideValidationRules = [

// For switching between edit and view mode
export const getTrainingMode = () => (dispatch) => {
const mainDiv = document.getElementById('main');
const editMode = JSON.parse(mainDiv.getAttribute('data-training_mode')).editMode;
dispatch({
type: SET_TRAINING_MODE,
data: { editMode },
});
};

Expand Down
4 changes: 1 addition & 3 deletions app/assets/javascripts/reducers/training.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
SLIDE_COMPLETED, SET_TRAINING_MODE
} from '../constants';

const mainDiv = document.getElementById('main');

const reviewAnswer = function (state, answer) {
const answerId = parseInt(answer);
const temp = { ...state, currentSlide: { ...state.currentSlide, selectedAnswer: answerId } };
Expand Down Expand Up @@ -106,7 +104,7 @@ export default function training(state = initialState, action) {
case SET_TRAINING_MODE:
return {
...state,
editMode: JSON.parse(mainDiv.getAttribute('data-training_mode')).editMode
editMode: data.editMode
};
default:
return state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const TrainingContentHandler = (props) => {
const [showModal, setShowModal] = useState(false);
const [updatingEditMode, setUpdatingEditMode] = useState(false);
const dispatch = useDispatch();
const { usersignedin: userSignedInStr } = document.getElementById('nav_root').dataset;
const userSignedIn = userSignedInStr === 'true';

const toggleModal = () => {
setShowModal(!showModal);
Expand Down Expand Up @@ -42,9 +44,12 @@ const TrainingContentHandler = (props) => {
<i className="icon icon-plus" />
</button>
)}
<button className="button dark" onClick={toggleEditMode} style={buttonStyle}>
{trainingMode}
</button>
{/* Switching to edit mode is only allowed for admins */}
{userSignedIn && props.currentUser.isAdmin && (
<button className="button dark" onClick={toggleEditMode} style={buttonStyle}>
{trainingMode}
</button>
)}
</div>
</div>
</div>
Expand Down
11 changes: 5 additions & 6 deletions spec/features/training_modification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@
before do
login_as(user, scope: :user)
visit '/training'
click_button 'Switch to Edit Mode'
end

it 'does not show the "Create Training Library" button' do
expect(page).not_to have_content('Create New Library')
it 'does not show the "Switch to Edit Mode" button' do
expect(page).not_to have_content('Switch to Edit Mode')
end
end
end

describe 'TrainingCategory' do
before do
login_as(user, scope: :user)
login_as(admin, scope: :user)
visit '/training'
click_button 'Switch to Edit Mode'
visit "/training/#{training_library.slug}"
Expand Down Expand Up @@ -155,7 +154,7 @@
# For Training Module
describe 'TrainingModule' do
before do
login_as(user, scope: :user)
login_as(admin, scope: :user)
visit '/training'
click_button 'Switch to Edit Mode'
visit "/training/#{training_library.slug}"
Expand Down Expand Up @@ -253,7 +252,7 @@
end

before do
login_as(user, scope: :user)
login_as(admin, scope: :user)
visit '/training'
click_button 'Switch to Edit Mode'
sleep 1
Expand Down

0 comments on commit bcf80c0

Please sign in to comment.