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

Alex/implement admin league operation #489

Merged
merged 51 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
18970b1
update package.json to latest release version
chris-nowicki Aug 29, 2024
8bafcb7
Merge branch 'develop' into alex/implement-admin-league-operation
alexappleget Aug 30, 2024
ba09e35
Fix: Added in functionality to create a new league.
alexappleget Sep 3, 2024
20072fe
Fix: Added in functionality for deleting a league.
alexappleget Sep 3, 2024
3357249
Fix: Changed function name for creating a new league in page.tsx
alexappleget Sep 3, 2024
2b443a1
Fix: Added the getCurrentLeague function to the page file for grabbin…
alexappleget Sep 3, 2024
08095de
Fix: Handled param for handleGetLeague().
alexappleget Sep 3, 2024
033c33a
Fix: added <tbody> parent element to data table because of hydration …
alexappleget Sep 3, 2024
697a7af
Fix: Adding in data table for league data.
alexappleget Sep 3, 2024
01cd943
Merge branch 'develop' into alex/implement-admin-league-operation
alexappleget Oct 10, 2024
1fe5867
Fix: Cleaned up page.tsx for Admin League Operations.
alexappleget Oct 10, 2024
db19193
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 15, 2024
da1d086
Nit Fix: Alphabetized the column headers
alexappleget Oct 15, 2024
00bab8c
Fix: Cleaned up page.tsx and added in <TableData /> component.
alexappleget Oct 15, 2024
d5f1eb7
Fix: Deleted apiFunctions and interface props as they aren't within t…
alexappleget Oct 15, 2024
6ae8dcd
Fix: Cleaned up column keys/headers for LeagueHeader for readability.
alexappleget Oct 15, 2024
80a8505
Fix: Fetched user league data and tweaked table column functionality …
alexappleget Oct 15, 2024
0036b06
Fix: Creating apiFunction to grab all entries in the leagues a user i…
alexappleget Oct 15, 2024
13d2a75
Fix: updated function to grab entries length.
alexappleget Oct 16, 2024
8d900d7
Fix: Got it working to show total entries in each league.
alexappleget Oct 16, 2024
7a0d5f4
Fix: Got data table to render total entries and entries alive. Also s…
alexappleget Oct 16, 2024
a3b6742
Fix: Deleted the utils function I created.
alexappleget Oct 16, 2024
f31c3b9
Fix: created interface file for new column props.
alexappleget Oct 16, 2024
5b7e609
Fix: alphabetized combinedData props.
alexappleget Oct 16, 2024
2d93d72
Fix: alphabetized things that I can in the page.tsx file for admin le…
alexappleget Oct 16, 2024
62873d3
Fix: alphabetized imports in TableColumns
alexappleget Oct 16, 2024
9939f20
Fix: creating api test for the getAllLeagueEntries();
alexappleget Oct 16, 2024
d621df0
Fix: fixed testing for getAllLeagueEntries()
alexappleget Oct 16, 2024
1559c7d
Fix: fixed testing
alexappleget Oct 16, 2024
798ff07
Fix: made page.tsx testing that passes but has an error.
alexappleget Oct 16, 2024
fea5080
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 16, 2024
a0c3859
Fix: deleted comment code
alexappleget Oct 17, 2024
4017afd
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 17, 2024
8f8258a
Fix: created test for getAllLeagueEntries() apiFunction
alexappleget Oct 17, 2024
d2f88e4
Fix: Fixed api function testing for getAllLeagueEntries.
alexappleget Oct 17, 2024
a894326
Fix: created testing for admin league page
alexappleget Oct 17, 2024
1f6048e
Fix: added sorting code
alexappleget Oct 17, 2024
6572029
Fix: got rid of trycatch{} block.
alexappleget Oct 18, 2024
2e8b5ae
Fix: handled comment by changing console.error to throw new Error in …
alexappleget Oct 21, 2024
8f62f60
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 24, 2024
36f4803
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 27, 2024
6a7650a
Fix: Handled Shashi's comment about apiFunction error code.
alexappleget Oct 28, 2024
adffce8
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Oct 31, 2024
fa72e54
Fix: changed divs to p tags.
alexappleget Oct 31, 2024
d2aa3b2
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Nov 6, 2024
31a41c4
Fix: changed <p> tags to <span> tags
alexappleget Nov 11, 2024
7436de1
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Nov 11, 2024
3134021
Fix: changed getAllLeagueEntries apiFunction name to getTotalEntries
alexappleget Nov 18, 2024
f301ee9
Fix: fixed testing
alexappleget Nov 18, 2024
b26a582
Fix: changed test name to match new function name
alexappleget Nov 18, 2024
7730fb5
Merge remote-tracking branch 'origin/develop' into alex/implement-adm…
alexappleget Nov 21, 2024
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
67 changes: 67 additions & 0 deletions api/apiFunctions.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
resetPassword,
resetRecoveredPassword,
updateUserEmail,
getTotalEntries
} from './apiFunctions';
import { IUser } from './apiFunctions.interface';
import { account, databases, ID } from './config';
Expand Down Expand Up @@ -497,4 +498,70 @@ describe('apiFunctions', () => {
);
});
});

describe('getTotalEntries()', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should return the total number of entries and alive entries in a league', async () => {
(databases.listDocuments as jest.Mock).mockResolvedValue({
documents: [
{
name: 'Entry 1',
user: '1234',
league: {
$id: 'league1',
survivors: ['123'],
participants: ['123', '1234'],
leagueName: 'League 1',
logo: '',
},
eliminated: true,
selectedTeams: ['Browns', 'Bears'],
},
{
name: 'Entry 2',
user: '1234',
league: {
$id: 'league1',
survivors: ['123'],
participants: ['123', '1234'],
leagueName: 'League 1',
logo: '',
},
eliminated: false,
selectedTeams: ['Patriots', 'Eagles'],
},
{
name: 'Entry 3',
user: '1234',
league: {
$id: 'league2',
survivors: ['123'],
participants: ['123', '1234'],
leagueName: 'League 2',
logo: '',
},
eliminated: false,
selectedTeams: ['Bears', 'Cowboys'],
},
],
});

const leagues = ['league1', 'league2'];

const result = await getTotalEntries({ leagues });

expect(result).toEqual([
{
totalEntries: 2,
alive: 1,
},
{
totalEntries: 1,
alive: 1,
},
]);
});
});
});
37 changes: 36 additions & 1 deletion api/apiFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,6 @@ export async function createEntry({
}

/**

* Update an entry
* @param props - The entry data
* @param props.entryId - The entry ID
Expand Down Expand Up @@ -498,3 +497,39 @@ export async function addUserToLeague({
throw new Error('Error getting user document ID', { cause: error });
}
}

/**
* Grabs the length of entries in each league.
* @param props - Props being passed in.
* @param props.leagues - All user leagues.
* @returns {Promise<number>} - Length of entries.
*/
export async function getTotalEntries({
leagues,
}: {
leagues: string[];
}): Promise<{ totalEntries: number; alive: number }[]> {
try {
const response = await databases.listDocuments(
appwriteConfig.databaseId,
Collection.ENTRIES,
[Query.limit(5000)],
);
const entries = leagues.map((leagueId) => {
const leagueEntries = response.documents.filter(
(entry) => entry.league.$id === leagueId,
);
const aliveEntries = leagueEntries.filter(
(aliveEntry) => aliveEntry.eliminated === false,
);
Comment on lines +519 to +524
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Future task, but we'll optimize this into 1 iteration.


return {
totalEntries: leagueEntries.length,
alive: aliveEntries.length,
};
});
return entries;
} catch (error) {
throw error;
}
}
46 changes: 41 additions & 5 deletions app/(admin)/admin/leagues/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,46 @@
import { render, screen } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import AdminLeagues from './page';
import { getUserLeagues } from '@/utils/utils';
import { getTotalEntries } from '@/api/apiFunctions';

jest.mock('@/store/dataStore', () => ({
useDataStore: jest.fn(() => ({
user: {
documentId: '123',
id: '1234',
email: '[email protected]',
leagues: ['league1', 'league2'],
},
})),
}));

jest.mock('@/utils/utils', () => ({
getUserLeagues: jest.fn(() => Promise.resolve([])),
cn: jest.fn(),
}));

jest.mock('@/api/apiFunctions', () => ({
getTotalEntries: jest.fn(),
}));

describe('AdminLeagues', () => {
it('should render the AdminLeagues page with LeagueCard components', () => {
render(<AdminLeagues />);
const leagueCards = screen.getAllByTestId('LeagueCard');
expect(leagueCards.length).toBe(3);
render(<AdminLeagues />);

const mockGetUserLeagues = getUserLeagues as jest.Mock;
const mockGetTotalEntries = getTotalEntries as jest.Mock;

beforeEach(() => {
jest.clearAllMocks();
});

it('should render the League data table component', async () => {
mockGetTotalEntries.mockResolvedValue([]);

mockGetUserLeagues.mockResolvedValue([]);

await waitFor(() => {
const leagueTable = screen.getByTestId('data-table');
expect(leagueTable).toBeInTheDocument();
});
});
});
54 changes: 34 additions & 20 deletions app/(admin)/admin/leagues/page.tsx
Original file line number Diff line number Diff line change
@@ -1,34 +1,48 @@
// Copyright (c) Gridiron Survivor.
// Licensed under the MIT License.

import { JSX } from 'react';
import { LeagueCard } from '@/components/LeagueCard/LeagueCard';
'use client';
import { getTotalEntries } from '@/api/apiFunctions';
import { getUserLeagues } from '@/utils/utils';
import { IEntryWithLeague } from '@/components/TableColumns/TableColumns.interface';
import { JSX, useEffect, useState } from 'react';
import { leagueColumns } from '@/components/TableColumns/TableColumns';
import TableData from '@/components/TableData/TableData';
import { useDataStore } from '@/store/dataStore';

/**
* Renders the admin page.
* @returns {JSX.Element} - The rendered Admin Leagues page.
*/
const AdminLeagues = (): JSX.Element => {
const [leaguesData, setLeaguesData] = useState<IEntryWithLeague[]>([]);
const { user } = useDataStore((state) => state);
choir241 marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get all leagues the user is a part of.
*/
const fetchData = async (): Promise<void> => {
choir241 marked this conversation as resolved.
Show resolved Hide resolved
const entries = await getTotalEntries({ leagues: user.leagues });
const leagues = await getUserLeagues(user.leagues);
const combinedData = leagues.map((league, index) => ({
aliveEntries: entries[index].alive,
leagueId: '',
leagueName: league.leagueName,
logo: '',
participants: league.participants,
survivors: league.survivors,
totalEntries: entries[index].totalEntries,
}));
setLeaguesData(combinedData);
};

useEffect(() => {
fetchData();
}, [user.leagues]);
choir241 marked this conversation as resolved.
Show resolved Hide resolved

return (
<section className="grid grid-cols-2 gap-6">
<LeagueCard
href={'#'}
survivors={20}
title={'Demo League 1'}
totalPlayers={30}
/>
<LeagueCard
href={'#'}
survivors={20}
title={'Demo League 2'}
totalPlayers={30}
/>
<LeagueCard
href={'#'}
survivors={20}
title={'Demo League 3'}
totalPlayers={30}
/>
<TableData columns={leagueColumns} data={leaguesData} />
</section>
);
};
Expand Down
1 change: 1 addition & 0 deletions components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Table = React.forwardRef<
ref={ref}
className={cn('table-auto border border-border rounded-md', className)}
{...props}
data-testid="data-table"
/>
));
Table.displayName = 'Table';
Expand Down
9 changes: 9 additions & 0 deletions components/TableColumns/TableColumns.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Gridiron Survivor.
// Licensed under the MIT License.

import { ILeague } from '@/api/apiFunctions.interface';

export interface IEntryWithLeague extends ILeague {
aliveEntries: number;
totalEntries: number;
}
Loading