diff --git a/components/admin/MakeUserOfficerCard.tsx b/components/admin/MakeUserOfficerCard.tsx index 21a33d6a..e46aee8d 100644 --- a/components/admin/MakeUserOfficerCard.tsx +++ b/components/admin/MakeUserOfficerCard.tsx @@ -1,72 +1,110 @@ -import { Disclosure } from "@headlessui/react"; -import { Checkbox } from "@mui/material"; -import Button from "components/Button"; -import { useState } from "react"; -import { gqlQueries } from "src/api"; +import { Disclosure } from '@headlessui/react'; +import { Checkbox } from '@mui/material'; +import Button from 'components/Button'; +import { useState } from 'react'; +import { gqlQueries } from 'src/api'; interface MakeUserOfficerCardProps { - firstName: string; - lastName: string; - netid: string; - profileId: string; - divisions: Array<{ id: string; deptName: string }>; + firstName: string; + lastName: string; + netid: string; + profileId: string; + divisions: Array<{ id: string; deptName: string }>; } -export default function MakeUserOfficerCard({ firstName, lastName, netid, profileId, divisions }: MakeUserOfficerCardProps) { - const [divisionStatus, setDivisionStatus] = useState(Array(divisions.length).fill(false)); - const updateDivisionStatusAtIndex = (index: number, newStatus: boolean) => { - setDivisionStatus((prev) => prev.map((status, listIndex) => { - if (index === listIndex) return newStatus; - return status; - })); - } +export default function MakeUserOfficerCard({ + firstName, + lastName, + netid, + profileId, + divisions, +}: MakeUserOfficerCardProps) { + const [divisionStatus, setDivisionStatus] = useState( + Array(divisions.length).fill(false), + ); + const updateDivisionStatusAtIndex = (index: number, newStatus: boolean) => { + setDivisionStatus((prev) => + prev.map((status, listIndex) => { + if (index === listIndex) return newStatus; + return status; + }), + ); + }; - return - {({ open }) => ( - <> - - {`${firstName} ${lastName} (${netid})`} - - - - - - Check all division you want to add this user to: - {divisions.map((division, index) => ( -
- updateDivisionStatusAtIndex(index, e.target.checked)}/> -

{division.deptName}

-
- ))} - -
- - )} -
-} \ No newline at end of file + update: { + divisions: { + connect: [ + { + id: division.id, + }, + ], + }, + }, + }); + }), + ); + alert('Successfully added user to checked division(s)'); + setDivisionStatus((prev) => prev.map(() => false)); + }} + > + click to add user to checked division + + + + )} + + ); +} diff --git a/lib/generated/graphql.ts b/lib/generated/graphql.ts index 351e1ad4..e281af6a 100644 --- a/lib/generated/graphql.ts +++ b/lib/generated/graphql.ts @@ -2284,6 +2284,7 @@ export type Mutation = { updateOneScoreEntry?: Maybe; updateOneTypeformApplication?: Maybe; upsertOneDirector: Director; + upsertOneOfficer: Officer; upsertOneParticipant: Participant; upsertOneProfile: Profile; }; @@ -2418,6 +2419,13 @@ export type MutationUpsertOneDirectorArgs = { }; +export type MutationUpsertOneOfficerArgs = { + create: OfficerCreateInput; + update: OfficerUpdateInput; + where: OfficerWhereUniqueInput; +}; + + export type MutationUpsertOneParticipantArgs = { create: ParticipantCreateInput; update: ParticipantUpdateInput; @@ -2567,6 +2575,14 @@ export type OfficerCountDivisionsArgs = { where?: InputMaybe; }; +export type OfficerCreateInput = { + director?: InputMaybe; + divisions?: InputMaybe; + dummy?: InputMaybe; + id?: InputMaybe; + profile: ProfileCreateNestedOneWithoutOfficerInput; +}; + export type OfficerCreateNestedManyWithoutDivisionsInput = { connect?: InputMaybe>; connectOrCreate?: InputMaybe>; @@ -5327,12 +5343,13 @@ export type GetAddOfficerPageDataQueryVariables = Exact<{ [key: string]: never; export type GetAddOfficerPageDataQuery = { __typename?: 'Query', me: { __typename?: 'User', profile?: { __typename?: 'Profile', officer?: { __typename?: 'Officer', divisions: Array<{ __typename?: 'Division', id: string, deptName: string }> } | null } | null }, officerEligibleProfiles: Array<{ __typename?: 'Profile', lastName: string, firstName: string, netid: string, id: string }> }; export type AddUserToDivisionMutationVariables = Exact<{ - data: OfficerUpdateInput; where: OfficerWhereUniqueInput; + create: OfficerCreateInput; + update: OfficerUpdateInput; }>; -export type AddUserToDivisionMutation = { __typename?: 'Mutation', updateOneOfficer?: { __typename?: 'Officer', profile: { __typename?: 'Profile', firstName: string, lastName: string, officer?: { __typename?: 'Officer', divisions: Array<{ __typename?: 'Division', deptName: string }> } | null } } | null }; +export type AddUserToDivisionMutation = { __typename?: 'Mutation', upsertOneOfficer: { __typename?: 'Officer', profile: { __typename?: 'Profile', firstName: string, lastName: string }, divisions: Array<{ __typename?: 'Division', deptName: string }> } }; export type GetAddParticipantPageDataQueryVariables = Exact<{ [key: string]: never; }>; @@ -5913,16 +5930,14 @@ export const GetAddOfficerPageDataDocument = gql` } `; export const AddUserToDivisionDocument = gql` - mutation addUserToDivision($data: OfficerUpdateInput!, $where: OfficerWhereUniqueInput!) { - updateOneOfficer(data: $data, where: $where) { + mutation addUserToDivision($where: OfficerWhereUniqueInput!, $create: OfficerCreateInput!, $update: OfficerUpdateInput!) { + upsertOneOfficer(where: $where, create: $create, update: $update) { profile { firstName lastName - officer { - divisions { - deptName - } - } + } + divisions { + deptName } } } diff --git a/lib/graphql/typegraphql-prisma/exposedResolvers.ts b/lib/graphql/typegraphql-prisma/exposedResolvers.ts index 4227f19e..41ca34d1 100644 --- a/lib/graphql/typegraphql-prisma/exposedResolvers.ts +++ b/lib/graphql/typegraphql-prisma/exposedResolvers.ts @@ -48,6 +48,7 @@ import { CreateOneScoreEntryResolver, DeleteOneScoreEntryResolver, UpdateOneScoreEntryResolver, + UpsertOneOfficerResolver } from '@generated/type-graphql'; export const exposedResolvers: NonEmptyArray = [ @@ -100,5 +101,6 @@ export const exposedResolvers: NonEmptyArray = [ FindManyDivisionResolver, FindManyEventResolver, UpdateOneOfficerResolver, - UpdateOneScoreEntryResolver + UpdateOneScoreEntryResolver, + UpsertOneOfficerResolver ]; diff --git a/pages/admin/officer/add.tsx b/pages/admin/officer/add.tsx index 68f8a9e3..671333e8 100644 --- a/pages/admin/officer/add.tsx +++ b/pages/admin/officer/add.tsx @@ -9,6 +9,7 @@ import MakeUserOfficerCard from 'components/admin/MakeUserOfficerCard'; import { OfficerStatusContext } from 'components/context/OfficerStatus'; import { useRouter } from 'next/router'; import AdminOnlyComponent from 'components/admin/AdminOnly'; +import Fuse from 'fuse.js'; export default function AddOfficerPage() { // Need to get logged in user profile @@ -21,16 +22,16 @@ export default function AddOfficerPage() { { enabled: status === 'authenticated' && officerStatusData.isOfficer }, ); const [searchQuery, setSearchQuery] = useState(''); - const [profiles, setProfiles] = useState([]); + const [profiles, setProfiles] = useState( + [], + ); let filteredProfiles = profiles; if (searchQuery !== '') { - filteredProfiles = profiles.filter( - (profile) => - profile.netid === searchQuery || - `${profile.firstName} ${profile.lastName}`.toLowerCase() === searchQuery.toLowerCase() || - profile.firstName.toLowerCase() === searchQuery.toLowerCase() || - profile.lastName.toLowerCase() === searchQuery.toLowerCase(), - ); + const fuse = new Fuse(profiles, { + keys: ['firstName', 'lastName'], + }); + const result = fuse.search(searchQuery); + filteredProfiles = result.map((data) => data.item); } else { filteredProfiles = []; } diff --git a/src/graphql/officer.graphql b/src/graphql/officer.graphql index b1f79717..f686245b 100644 --- a/src/graphql/officer.graphql +++ b/src/graphql/officer.graphql @@ -17,16 +17,14 @@ query getAddOfficerPageData { } } -mutation addUserToDivision($data: OfficerUpdateInput!, $where: OfficerWhereUniqueInput!) { - updateOneOfficer(data: $data, where: $where) { +mutation addUserToDivision($where: OfficerWhereUniqueInput!, $create: OfficerCreateInput!, $update: OfficerUpdateInput!) { + upsertOneOfficer(where: $where, create: $create, update: $update) { profile { firstName lastName - officer { - divisions { - deptName - } - } + } + divisions { + deptName } } -} \ No newline at end of file +}