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

Discord Id Check #69

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

1. Copy the Client ID and update the `DISCORD_CLIENT_ID` value.

2. Reveal and copy the Client Secret and update the `DISCORD_SECRET` value.
2. Reveal and copy the Client Secret and update the `DISCORD_SECRET_ID` value.

## Setting up Discord Server

Expand Down
61 changes: 61 additions & 0 deletions packages/web/src/components/DiscordButton/DiscordButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { Button, Box, Alert, AlertIcon } from '@chakra-ui/react';
import { FaDiscord } from 'react-icons/fa';
import { User } from '../userprofile/UserProfile';

export const DiscordButton: React.FC = () => {
const [user, setUser] = React.useState<User | undefined>();
const [errorMessage, setErrorMessage] = React.useState<string>('');

React.useEffect(() => {
const fetchUser = async () => {
try {
const res = await fetch(`/api/users/me`);
const data = await res.json();

setUser(data);
} catch {
setErrorMessage('User could not be found');
}
};

fetchUser();
}, []);

if (!user) {
if (errorMessage == '') return null;
else
return (
<Alert status="error">
<AlertIcon />
{errorMessage}
</Alert>
);
} else {
return (
<Box>
{user.discordId ? (
<Button
as="a"
href="https://discord.com/"
bg="discord.400"
textColor="white"
leftIcon={<FaDiscord color="white" />}
>
Go to Discord
</Button>
) : (
<Button
as="a"
href="/api/auth/discord/login"
bg="discord.400"
textColor="white"
leftIcon={<FaDiscord color="white" />}
>
Join our Discord
</Button>
)}
</Box>
);
}
};
1 change: 1 addition & 0 deletions packages/web/src/components/DiscordButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './DiscordButton';
2 changes: 1 addition & 1 deletion packages/web/src/components/Layout/MarketingLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Button, Link, Heading, HStack, Spacer, VStack } from '@chakra-ui/react';
import { Button, Heading, HStack, Spacer, VStack } from '@chakra-ui/react';
import { GiCrossFlare } from 'react-icons/gi';

export const MarketingLayout: React.FC = ({ children }) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/web/src/components/userprofile/UserProfile.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { useRouter } from 'next/router';
import { Heading, HStack, Spacer, useTheme, VStack, Text, Box, Button } from '@chakra-ui/react';
import { Text, Box, Button } from '@chakra-ui/react';

interface User {
export interface User {
name: string;
pronouns?: string;
schoolName?: string;
Expand All @@ -29,7 +29,7 @@ export const UserProfile: React.FC<UserProfileProps> = ({ user, setUser }: UserP
'content-type': 'application/json',
},
body: JSON.stringify({
discordId: undefined,
discordId: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

Why was this change made? null doesn't match the type above (string in conjunction with ? becomes string | undefined which is not the same as string | null)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was made because I don't think the database knows what undefined was. Unless we change it to null the discord ID is not actually removed from the profile. it just selects the profile but doesn't actually remove the Discord ID.

}),
});
if (!res.ok) {
Expand Down
8 changes: 2 additions & 6 deletions packages/web/src/pages/app/community.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from '@chakra-ui/react';
import { NextPage } from 'next';
import React from 'react';
import { FaDiscord } from 'react-icons/fa';
import { DiscordButton } from '../../components/DiscordButton';
import { AppLayout } from '../../components/Layout';

const Community: NextPage = () => {
Expand Down Expand Up @@ -91,11 +91,7 @@ const Community: NextPage = () => {
</Accordion>
<HStack>
<Spacer />
<Button bg="discord.400" textColor="white" leftIcon={<FaDiscord color="white" />}>
<Link _hover={{ textDecoration: 'none' }} href="https://discord.gg">
Join our Discord
</Link>
</Button>
<DiscordButton />
<Spacer />
</HStack>
</AppLayout>
Expand Down
60 changes: 60 additions & 0 deletions packages/web/tests/components/DiscordButton/DiscordButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from 'react';
import fetchMock from 'fetch-mock-jest';
import { DiscordButton } from '../../../src/components/DiscordButton';
import { render, screen, waitFor } from '../../testUtils/testTools';
import { User } from '../../../src/components/userprofile/UserProfile';

const sampleUser: Partial<User> = {
name: 'James',
discordId: undefined,
};

const sampleUser2: User = {
name: 'James',
discordId: '34523452345',
};

describe('DiscordButton component', () => {
beforeEach(async () => {
jest.clearAllMocks();
});

it('shows an error message when the user is not found', async () => {
fetchMock.get('/api/users/me', 500);

render(<DiscordButton />);

await waitFor(() => {
expect(fetchMock).toHaveFetched('/api/users/me');
expect(screen.getByText('User could not be found')).toBeVisible();
});
});

it('renders the Join Discord button when user has no discord id', async () => {
fetchMock.get('/api/users/me', sampleUser);

render(<DiscordButton />);

await waitFor(() => {
const JoinDiscord = screen.queryByText('Join our Discord');
expect(fetchMock).toHaveFetched('/api/users/me');

expect(JoinDiscord).toBeVisible();
expect(JoinDiscord).toHaveAttribute('href', '/api/auth/discord/login');
});
});

it('renders Go to Discord button when user has a discordId', async () => {
fetchMock.get('/api/users/me', sampleUser2);

render(<DiscordButton />);

await waitFor(() => {
const GoToDiscord = screen.queryByText('Go to Discord');
expect(fetchMock).toHaveFetched('/api/users/me');

expect(GoToDiscord).toBeVisible();
expect(GoToDiscord).toHaveAttribute('href', 'https://discord.com/');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { sample } from 'lodash';
import React from 'react';
import userEvent from '@testing-library/user-event';
import fetchMock from 'fetch-mock-jest';
Expand Down
10 changes: 6 additions & 4 deletions packages/web/tests/pages/app/community.test.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import React from 'react';

import { render, screen, waitFor } from '../../testUtils/testTools';
import { getMock } from '../../testUtils/getMock';
import { AppLayout } from '../../../src/components/Layout';
import Community from '../../../src/pages/app/community';
import { DiscordButton } from '../../../src/components/DiscordButton/DiscordButton';

jest.mock('../../../src/components/Layout/AppLayout.tsx');
getMock(AppLayout).mockImplementation(({ children }) => <>{children}</>);

jest.mock('../../../src/components/DiscordButton/DiscordButton.tsx');
const discordText = 'discordButton';
getMock(DiscordButton).mockImplementation(() => <p>{discordText}</p>);

describe('community page', () => {
beforeEach(async () => {
jest.clearAllMocks();
Expand All @@ -25,8 +29,6 @@ describe('community page', () => {
screen.queryByText('Remember to follow the Community Guidelines listed above.'),
).not.toBeVisible();
expect(screen.queryByText('Should I change my Discord profile avatar?')).not.toBeVisible();
expect(screen.queryByText('Join our Discord')).toBeVisible();
expect(screen.queryByText('Join our Discord')).toHaveAttribute('href', 'https://discord.gg');
expect(screen.queryByText('here')).toHaveAttribute('href', '/api/auth/discord/login');
expect(screen.queryByText(discordText)).toBeVisible();
});
});