Skip to content

Commit

Permalink
fix: disable save on anonymous not read
Browse files Browse the repository at this point in the history
  • Loading branch information
juancarlosfarah committed Feb 20, 2024
1 parent 22d56a1 commit 302f4ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cypress/e2e/player/main.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import {
import { USER_ANSWER_CONTENT } from '../../fixtures/appData';
import { ANSWER_SETTING, QUESTION_SETTING } from '../../fixtures/appSettings';

describe('player view as reader', () => {
describe('player view as anonymous', () => {
beforeEach(() => {
cy.setUpApi(
{
appSettings: [QUESTION_SETTING, ANSWER_SETTING],
},
{
context: Context.Player,
permission: PermissionLevel.Read,
memberId: undefined,
},
);
cy.visit(`/`);
Expand Down Expand Up @@ -51,15 +51,15 @@ describe('player view as reader', () => {
});
});

describe('player view as writer', () => {
describe('player view as reader', () => {
beforeEach(() => {
cy.setUpApi(
{
appSettings: [QUESTION_SETTING, ANSWER_SETTING],
},
{
context: Context.Player,
permission: PermissionLevel.Write,
permission: PermissionLevel.Read,
},
);
cy.visit(`/`);
Expand Down
26 changes: 9 additions & 17 deletions src/modules/main/PlayerView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@mui/material';

import { useLocalContext } from '@graasp/apps-query-client';
import { AppData, PermissionLevel } from '@graasp/sdk';
import { AppData } from '@graasp/sdk';

import isEqual from 'lodash.isequal';
import sortBy from 'lodash.sortby';
Expand All @@ -29,7 +29,7 @@ function isAnswer(appData: AppData): boolean {

const PlayerView = (): JSX.Element => {
const { t } = useTranslation('translations', { keyPrefix: 'PLAYER' });
const { permission } = useLocalContext();
const { memberId } = useLocalContext();
const {
question,
// answer: answerSavedState,
Expand All @@ -55,29 +55,21 @@ const PlayerView = (): JSX.Element => {
const [answer, setAnswer] = useState<string>(savedAnswer);

const disableSave = useMemo(() => {
// disable if permission is read
if (permission === PermissionLevel.Read) {
// disable if there is no user (logged out or anonymous)
if (!memberId) {
return true;
}
// disable if answer is equal
if (isEqual(savedAnswer, answer)) {
return true;
}

// disable if minimum length is not achieved
if (answer.length < minChars) {
return true;
}

return false;
}, [answer, savedAnswer, permission, minChars]);
return isEqual(savedAnswer, answer);
}, [answer, savedAnswer, memberId]);

const disabledMessage = useMemo(() => {
if (permission === PermissionLevel.Read) {
// disable if there is no user (logged out or anonymous)
if (!memberId) {
return t('SAVE_BUTTON');
}
return t('SAVED_MESSAGE');
}, [permission, t]);
}, [memberId, t]);

const handleChangeAnswer = (event: ChangeEvent<HTMLInputElement>): void => {
const { value } = event.target;
Expand Down

0 comments on commit 302f4ea

Please sign in to comment.