diff --git a/frontend-react/src/components/admin/AdminPage.tsx b/frontend-react/src/components/admin/AdminPage.tsx index 0c4597862..e64bb924c 100644 --- a/frontend-react/src/components/admin/AdminPage.tsx +++ b/frontend-react/src/components/admin/AdminPage.tsx @@ -1,11 +1,11 @@ -import { useMutation, useQuery } from "@apollo/client"; +import { useQuery } from "@apollo/client"; import { Box, Flex, useToast } from "@chakra-ui/core"; import { Redirect, RouteComponentProps } from "@reach/router"; import React, { useState } from "react"; import { - UpdateTwitterVars, - UPDATE_TWITTER, -} from "../../graphql/mutations/updateTwitter"; + MutationUpdatePlayerIdArgs, + useUpdatePlayerIdMutation, +} from "../../generated/graphql"; import { USER } from "../../graphql/queries/user"; import { UserData } from "../../types"; import Error from "../common/Error"; @@ -17,22 +17,18 @@ import Input from "../elements/Input"; import VotingManager from "./VotingManager"; const AdminPage: React.FC = () => { - const [updateTwitterForms, setUpdateTwitterForms] = useState< - Partial + const [updatePlayerIdForms, setUpdatePlayerIdForms] = useState< + Partial >({}); const toast = useToast(); const { data: userData, error: userError, loading: userLoading } = useQuery< UserData >(USER); - const [updateTwitter] = useMutation< - { updateTwitter: boolean }, - UpdateTwitterVars - >(UPDATE_TWITTER, { - variables: updateTwitterForms as UpdateTwitterVars, - onCompleted: (data) => { + const [updatePlayerId] = useUpdatePlayerIdMutation({ + onCompleted: () => { toast({ - description: "Twitter updated", + description: "Player ID updated", position: "top-right", status: "success", duration: 10000, @@ -58,26 +54,42 @@ const AdminPage: React.FC = () => { return ( <> - Update Twitter + Update player ID - setUpdateTwitterForms({ ...updateTwitterForms, unique_id: value }) + setUpdatePlayerIdForms({ + ...updatePlayerIdForms, + playerId: value, + }) } - label="Unique ID" + label="Player ID" /> - setUpdateTwitterForms({ ...updateTwitterForms, twitter: value }) + setUpdatePlayerIdForms({ ...updatePlayerIdForms, discordId: value }) } - label="Twitter" + label="Discord ID" /> - + diff --git a/frontend-react/src/generated/graphql.tsx b/frontend-react/src/generated/graphql.tsx index 71b2f489e..78835059e 100644 --- a/frontend-react/src/generated/graphql.tsx +++ b/frontend-react/src/generated/graphql.tsx @@ -140,6 +140,7 @@ export type Mutation = { generateMaplistFromVotes?: Maybe; updateUser?: Maybe; updateAvatars?: Maybe; + updatePlayerId: Scalars['Boolean']; addDetailedTournament: Scalars['Boolean']; addPrivateBattles: Scalars['Int']; replaceDraftLeaderboard: Scalars['Boolean']; @@ -212,6 +213,12 @@ export type MutationUpdateAvatarsArgs = { }; +export type MutationUpdatePlayerIdArgs = { + discordId: Scalars['String']; + playerId: Scalars['String']; +}; + + export type MutationAddDetailedTournamentArgs = { plus_server: PlusServer; tournament: DetailedTournamentInput; @@ -951,6 +958,17 @@ export type UserLeanFragment = ( & Pick ); +export type UpdatePlayerIdMutationVariables = Exact<{ + playerId: Scalars['String']; + discordId: Scalars['String']; +}>; + + +export type UpdatePlayerIdMutation = ( + { __typename?: 'Mutation' } + & Pick +); + export type GetPeakXPowerLeaderboardQueryVariables = Exact<{ page?: Maybe; weapon?: Maybe; @@ -1040,6 +1058,37 @@ export const UserLeanFragmentDoc = gql` avatarUrl } `; +export const UpdatePlayerIdDocument = gql` + mutation updatePlayerId($playerId: String!, $discordId: String!) { + updatePlayerId(playerId: $playerId, discordId: $discordId) +} + `; +export type UpdatePlayerIdMutationFn = Apollo.MutationFunction; + +/** + * __useUpdatePlayerIdMutation__ + * + * To run a mutation, you first call `useUpdatePlayerIdMutation` within a React component and pass it any options that fit your needs. + * When your component renders, `useUpdatePlayerIdMutation` returns a tuple that includes: + * - A mutate function that you can call at any time to execute the mutation + * - An object with fields that represent the current status of the mutation's execution + * + * @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2; + * + * @example + * const [updatePlayerIdMutation, { data, loading, error }] = useUpdatePlayerIdMutation({ + * variables: { + * playerId: // value for 'playerId' + * discordId: // value for 'discordId' + * }, + * }); + */ +export function useUpdatePlayerIdMutation(baseOptions?: Apollo.MutationHookOptions) { + return Apollo.useMutation(UpdatePlayerIdDocument, baseOptions); + } +export type UpdatePlayerIdMutationHookResult = ReturnType; +export type UpdatePlayerIdMutationResult = Apollo.MutationResult; +export type UpdatePlayerIdMutationOptions = Apollo.BaseMutationOptions; export const GetPeakXPowerLeaderboardDocument = gql` query getPeakXPowerLeaderboard($page: Int, $weapon: String) { getPeakXPowerLeaderboard(page: $page, weapon: $weapon) { diff --git a/frontend-react/src/graphql/mutations/updatePlayerId.graphql b/frontend-react/src/graphql/mutations/updatePlayerId.graphql new file mode 100644 index 000000000..ac526b5d3 --- /dev/null +++ b/frontend-react/src/graphql/mutations/updatePlayerId.graphql @@ -0,0 +1,3 @@ +mutation updatePlayerId($playerId: String!, $discordId: String!) { + updatePlayerId(playerId: $playerId, discordId: $discordId) +} diff --git a/frontend-react/src/graphql/mutations/updateTwitter.ts b/frontend-react/src/graphql/mutations/updateTwitter.ts deleted file mode 100644 index 9fab1434c..000000000 --- a/frontend-react/src/graphql/mutations/updateTwitter.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { DocumentNode, gql } from "@apollo/client"; - -export interface UpdateTwitterVars { - unique_id: string; - twitter: string; -} - -export const UPDATE_TWITTER: DocumentNode = gql` - mutation updateTwitter($unique_id: String!, $twitter: String!) { - updateTwitter(unique_id: $unique_id, twitter: $twitter) - } -`; diff --git a/graphql-schemas/user.js b/graphql-schemas/user.js index 6a7a59e35..e64936299 100644 --- a/graphql-schemas/user.js +++ b/graphql-schemas/user.js @@ -35,6 +35,7 @@ const typeDef = gql` bio: String ): Boolean updateAvatars(lohiToken: String!, toUpdate: [DiscordIdAvatar!]!): Boolean + updatePlayerId(discordId: String!, playerId: String!): Boolean! } "The control sensitivity used in Splatoon 2" @@ -272,6 +273,18 @@ const resolvers = { ) ); + return true; + }, + updatePlayerId: async (_, args, ctx) => { + if (!ctx.user) throw new AuthenticationError("not logged in"); + if (ctx.user.discord_id !== process.env.ADMIN_ID) { + throw new AuthenticationError("not admin"); + } + + await ObjectionUser.query() + .patch({ playerId: args.playerId }) + .where("discordId", "=", args.discordId); + return true; }, },