Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to toggle group member visibility #300

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/updateGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PlayerGroup, playerGroupSchema } from '../entities/playerGroup'
import api from './api'
import makeValidatedRequest from './makeValidatedRequest'

type Data = Pick<PlayerGroup, 'name' | 'description' | 'rules' | 'ruleMode'>
type Data = Pick<PlayerGroup, 'name' | 'description' | 'rules' | 'ruleMode' | 'membersVisible'>

const updateGroup = makeValidatedRequest(
(gameId: number, groupId: string, data: Data) => api.put(`/games/${gameId}/player-groups/${groupId}`, data),
Expand Down
1 change: 1 addition & 0 deletions src/entities/playerGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const playerGroupSchema = z.object({
rules: z.array(playerGroupRuleSchema),
ruleMode: z.nativeEnum(PlayerGroupRuleMode),
count: z.number(),
membersVisible: z.boolean(),
updatedAt: z.string().datetime()
})

Expand Down
4 changes: 2 additions & 2 deletions src/modals/IntegrationDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export default function IntegrationDetails({

<div className='flex justify-between items-start'>
<div>
<p className='font-medium'>Sync leaderboards</p>
<label htmlFor='sync-leaderboards' className='font-semibold'>Sync leaderboards</label>
<p className='text-sm text-gray-500'>Push leaderboard entries to Steam</p>
</div>
<div className='flex items-center space-x-4'>
Expand All @@ -258,7 +258,7 @@ export default function IntegrationDetails({

<div className='flex justify-between items-start'>
<div>
<p className='font-medium'>Sync stats</p>
<label htmlFor='sync-stats' className='font-semibold'>Sync stats</label>
<p className='text-sm text-gray-500'>Push individual and global values to Steam</p>
</div>
<div className='flex items-center space-x-4'>
Expand Down
35 changes: 31 additions & 4 deletions src/modals/groups/GroupDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import TextInput from '../../components/TextInput'
import Button from '../../components/Button'
import buildError from '../../utils/buildError'
import ErrorMessage, { TaloError } from '../../components/ErrorMessage'
import { SubmitHandler, useForm } from 'react-hook-form'
import { Controller, SubmitHandler, useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import createGroup from '../../api/createGroup'
import activeGameState, { SelectedActiveGame } from '../../state/activeGameState'
Expand All @@ -24,10 +24,13 @@ import { metaGroupFields } from '../../constants/metaProps'
import { PlayerGroup, PlayerGroupRule, PlayerGroupRuleCastType, PlayerGroupRuleName, PlayerGroupRuleMode } from '../../entities/playerGroup'
import { KeyedMutator } from 'swr'
import { z } from 'zod'
import Toggle from '../../components/toggles/Toggle'
import Link from '../../components/Link'

const validationSchema = z.object({
name: z.string(),
description: z.string()
description: z.string(),
membersVisible: z.boolean()
})

type FormValues = z.infer<typeof validationSchema>
Expand Down Expand Up @@ -101,11 +104,12 @@ export default function GroupDetails({
return rules.every(isGroupRuleValid)
}, [rules])

const { register, handleSubmit, formState: { isValid, errors } } = useForm<FormValues>({
const { register, handleSubmit, formState: { isValid, errors }, control } = useForm<FormValues>({
resolver: zodResolver(validationSchema),
defaultValues: {
name: editingGroup?.name ?? '',
description: editingGroup?.description ?? ''
description: editingGroup?.description ?? '',
membersVisible: editingGroup?.membersVisible ?? false
},
mode: 'onChange'
})
Expand Down Expand Up @@ -213,6 +217,29 @@ export default function GroupDetails({
errors={[errors.description?.message]}
/>

<Controller
control={control}
name='membersVisible'
render={({
field: { onChange, value, ref }
}) => (
<div className='flex justify-between items-start'>
<div>
<label htmlFor='members-visible' className='font-semibold'>Members visible</label>
<p className='text-sm text-gray-500'>If enabled, player data will be returned in the <Link to='https://docs.trytalo.com/docs/http/player-group-api'>player group API</Link></p>
</div>
<div className='flex items-center space-x-4'>
<Toggle
id='members-visible'
inputRef={ref}
enabled={value}
onToggle={onChange}
/>
</div>
</div>
)}
/>

<div>
<p className='font-semibold mb-2'>Membership</p>
<GroupRules
Expand Down
8 changes: 8 additions & 0 deletions src/modals/groups/__tests__/GroupDetails.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('<GroupDetails />', () => {
rules: [],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}
]
Expand Down Expand Up @@ -132,6 +133,7 @@ describe('<GroupDetails />', () => {
],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: true,
updatedAt: new Date().toISOString()
}}
/>
Expand All @@ -140,6 +142,7 @@ describe('<GroupDetails />', () => {

expect(screen.getByLabelText('Name')).toHaveValue('Winners')
expect(screen.getByLabelText('Description')).toHaveValue('Players who have won the game')
expect(screen.getByLabelText('Members visible')).toBeEnabled()

expect(screen.getByText('Update')).toBeInTheDocument()
})
Expand All @@ -158,6 +161,7 @@ describe('<GroupDetails />', () => {
],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}

Expand Down Expand Up @@ -215,6 +219,7 @@ describe('<GroupDetails />', () => {
rules: [],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}}
/>
Expand All @@ -240,6 +245,7 @@ describe('<GroupDetails />', () => {
rules: [],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}

Expand Down Expand Up @@ -281,6 +287,7 @@ describe('<GroupDetails />', () => {
rules: [],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}

Expand Down Expand Up @@ -320,6 +327,7 @@ describe('<GroupDetails />', () => {
rules: [],
ruleMode: PlayerGroupRuleMode.AND,
count: 0,
membersVisible: false,
updatedAt: new Date().toISOString()
}}
/>
Expand Down