Skip to content

Commit

Permalink
Merge pull request #30 from bcgsc/fix/issue-28-readonly-user-cant-see…
Browse files Browse the repository at this point in the history
…-graph

fix: allow readonly user to swap to graph view
  • Loading branch information
areisle authored Feb 9, 2022
2 parents f72972b + d1684cb commit b7d745d
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/components/RecordForm/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as api from '@/services/api';

import RecordForm from '..';

const auth = { user: { '@rid': '23:9' } };
const auth = { user: { '@rid': '23:9' }, hasWriteAccess: true };

jest.mock('@/services/api', () => {
const mockRequest = () => ({
Expand Down
3 changes: 3 additions & 0 deletions src/components/RecordForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { cleanPayload, FORM_VARIANT } from '@/components/util';
import api from '@/services/api';
import schema from '@/services/schema';

import { useAuth } from '../Auth';
import EdgeTable from './EdgeTable';
import RelatedStatementsTable from './RelatedStatementsTable';
import RelatedVariantsTable from './RelatedVariantsTable';
Expand Down Expand Up @@ -49,6 +50,7 @@ const RecordForm = ({
...rest
}) => {
const snackbar = useSnackbar();
const auth = useAuth();

const [actionInProgress, setActionInProgress] = useState(false);
const controllers = useRef([]);
Expand Down Expand Up @@ -199,6 +201,7 @@ const RecordForm = ({
<div className={`header__actions header__actions--${variant}`}>
{onTopClick && (variant === FORM_VARIANT.VIEW || variant === FORM_VARIANT.EDIT) && (
<RecordFormStateToggle
allowEdit={auth.hasWriteAccess}
message="Are you sure? You will lose your changes."
onClick={handleToggleState}
requireConfirm={variant === 'edit' && formIsDirty}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('RecordFormStateToggle', () => {
beforeEach(() => {
({ getByText } = render((
<RecordFormStateToggle
allowEdit
message="Changes you will lose"
onClick={clickSpy}
value="view"
Expand Down
18 changes: 12 additions & 6 deletions src/components/RecordFormStateToggle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import ConfirmActionDialog from '@/components/ActionButton/ConfirmActionDialog';
*
* @property {object} props
* @property {function} props.onClick parent handler function to toggle states
* @property {bool} props.allowEdit if true, edit button is shown
* @property {bool} props.requireConfirm flag to check whether confirmation is needed
* @property {string} props.message message displayed in confirmation dialog
* @property {string} props.value starting variant value
Expand All @@ -23,6 +24,7 @@ function RecordFormStateToggle({
requireConfirm,
message,
value: inputValue,
allowEdit,
}) {
const [value, setValue] = useState(inputValue);
const [dialogOpen, setDialogOpen] = useState(false);
Expand Down Expand Up @@ -78,12 +80,14 @@ function RecordFormStateToggle({
>
<ViewIcon /><span className="toggle-option__text">View</span>
</ToggleButton>
<ToggleButton
aria-label="edit"
value="edit"
>
<EditIcon /><span className="toggle-option__text">Edit</span>
</ToggleButton>
{allowEdit && (
<ToggleButton
aria-label="edit"
value="edit"
>
<EditIcon /><span className="toggle-option__text">Edit</span>
</ToggleButton>
)}
</ToggleButtonGroup>
{requireConfirm && (
<ConfirmActionDialog
Expand All @@ -99,6 +103,7 @@ function RecordFormStateToggle({
}

RecordFormStateToggle.propTypes = {
allowEdit: PropTypes.bool,
message: PropTypes.string,
onClick: PropTypes.func,
requireConfirm: PropTypes.bool,
Expand All @@ -107,6 +112,7 @@ RecordFormStateToggle.propTypes = {

RecordFormStateToggle.defaultProps = {
onClick: () => {},
allowEdit: false,
requireConfirm: false,
message: 'Are you sure?',
value: 'view',
Expand Down
2 changes: 1 addition & 1 deletion src/components/StatementForm/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AuthContext } from '@/components/Auth';
import StatementForm from '..';


const auth = { user: { '@rid': '23:9' } };
const auth = { user: { '@rid': '23:9' }, hasWriteAccess: true };

jest.mock('@/services/api', () => {
const mockRequest = () => ({
Expand Down
1 change: 1 addition & 0 deletions src/components/StatementForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ const StatementForm = ({
{civicEvidenceId && <CivicEvidenceLink evidenceId={civicEvidenceId} />}
{onTopClick && (variant === FORM_VARIANT.VIEW || variant === FORM_VARIANT.EDIT) && (
<RecordFormStateToggle
allowEdit={auth.hasWriteAccess}
message="Are you sure? You will lose your changes."
onClick={handleToggleState}
requireConfirm={variant === 'edit' && formIsDirty}
Expand Down
14 changes: 2 additions & 12 deletions src/views/RecordView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React, {
} from 'react';
import useDeepCompareEffect from 'use-deep-compare-effect';

import { useAuth } from '@/components/Auth';
import RecordForm from '@/components/RecordForm';
import StatementForm from '@/components/StatementForm';
import { HistoryPropType } from '@/components/types';
Expand Down Expand Up @@ -49,7 +48,6 @@ const getModelFromName = (path = '', modelName = '', variant = FORM_VARIANT.VIEW

const RecordView = (props) => {
const { history, match: { path, params: { rid, modelName: modelNameParam, variant } } } = props;
const auth = useAuth();

const [recordContent, setRecordContent] = useState({});
const [modelName, setModelName] = useState(modelNameParam || '');
Expand Down Expand Up @@ -164,11 +162,7 @@ const RecordView = (props) => {
navigateToGraph={navigateToGraphView}
onError={handleError}
onSubmit={handleSubmit}
onTopClick={
auth.hasWriteAccess
? onTopClick
: null
}
onTopClick={onTopClick}
rid={rid}
title={DEFAULT_TITLES[variant].replace(':modelName', 'Statement')}
value={recordContent}
Expand All @@ -182,11 +176,7 @@ const RecordView = (props) => {
navigateToGraph={navigateToGraphView}
onError={handleError}
onSubmit={handleSubmit}
onTopClick={
auth.hasWriteAccess
? onTopClick
: null
}
onTopClick={onTopClick}
rid={rid}
title={DEFAULT_TITLES[variant].replace(':modelName', modelName)}
value={recordContent}
Expand Down

0 comments on commit b7d745d

Please sign in to comment.