update player id

This commit is contained in:
Kalle (Sendou) 2020-10-08 20:03:58 +03:00
parent 585ce74c5f
commit d0b8cefe73
5 changed files with 98 additions and 33 deletions

View File

@ -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<RouteComponentProps> = () => {
const [updateTwitterForms, setUpdateTwitterForms] = useState<
Partial<UpdateTwitterVars>
const [updatePlayerIdForms, setUpdatePlayerIdForms] = useState<
Partial<MutationUpdatePlayerIdArgs>
>({});
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<RouteComponentProps> = () => {
return (
<>
<PageHeader title="Admin" />
<SubHeader>Update Twitter</SubHeader>
<SubHeader>Update player ID</SubHeader>
<Flex my="1em">
<Box mr="1em">
<Input
value={updateTwitterForms.unique_id ?? ""}
value={updatePlayerIdForms.playerId ?? ""}
setValue={(value) =>
setUpdateTwitterForms({ ...updateTwitterForms, unique_id: value })
setUpdatePlayerIdForms({
...updatePlayerIdForms,
playerId: value,
})
}
label="Unique ID"
label="Player ID"
/>
</Box>
<Input
value={updateTwitterForms.twitter ?? ""}
value={updatePlayerIdForms.discordId ?? ""}
setValue={(value) =>
setUpdateTwitterForms({ ...updateTwitterForms, twitter: value })
setUpdatePlayerIdForms({ ...updatePlayerIdForms, discordId: value })
}
label="Twitter"
label="Discord ID"
/>
</Flex>
<Button onClick={() => updateTwitter()}>Submit</Button>
<Button
onClick={() => {
if (!updatePlayerIdForms.playerId || !updatePlayerIdForms.discordId)
return;
updatePlayerId({
variables: {
playerId: updatePlayerIdForms.playerId,
discordId: updatePlayerIdForms.discordId,
},
});
}}
>
Submit
</Button>
<Box>
<VotingManager />
</Box>

View File

@ -140,6 +140,7 @@ export type Mutation = {
generateMaplistFromVotes?: Maybe<Scalars['Boolean']>;
updateUser?: Maybe<Scalars['Boolean']>;
updateAvatars?: Maybe<Scalars['Boolean']>;
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<NewUser, 'profilePath' | 'fullUsername' | 'avatarUrl'>
);
export type UpdatePlayerIdMutationVariables = Exact<{
playerId: Scalars['String'];
discordId: Scalars['String'];
}>;
export type UpdatePlayerIdMutation = (
{ __typename?: 'Mutation' }
& Pick<Mutation, 'updatePlayerId'>
);
export type GetPeakXPowerLeaderboardQueryVariables = Exact<{
page?: Maybe<Scalars['Int']>;
weapon?: Maybe<Scalars['String']>;
@ -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<UpdatePlayerIdMutation, UpdatePlayerIdMutationVariables>;
/**
* __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<UpdatePlayerIdMutation, UpdatePlayerIdMutationVariables>) {
return Apollo.useMutation<UpdatePlayerIdMutation, UpdatePlayerIdMutationVariables>(UpdatePlayerIdDocument, baseOptions);
}
export type UpdatePlayerIdMutationHookResult = ReturnType<typeof useUpdatePlayerIdMutation>;
export type UpdatePlayerIdMutationResult = Apollo.MutationResult<UpdatePlayerIdMutation>;
export type UpdatePlayerIdMutationOptions = Apollo.BaseMutationOptions<UpdatePlayerIdMutation, UpdatePlayerIdMutationVariables>;
export const GetPeakXPowerLeaderboardDocument = gql`
query getPeakXPowerLeaderboard($page: Int, $weapon: String) {
getPeakXPowerLeaderboard(page: $page, weapon: $weapon) {

View File

@ -0,0 +1,3 @@
mutation updatePlayerId($playerId: String!, $discordId: String!) {
updatePlayerId(playerId: $playerId, discordId: $discordId)
}

View File

@ -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)
}
`;

View File

@ -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;
},
},