Skip to content
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
15 changes: 15 additions & 0 deletions app/models/league.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ export async function getLeaguesByYear(year: League['year']) {
});
}

export async function getLeagueCountsByYear() {
return prisma.league.groupBy({
by: ['year'],
_count: { _all: true },
});
}

export async function getLeagueCountForYear(year: League['year']) {
return prisma.league.count({
where: {
year,
},
});
}

export async function updateLeague(league: Partial<League>) {
return prisma.league.update({
where: {
Expand Down
8 changes: 8 additions & 0 deletions app/models/season.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export async function updateSeason(season: Partial<Season>) {
});
}

export async function deleteSeason(id: Season['id']) {
return prisma.season.delete({
where: {
id,
},
});
}

export async function updateActiveSeason(id: Season['id']) {
return prisma.$transaction([
prisma.season.updateMany({
Expand Down
74 changes: 70 additions & 4 deletions app/routes/admin.season._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import {
} from 'remix-typedjson';
import Alert from '~/components/ui/Alert';
import Button from '~/components/ui/FlexSpotButton';
import {
getLeagueCountForYear,
getLeagueCountsByYear,
} from '~/models/league.server';
import {
createSeason,
deleteSeason,
getSeasonById,
getSeasons,
updateActiveSeason,
updateSeason,
Expand All @@ -30,8 +36,7 @@ export const action = async ({ request }: ActionFunctionArgs) => {

switch (action) {
case 'createSeason': {
//const year = new Date().getFullYear();
const year = new Date('2024-04-01').getFullYear();
const year = new Date().getFullYear();
const season = await createSeason({
year,
isCurrent: false,
Expand All @@ -45,6 +50,36 @@ export const action = async ({ request }: ActionFunctionArgs) => {
message: `${season.year} season has been created`,
});
}
case 'deleteSeason': {
const seasonId = formData.get('seasonId');
if (typeof seasonId !== 'string') {
throw new Error(`Form not generated correctly.`);
}

const season = await getSeasonById(seasonId);
if (!season) {
return typedjson({ message: 'Season not found.' });
}

if (season.isCurrent) {
return typedjson({
message: `Cannot delete the active season.`,
});
}

const leagueCount = await getLeagueCountForYear(season.year);
if (leagueCount > 0) {
return typedjson({
message: `Cannot delete a season that has leagues attached.`,
});
}

await deleteSeason(seasonId);

return typedjson({
message: `${season.year} season has been deleted`,
});
}
case 'setActive': {
const seasonId = formData.get('seasonId');
if (typeof seasonId !== 'string') {
Expand Down Expand Up @@ -123,11 +158,17 @@ export const loader = async ({ request }: LoaderFunctionArgs) => {

const seasons = await getSeasons();

return typedjson({ seasons });
const leagueCounts = await getLeagueCountsByYear();
const leagueCountsByYear: Record<number, number> = {};
for (const { year, _count } of leagueCounts) {
leagueCountsByYear[year] = _count._all;
}

return typedjson({ seasons, leagueCountsByYear });
};

export default function SeasonIndex() {
const { seasons } = useTypedLoaderData<typeof loader>();
const { seasons, leagueCountsByYear } = useTypedLoaderData<typeof loader>();
const actionData = useTypedActionData<typeof action>();
const navigation = useNavigation();

Expand All @@ -139,6 +180,7 @@ export default function SeasonIndex() {
<thead>
<tr>
<th>Year</th>
<th>Leagues</th>
<th>Active?</th>
<th>Open Registration?</th>
<th>Open F²?</th>
Expand All @@ -157,9 +199,12 @@ export default function SeasonIndex() {
isOpenForDFSSurvivor,
} = season;

const leagueCount = leagueCountsByYear[year] ?? 0;

return (
<tr key={id}>
<td>{year}</td>
<td>{leagueCount}</td>
<td>{isCurrent ? 'Yes' : 'No'}</td>
<td>{isOpenForRegistration ? 'Yes' : 'No'}</td>
<td>{isOpenForFSquared ? 'Yes' : 'No'}</td>
Expand All @@ -173,6 +218,27 @@ export default function SeasonIndex() {
</Button>
</Form>
)}
{!isCurrent && leagueCount === 0 && (
<Form method='POST' style={{ display: 'inline' }}>
<input type='hidden' name='seasonId' value={id} />
<Button
type='submit'
name='_action'
value='deleteSeason'
onClick={e => {
if (
!confirm(
'Are you sure you want to delete this season?',
)
) {
e.preventDefault();
}
}}
>
Delete
</Button>
</Form>
)}
{isCurrent && (
<>
<Form method='POST'>
Expand Down
Loading