Skip to content

Commit

Permalink
Merge pull request #94 from hellomuthu23/add-moderator-option
Browse files Browse the repository at this point in the history
Allow members to be moderator
  • Loading branch information
hellomuthu23 committed Feb 14, 2024
2 parents f53bcd8 + 3cea05b commit 2b93916
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/Players/PlayerCard/PlayerCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const PlayerCard: React.FC<PlayerCardProps> = ({ game, player, currentPla
title={player.name}
titleTypographyProps={{ variant: 'subtitle2', noWrap: true, title: player.name }}
action={
isModerator(game.createdById, currentPlayerId) &&
isModerator(game.createdById, currentPlayerId, game.isAllowMembersToManageSession) &&
player.id !== currentPlayerId && (
<IconButton
title='Remove'
Expand Down
34 changes: 33 additions & 1 deletion src/components/Poker/CreateGame/CreateGame.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,39 @@ describe('CreateGame component', () => {
expect(gamesService.addNewGame).toHaveBeenCalled();

expect(gamesService.addNewGame).toHaveBeenCalledWith(
expect.objectContaining({ createdBy: 'Rock', gameType: 'Fibonacci', name: 'Marvels' }),
expect.objectContaining({
createdBy: 'Rock',
gameType: 'Fibonacci',
name: 'Marvels',
isAllowMembersToManageSession: false,
}),
);
});
it('should be able to create new session with Allow members to manage session', async () => {
render(<CreateGame />);
const sessionName = screen.getByPlaceholderText('Enter a session name');
userEvent.clear(sessionName);
userEvent.type(sessionName, 'Marvels');

const userName = screen.getByPlaceholderText('Enter your name');
userEvent.clear(userName);
userEvent.type(userName, 'Rock');

const allowMembersToManageSession = screen.getByText('Allow members to manage session');
userEvent.click(allowMembersToManageSession);

const createButton = screen.getByText('Create');
userEvent.click(createButton);

expect(gamesService.addNewGame).toHaveBeenCalled();

expect(gamesService.addNewGame).toHaveBeenCalledWith(
expect.objectContaining({
createdBy: 'Rock',
gameType: 'Fibonacci',
name: 'Marvels',
isAllowMembersToManageSession: true,
}),
);
});
it('should be able to create new session of TShirt Sizing', async () => {
Expand Down
13 changes: 13 additions & 0 deletions src/components/Poker/CreateGame/CreateGame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CardActions,
CardContent,
CardHeader,
Checkbox,
FormControlLabel,
Grow,
Radio,
Expand Down Expand Up @@ -35,6 +36,7 @@ export const CreateGame = () => {
const [gameType, setGameType] = useState(GameType.Fibonacci);
const [hasDefaults, setHasDefaults] = useState({ game: true, name: true });
const [loading, setLoading] = useState(false);
const [allowMembersToManageSession, setAllowMembersToManageSession] = useState(false);
const [customOptions, setCustomOptions] = React.useState(Array(10).fill(''));
const [error, setError] = React.useState(false);
const { t } = useTranslation();
Expand All @@ -56,6 +58,7 @@ export const CreateGame = () => {
name: gameName,
createdBy: createdBy,
gameType: gameType,
isAllowMembersToManageSession: allowMembersToManageSession,
cards: gameType === GameType.Custom ? getCustomCards(customOptions) : getCards(gameType),
createdAt: new Date(),
};
Expand Down Expand Up @@ -183,6 +186,16 @@ export const CreateGame = () => {
)}
</>
)}
<FormControlLabel
control={
<Checkbox
color='primary'
checked={allowMembersToManageSession}
onChange={() => setAllowMembersToManageSession(!allowMembersToManageSession)}
/>
}
label='Allow members to manage session'
/>
</CardContent>
<CardActions className='CreateGameCardAction'>
<Button
Expand Down
2 changes: 1 addition & 1 deletion src/components/Poker/GameController/GameController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const GameController: React.FC<GameControllerProps> = ({ game, currentPla
className='GameControllerCardTitle'
></CardHeader>
<CardContent className='GameControllerCardContentArea'>
{isModerator(game.createdById, currentPlayerId) && (
{isModerator(game.createdById, currentPlayerId, game.isAllowMembersToManageSession) && (
<>
<div className='GameControllerButtonContainer'>
<div className='GameControllerButton'>
Expand Down
8 changes: 6 additions & 2 deletions src/components/Poker/RecentGames/RecentGames.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ export const RecentGames = () => {
>
<TableCell>{recentGame.name}</TableCell>
<TableCell align='left'>{recentGame.createdBy}</TableCell>
{isModerator(recentGame.createdById, getCurrentPlayerId(recentGame.id)) ? (
{isModerator(
recentGame.createdById,
getCurrentPlayerId(recentGame.id),
recentGame.isAllowMembersToManageSession,
) ? (
<TableCell align='center' onClick={(e) => e.stopPropagation()}>
<AlertDialog
title='Remove recent game'
Expand All @@ -103,7 +107,7 @@ export const RecentGames = () => {
<TableCell align='left'></TableCell>
)}
</TableRow>
)
),
)}
</TableBody>
</Table>
Expand Down
2 changes: 2 additions & 0 deletions src/types/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface Game {
average: number;
gameStatus: Status;
gameType?: GameType | GameType.Fibonacci;
isAllowMembersToManageSession?: boolean;
cards: CardConfig[];
createdBy: string;
createdById: string;
Expand All @@ -18,6 +19,7 @@ export interface NewGame {
name: string;
gameType: string;
cards: CardConfig[];
isAllowMembersToManageSession?: boolean;
createdBy: string;
createdAt: Date;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Player {
export interface PlayerGame {
id: string;
name: string;
isAllowMembersToManageSession?: boolean;
createdById: string;
createdBy: string;
playerId: string;
Expand Down
51 changes: 51 additions & 0 deletions src/utils/isModerator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { isModerator } from './isModerator';

describe('isModerator', () => {
it('should return true if the currentPlayerId matches the moderatorId', () => {
const moderatorId = 'moderator123';
const currentPlayerId = 'moderator123';
const isAllowMembersToManageSession = false;

const result = isModerator(moderatorId, currentPlayerId, isAllowMembersToManageSession);

expect(result).toBe(true);
});

it('should return false if the currentPlayerId does not match the moderatorId', () => {
const moderatorId = 'moderator123';
const currentPlayerId = 'player456';
const isAllowMembersToManageSession = false;

const result = isModerator(moderatorId, currentPlayerId, isAllowMembersToManageSession);

expect(result).toBe(false);
});

it('should return false if isAllowMembersToManageSession is false', () => {
const moderatorId = 'moderator123';
const currentPlayerId = 'player456';
const isAllowMembersToManageSession = false;

const result = isModerator(moderatorId, currentPlayerId, isAllowMembersToManageSession);

expect(result).toBe(false);
});
it('should return false if isAllowMembersToManageSession is undefined', () => {
const moderatorId = 'moderator123';
const currentPlayerId = 'player456';
const isAllowMembersToManageSession = undefined;

const result = isModerator(moderatorId, currentPlayerId, isAllowMembersToManageSession);

expect(result).toBe(false);
});
it('should return true if isAllowMembersToManageSession is true', () => {
const moderatorId = 'moderator123';
const currentPlayerId = 'moderator123';
const isAllowMembersToManageSession = true;

const result = isModerator(moderatorId, currentPlayerId, isAllowMembersToManageSession);

expect(result).toBe(true);
});
});
11 changes: 9 additions & 2 deletions src/utils/isModerator.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
export const isModerator = (moderatorId: string, currentPlayerId: string | undefined) => {
export const isModerator = (
moderatorId: string,
currentPlayerId: string | undefined,
isAllowMembersToManageSession: boolean | undefined,
) => {
if (isAllowMembersToManageSession) {
return true;
}
return moderatorId === currentPlayerId;
};
};

0 comments on commit 2b93916

Please sign in to comment.