Skip to content

Commit

Permalink
Cleaned up accent color logic and theming functions. Started theming …
Browse files Browse the repository at this point in the history
…manage users tab
  • Loading branch information
jacoblurie29 committed Sep 14, 2023
1 parent d27f7f9 commit d5ab4fc
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 53 deletions.
5 changes: 3 additions & 2 deletions components/Organizer/JudgingTab/JudgingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Skeleton } from 'antd';
import { TeamData, ScoreData, UserData } from '../../../types/database';
import { useCustomSWR, RequestType } from '../../../utils/request-utils';
import AllScores from './allScores';
import styles from '../../../styles/Organizer.module.css';

const JudgingTab = () => {
// Teams data
Expand Down Expand Up @@ -30,15 +31,15 @@ const JudgingTab = () => {
const dataNull = !teamsData || !scoresData || !judgeData;

return (
<>
<div className={styles.tab}>
{error ? (
<div>Failed to load data.</div>
) : dataNull ? (
<div>Loading...</div>
) : (
<AllScores teamData={teamsData} scoreData={scoresData} userData={judgeData} />
)}
</>
</div>
);
};

Expand Down
23 changes: 22 additions & 1 deletion components/Organizer/ManageUsersTab/ManageUsersTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@ import { Empty, Skeleton } from 'antd';
import { useSWRConfig } from 'swr';
import { useCustomSWR, RequestType } from '../../../utils/request-utils';
import ManageRoleForm, { ManageFormFields } from './manageRoleForm';
import { handleManageFormSubmit } from '../../../utils/organizer-utils';
import { ScopedMutator } from 'swr/dist/types';
import { handleSubmitSuccess, handleSubmitFailure } from '../../../lib/helpers';

const ManageUsersTab = () => {
const { mutate } = useSWRConfig();

/**
* Handles submission of the Manage Form with the specified role data.
* @param {ManageFormFields} roleData - The form data for the role being managed.
* @param {ScopedMutator} mutate - The scoped mutator function to update the query cache.
*/
const handleManageFormSubmit = async (roleData: ManageFormFields, mutate: ScopedMutator) => {
const res = await fetch(`/api/manage-role`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ formData: roleData }),
});

if (res.ok) {
mutate('/api/manage-role');
handleSubmitSuccess();
} else handleSubmitFailure(await res.text());
};

// User data
const { data: userData, error } = useCustomSWR<ManageFormFields>({
url: '/api/manage-role',
Expand Down
43 changes: 26 additions & 17 deletions components/Organizer/ManageUsersTab/manageRoleForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Form, Button, Select, Row, Col, message } from 'antd';
import Text from 'antd/lib/typography/Text';
import { useState } from 'react';
import { useContext, useState } from 'react';
import { ThemeContext, getAccentColor, getThemedClass } from '../../../theme/themeProvider';
import styles from '../../../styles/ManageUsersTab.module.css';

const { Option } = Select;

Expand All @@ -16,41 +18,44 @@ export interface ManageFormProps {
onSubmit: (value: ManageFormFields) => Promise<void>;
}

export default function ManageRoleForm(props: ManageFormProps) {
const { onSubmit } = props;
export default function ManageRoleForm({ onSubmit, formData }: ManageFormProps) {
const layout = {
labelCol: { span: 16 },
labelAlign: 'left',
};

const { accentColor, baseTheme } = useContext(ThemeContext);

const [modified, setModified] = useState<string[]>([]);

// TODO: probably add search
const [form] = Form.useForm();

return (
<Form {...layout} labelAlign="left" form={form} onFinish={onSubmit}>
{props.formData.map(config => (
{formData.map(config => (
<Form.Item
name={config._id}
// display name and email
label={
<Text style={{ height: '50px' }}>
{config.name}
<br />
<Text type="secondary">{config.email}</Text>
</Text>
<div>
<span className={styles[getThemedClass('manageUserPrimaryLabel', baseTheme)]}>
{config.name}
</span>
<div style={{ color: getAccentColor(accentColor), fontWeight: 200, paddingBottom: '5px' }}>
{config.email}
</div>
</div>
}
colon={false}
key={config._id}
initialValue={config.userType}>
<Select
placeholder="Select Role"
status={modified.includes(config._id) ? 'warning' : ''}
// not really a warning just good visually
// make box glow if role has been changed
status={modified.includes(config._id) ? 'warning' : ''}
onSelect={(role: string) => {
if (role !== props.formData.find(user => user._id === config._id)?.userType) {
if (role !== formData.find(user => user._id === config._id)?.userType) {
setModified([...modified, config._id]);
} else {
setModified([...modified.filter(user => user !== config._id)]);
Expand All @@ -64,20 +69,24 @@ export default function ManageRoleForm(props: ManageFormProps) {
))}
<Row gutter={16}>
<Col offset={10}>
<Button type="primary" htmlType="submit">
<button
type="submit"
className={styles['manageUserSubmitButton']}
style={{ backgroundColor: getAccentColor(accentColor) }}>
Submit
</Button>
</button>
</Col>
<Col>
<Button
htmlType="reset"
<button
type="reset"
className={styles['manageUserClearButton']}
onClick={() => {
form.resetFields();
message.success('Successfuly reset form!');
setModified([]);
}}>
Clear
</Button>
</button>
</Col>
</Row>
</Form>
Expand Down
26 changes: 18 additions & 8 deletions pages/OrganizerDash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import PreAddUsersTab from '../components/Organizer/PreAddUsersTab/PreAddUsersTa
import ApplicantsTab from '../components/Organizer/ApplicantsTab/ApplicantsTab';
import EventsTab from '../components/Organizer/EventsTab/EventsTab';
import styles from '../styles/Organizer.module.css';
import { useTheme } from '../theme/themeProvider';
import { ThemeContext, getAccentColor, getThemedClass } from '../theme/themeProvider';
import { useContext } from 'react';

export default function OrganizerDash() {
// Get session data
const { data: session, status } = useSession();
const { accentColor, baseTheme } = useContext(ThemeContext);

console.log(useTheme('organizerHeaderEmailText'));
console.log(getThemedClass('organizerHeaderEmailText', baseTheme));

return (
<div className={styles[useTheme('organizerMain')]}>
<div className={styles[useTheme('organizerHeader')]}>
<h1 className={styles[useTheme('organizerTitle')]}>Organizer Dashboard</h1>
<div className={styles[useTheme('organizerHeaderEmail')]}>
<div className={styles[useTheme('organizerHeaderEmailText')]}>{session?.user?.email}</div>
<div className={styles[getThemedClass('organizerMain', baseTheme)]}>
<div className={styles[getThemedClass('organizerHeader', baseTheme)]}>
<h1 className={styles[getThemedClass('organizerTitle', baseTheme)]}>Organizer Dashboard</h1>
<div className={styles[getThemedClass('organizerHeaderEmail', baseTheme)]}>
<div className={styles[getThemedClass('organizerHeaderEmailText', baseTheme)]}>
{session?.user?.email}
</div>
<div>
<button className={styles[useTheme('organizerButton')]} onClick={() => signOut()}>
<button
className={styles[getThemedClass('organizerButton', baseTheme)]}
onClick={() => signOut()}>
Sign out
</button>
</div>
Expand All @@ -31,6 +37,10 @@ export default function OrganizerDash() {
<Space direction="vertical">
<Tabs
defaultActiveKey="1"
style={{
color: getAccentColor(accentColor),
width: '90vw',
}}
items={[
{
label: `Schedule`,
Expand Down
3 changes: 2 additions & 1 deletion pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ export default function Page() {
<Layout
style={{
padding: session?.userType === undefined || session?.userType === 'HACKER' ? '0px' : '30px',
height: `100%`,
height: `100vh`,
width: `100vw`,
overflow: 'auto',
backgroundColor:
session?.userType === undefined || session?.userType === 'HACKER'
? 'white'
Expand Down
28 changes: 28 additions & 0 deletions styles/ManageUsersTab.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.manageUserPrimaryLabel-light {
height: 50px;
color: black;
font-weight: 600;
}

.manageUserPrimaryLabel-dark {
height: 50px;
color: white;
font-weight: 600;
}

.manageUserSubmitButton {
border-radius: 10px;
border: none;
font-weight: 600;
padding: 4px 10px;
transition: 0.2s;
}

.manageUserClearButton {
border-radius: 10px;
background-color: rgb(208, 208, 208);
border: none;
font-weight: 600;
padding: 4px 10px;
transition: 0.2s;
}
5 changes: 5 additions & 0 deletions styles/Organizer.module.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.tab {
height: 100%;
width: 100%;
}

.organizerHeader-light {
display: flex;
justify-content: space-between;
Expand Down
2 changes: 2 additions & 0 deletions styles/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans,
Helvetica Neue, sans-serif;
overscroll-behavior: none;
height: 100%;
}

a {
Expand Down
8 changes: 7 additions & 1 deletion theme/theme.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
export const themeConstants = {
// A default color palette for buttons, in case one isn't provided
light: {
backgroundColor: '#ffffff',
},
dark: {
backgroundColor: '#1D1743',
},
accent: {
blue: '#2F80ED',
green: '#219653',
pink: '#EB5757',
yellow: '#F2C94C',
orange: '#F2994A',
},
};
33 changes: 30 additions & 3 deletions theme/themeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createContext, useContext, useState } from 'react';
import { themeConstants } from './theme';

// Base theme options
export enum Theme {
Expand Down Expand Up @@ -34,7 +35,7 @@ export const ThemeContext = createContext<ThemeContextType>({
// Create the wrapper for the theme context
export const ThemeProvider = ({ children }: { children: any }) => {
const [baseTheme, setBaseTheme] = useState<Theme>(Theme.DARK);
const [accentColor, setAccentColor] = useState<AccentColor>(AccentColor.BLUE);
const [accentColor, setAccentColor] = useState<AccentColor>(AccentColor.ORANGE);

const value = {
baseTheme,
Expand All @@ -54,7 +55,33 @@ export const ThemeProvider = ({ children }: { children: any }) => {
* <div className={styles[useTheme('organizerMain')]}>
* ```
*/
export const useTheme = (className: string) => {
const { baseTheme } = useContext(ThemeContext);
export const getThemedClass = (className: string, baseTheme: Theme) => {
return `${className}-${baseTheme === Theme.LIGHT ? 'light' : 'dark'}`;
};

/**
* Returns the accent color hex code for a given accent color enum value
* @param accentColor - the accent color enum value
* @returns the hex code for the accent color
*
* @example
* ```tsx
* <div style={{ color: useAccentColor(AccentColor.BLUE) }}>Hello World</div>
* ```
*/
export const getAccentColor = (accentColor: AccentColor) => {
switch (accentColor) {
case AccentColor.BLUE:
return themeConstants.accent.blue;
case AccentColor.GREEN:
return themeConstants.accent.green;
case AccentColor.PINK:
return themeConstants.accent.pink;
case AccentColor.YELLOW:
return themeConstants.accent.yellow;
case AccentColor.ORANGE:
return themeConstants.accent.orange;
default:
return themeConstants.accent.blue;
}
};
20 changes: 0 additions & 20 deletions utils/organizer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ export const generateScheduleB = (teams: TeamData[], judges: UserData[]) => {
return sessionsB;
};

/**
* Handles submission of the Manage Form with the specified role data.
* @param {ManageFormFields} roleData - The form data for the role being managed.
* @param {ScopedMutator} mutate - The scoped mutator function to update the query cache.
*/
export const handleManageFormSubmit = async (roleData: ManageFormFields, mutate: ScopedMutator) => {
const res = await fetch(`/api/manage-role`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ formData: roleData }),
});

if (res.ok) {
mutate('/api/manage-role');
handleSubmitSuccess();
} else handleSubmitFailure(await res.text());
};

/**
* Handles pre-add user deletion with the specified user data.
* @param {PreAddData} user - The user data object to be deleted from pre-add.
Expand Down

0 comments on commit d5ab4fc

Please sign in to comment.