Skip to content

Commit

Permalink
add mailing lists to admin page and clean up interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
135ze committed Dec 1, 2024
1 parent 76ac98c commit dcf0f32
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
8 changes: 7 additions & 1 deletion src/pages/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ export default async function handler(
try {
const users = await prisma.user.findMany({
include: {
mailingLists: getMailingLists,
mailingLists: getMailingLists
? {
include: {
mailingList: true,
},
}
: false,
},
});

Expand Down
22 changes: 10 additions & 12 deletions src/pages/manage.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { useEffect, useState } from 'react';
interface User {
id: number;
firstName: string;
lastName: string;
email: string;
role: string;
status: string;
numOfAbsences: string;
}
import { User } from '../types/types';

export default function Manage() {
const [users, setUsers] = useState<User[]>([]);
const [loading, setLoading] = useState<boolean>(true);

useEffect(() => {
const fetchUsers = async () => {
const apiUrl = `/api/users/`;
const apiUrl = `/api/users?getMailingLists=true`;

try {
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(response.statusText);
}
const data: User[] = await response.json();
setUsers(data);
const users: User[] = await response.json();
setUsers(users);
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Error fetching users:', error.message);
Expand Down Expand Up @@ -81,6 +73,7 @@ export default function Manage() {
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Email Subscriptions</th>
</tr>
</thead>
<tbody>
Expand All @@ -99,6 +92,11 @@ export default function Manage() {
<option value="ADMIN">Admin</option>
</select>
</td>
<td>
{user.mailingLists
?.map((mailingList) => mailingList.mailingList.name)
.join(', ')}
</td>
</tr>
))}
</tbody>
Expand Down
10 changes: 3 additions & 7 deletions src/pages/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { useSession } from 'next-auth/react';
import { useEffect, useState } from 'react';
import { SignOutButton } from '../components/SignOutButton';
import { Image } from '@chakra-ui/react';

interface UserData {
numOfAbsences: number;
absences: Array<any>;
}
import { User } from '../types/types';

export default function Profile() {
const { data: session, status } = useSession();
Expand All @@ -24,9 +20,9 @@ export default function Profile() {
if (!response.ok) {
throw new Error(response.statusText);
}
const data: UserData = await response.json();
const data: User = await response.json();
setNumAbsences(data.numOfAbsences);
setUsedAbsences(data.absences.length);
setUsedAbsences(data.absences?.length || null);
} catch (error) {
console.error('Error fetching data:', error);
}
Expand Down
38 changes: 38 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
interface Absence {
id: number;
lessonDate: Date;
lessonPlan?: string;
reasonOfAbsence: string;
notes?: string;
absentTeacherId: number;
substituteTeacherId?: number;
locationId: number;
subjectId: number;
}

export interface User {
id: number;
email: string;
firstName: string;
lastName: string;
role: string;
status: string;
numOfAbsences: number;
absences?: Absence[];
substitutes?: Absence[];
mailingLists?: UsersOnMailingLists[];
}

interface MailingList {
id: number;
name: string;
emails: string[];
users?: UsersOnMailingLists[];
}

export interface UsersOnMailingLists {
user: User;
userId: number;
mailingList: MailingList;
mailingListId: number;
}

0 comments on commit dcf0f32

Please sign in to comment.