Skip to content

Commit a8ecf51

Browse files
authored
Fixed hiddenFor field on Gig (#316)
* Fixed hiddenFor field in Gig * Added comment
1 parent b15d645 commit a8ecf51

3 files changed

Lines changed: 45 additions & 27 deletions

File tree

src/components/gig/form.tsx

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import NumberInput from 'components/input/number-input';
1515
import Checkbox from 'components/input/checkbox';
1616
import DatePicker from 'components/input/date-picker';
1717
import DateTimePicker from 'components/input/datetime-picker';
18+
import { api } from 'trpc/react';
19+
import MultiSelect from 'components/multi-select';
20+
import { lang } from 'utils/language';
21+
import { detailedName } from 'utils/corps';
1822

1923
interface GigFormProps {
2024
gig?: Gig & { type: { name: string } } & { hiddenFor: { corpsId: string }[] };
@@ -51,17 +55,16 @@ const GigForm = ({ gig, gigTypes }: GigFormProps) => {
5155

5256
const router = useRouter();
5357

54-
// const { data: corpsii } = api.corps.search.useQuery({});
55-
// const corpsiiOptions = corpsii?.map((c) => ({
56-
// label:
57-
// (c.number ? `#${c.number}` : 'p.e.') +
58-
// ' ' +
59-
// c.firstName +
60-
// ' ' +
61-
// (c.nickName ? '"' + c.nickName + '" ' : '') +
62-
// c.lastName,
63-
// value: c.id,
64-
// }));
58+
// Only fetch corps if user presses the button or if hiddenFor is populated
59+
// to prevent having to fetch all corps every time. The fetch is about 30KB
60+
// (as of 2025)
61+
const [hideFor, setHideFor] = useState((gig?.hiddenFor.length ?? 0) !== 0);
62+
const { data: corpsii } = api.corps.search.useQuery({}, { enabled: hideFor });
63+
64+
const corpsiiOptions = corpsii?.map((c) => ({
65+
label: detailedName(c),
66+
value: c.id,
67+
}));
6568

6669
const form = useForm<FormValues>({
6770
initialValues: newGig
@@ -220,18 +223,31 @@ const GigForm = ({ gig, gigTypes }: GigFormProps) => {
220223
description='Lämna tom för att inte visa kryssruta'
221224
{...form.getInputProps('checkbox2')}
222225
/>
223-
{/*
224-
<div className='col-span-1 flex flex-col focus-visible:ring-red-600 md:col-span-2'>
225-
<div>Dölj spelning</div>
226-
<MultiSelect
227-
placeholder='Välj corps...'
228-
className='outline-none focus-visible:outline-none'
229-
options={corpsiiOptions ?? []}
230-
defaultValue={form.values.hiddenFor}
231-
{...form.getInputProps('hiddenFor')}
232-
/>
233-
</div>
234-
*/}
226+
<div className='col-span-1 flex flex-col focus-visible:ring-red-600 md:col-span-2'>
227+
{hideFor ? (
228+
<>
229+
<div>{lang('Dölj spelning för', 'Hide gig from')}</div>
230+
<MultiSelect
231+
placeholder='Välj corps...'
232+
className='outline-none focus-visible:outline-none'
233+
options={corpsiiOptions ?? []}
234+
defaultValue={form.values.hiddenFor}
235+
{...form.getInputProps('hiddenFor')}
236+
/>
237+
</>
238+
) : (
239+
<Button
240+
color='no-fill'
241+
compact
242+
fullWidth
243+
onClick={() => {
244+
setHideFor(!hideFor);
245+
}}
246+
>
247+
{lang('Dölj spelning för corps...', 'Hide gig from corps...')}{' '}
248+
</Button>
249+
)}
250+
</div>
235251
<div className='flex space-x-4 whitespace-nowrap'>
236252
<Checkbox
237253
label='Allmän spelning?'

src/components/input/button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ export type ButtonProps = ButtonHTMLAttributes<HTMLButtonElement> & {
99
fullWidth?: boolean;
1010
};
1111

12-
type Color = 'red' | 'transparent' | 'navbutton';
12+
type Color = 'red' | 'transparent' | 'navbutton' | 'no-fill';
1313

1414
const colorClasses: Record<Color, string> = {
1515
red: 'bg-red-600 hover:bg-red-700 text-white',
1616
transparent: 'bg-transparent text-gray-700 dark:text-gray-300',
1717
navbutton: 'bg-transparent text-white',
18+
'no-fill': 'border-2 border-red-600 text-red-600',
1819
};
1920

2021
const Button = ({

src/server/trpc/router/gig.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export const gigRouter = router({
8989
dateOrder: z.enum(['asc', 'desc']).optional(),
9090
}),
9191
)
92-
.query(({ ctx, input }) => {
92+
.query(async ({ ctx, input }) => {
9393
const { startDate, endDate, dateOrder = 'asc' } = input;
9494
const corpsId = ctx.session?.user?.corps?.id;
9595
const visibilityFilter =
@@ -109,12 +109,12 @@ export const gigRouter = router({
109109
{
110110
// Stops hiding gig if it's in the past
111111
date: {
112-
gt: dayjs().startOf('day').toDate(),
112+
lt: dayjs().startOf('day').toDate(),
113113
},
114114
},
115115
],
116116
};
117-
return ctx.prisma.gig.findMany({
117+
const res = await ctx.prisma.gig.findMany({
118118
include: {
119119
type: {
120120
select: {
@@ -148,6 +148,7 @@ export const gigRouter = router({
148148
},
149149
],
150150
});
151+
return res;
151152
}),
152153

153154
upsert: restrictedProcedure('manageGigs')

0 commit comments

Comments
 (0)