Skip to content

Commit ae4c3ef

Browse files
authored
Show old nicknames when clicking nickname in infobox (#455)
1 parent 0c42b80 commit ae4c3ef

2 files changed

Lines changed: 75 additions & 4 deletions

File tree

src/components/corps/infobox.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { useRouter } from 'next/navigation';
1010
import { FormEvent, useState } from 'react';
1111
import { api } from 'trpc/react';
1212
import { filterNone } from 'utils/array';
13-
import { numberAndFullName } from 'utils/corps';
13+
import { displayName, numberAndFullName } from 'utils/corps';
1414
import { calcOperatingYearInterval, getOperatingYear } from 'utils/date';
1515
import { lang } from 'utils/language';
1616

@@ -120,6 +120,7 @@ const CorpsInfobox = ({
120120
const [showAllStreaks, setShowAllStreaks] = useState(false);
121121
const [newNickName, setNewNickName] = useState('');
122122
const [nickNameModalOpen, setNickNameModalOpen] = useState(false);
123+
const [showAllNicknames, setShowAllNicknames] = useState(false);
123124

124125
const dateFormatter = DATE_FORMATTERS[language];
125126

@@ -151,6 +152,7 @@ const CorpsInfobox = ({
151152
firstGigDate,
152153
firstRehearsalDate,
153154
lastSeenAt,
155+
givenNicknames,
154156
} = corps;
155157
const mainInstrument =
156158
instruments.find((i) => i.isMainInstrument)?.instrument.name ?? '';
@@ -309,9 +311,30 @@ const CorpsInfobox = ({
309311
)}
310312
</div>
311313
{(nickName || pronouns) && (
312-
<div className='mb-1 bg-transparent text-xs font-light text-neutral-500'>
313-
{filterNone([nickName, pronouns]).join(' • ')}
314-
</div>
314+
<>
315+
<div
316+
className='mb-1 bg-transparent text-xs font-light text-neutral-500'
317+
onClick={() => {
318+
setShowAllNicknames(!showAllNicknames);
319+
}}
320+
>
321+
{filterNone([nickName, pronouns]).join(' • ')}
322+
</div>
323+
{showAllNicknames && givenNicknames.length > 1 && (
324+
<div className='mb-1 flex flex-col gap-1 bg-transparent text-xs font-light text-neutral-500'>
325+
{lang('Också varit känd som:', 'Also been known as:')}
326+
{givenNicknames.map((nickname) => (
327+
<div
328+
key={`${corps.id}-nickname:${nickname.nickname}`}
329+
>{`${nickname.nickname.trim()}, ${
330+
language === 'sv' ? 'givet av' : 'given by'
331+
} ${displayName(nickname.createdBy)} ${
332+
language === 'sv' ? 'den' : 'at'
333+
} ${dateFormatter.format(nickname.createdAt)}.`}</div>
334+
))}
335+
</div>
336+
)}
337+
</>
315338
)}
316339
</div>
317340
<div className='italic'>

src/server/trpc/router/corps.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,26 @@ export const corpsRouter = router({
107107
},
108108
take: 1,
109109
},
110+
givenNicknames: {
111+
select: {
112+
createdBy: {
113+
select: {
114+
number: true,
115+
bNumber: true,
116+
firstName: true,
117+
nickName: true,
118+
lastName: true,
119+
displayName: true,
120+
},
121+
},
122+
createdAt: true,
123+
nickname: true,
124+
},
125+
orderBy: {
126+
createdAt: 'desc',
127+
},
128+
distinct: ['nickname'],
129+
},
110130
},
111131
where: {
112132
id: input.id,
@@ -290,6 +310,16 @@ export const corpsRouter = router({
290310
});
291311
}
292312

313+
if (nickName && nickName !== corps.nickName) {
314+
await ctx.prisma.nickname.create({
315+
data: {
316+
nickname: nickName,
317+
createdById: corps.id,
318+
forId: corps.id,
319+
},
320+
});
321+
}
322+
293323
return ctx.prisma.corps.update({
294324
where: {
295325
id: corps.id,
@@ -379,6 +409,24 @@ export const corpsRouter = router({
379409
corpsId: input.id,
380410
},
381411
});
412+
const oldNickname = await ctx.prisma.corps.findUniqueOrThrow({
413+
where: {
414+
id: input.id,
415+
},
416+
select: {
417+
nickName: true,
418+
},
419+
});
420+
421+
if (input.nickName !== oldNickname.nickName) {
422+
await ctx.prisma.nickname.create({
423+
data: {
424+
nickname: input.nickName,
425+
createdById: ctx.session.user.corps.id,
426+
forId: input.id,
427+
},
428+
});
429+
}
382430
}
383431

384432
const res = await ctx.prisma.corps.upsert({

0 commit comments

Comments
 (0)