mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-04-24 03:00:09 -04:00
simplified queries for player/guild pages
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import gql from 'fake-tag';
|
||||
|
||||
import { GetGuildsQuery, GetGuildsQueryVariables } from './autogen/types';
|
||||
import {
|
||||
GetGuildnamesQuery,
|
||||
GetGuildnamesQueryVariables,
|
||||
GetGuildsQuery,
|
||||
GetGuildsQueryVariables,
|
||||
} from './autogen/types';
|
||||
import { client } from './client';
|
||||
import { GuildFragment } from './fragments';
|
||||
|
||||
@@ -28,3 +33,29 @@ export const getGuilds = async (limit = 50) => {
|
||||
|
||||
return data.guild;
|
||||
};
|
||||
|
||||
const guildnamesQuery = gql`
|
||||
query GetGuildnames($limit: Int) {
|
||||
guild(where: { status: { _eq: ACTIVE } }, limit: $limit) {
|
||||
guildname
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const getGuildnames = async (limit = 50) => {
|
||||
const { data, error } = await client
|
||||
.query<GetGuildnamesQuery, GetGuildnamesQueryVariables>(guildnamesQuery, {
|
||||
limit,
|
||||
})
|
||||
.toPromise();
|
||||
|
||||
if (!data) {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
return data.guild.map((g) => g.guildname);
|
||||
};
|
||||
|
||||
@@ -3,6 +3,8 @@ import gql from 'fake-tag';
|
||||
import {
|
||||
GetPlayersQuery,
|
||||
GetPlayersQueryVariables,
|
||||
GetPlayerUsernamesQuery,
|
||||
GetPlayerUsernamesQueryVariables,
|
||||
PlayerFragmentFragment,
|
||||
} from './autogen/types';
|
||||
import { client } from './client';
|
||||
@@ -51,3 +53,44 @@ export const getTopPlayers = async (): Promise<PlayerFragmentFragment[]> => {
|
||||
const playersArr = await Promise.all(promises);
|
||||
return playersArr.reduce((_total, _players) => [..._total, ..._players], []);
|
||||
};
|
||||
|
||||
const playerUsernamesQuery = gql`
|
||||
query GetPlayerUsernames($limit: Int, $offset: Int) {
|
||||
player(order_by: { total_xp: desc }, limit: $limit, offset: $offset) {
|
||||
username
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
export const getPlayerUsernames = async (
|
||||
limit = 50,
|
||||
offset = 0,
|
||||
): Promise<string[]> => {
|
||||
const { data, error } = await client
|
||||
.query<GetPlayerUsernamesQuery, GetPlayerUsernamesQueryVariables>(
|
||||
playerUsernamesQuery,
|
||||
{
|
||||
limit,
|
||||
offset,
|
||||
},
|
||||
)
|
||||
.toPromise();
|
||||
|
||||
if (!data) {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
return data.player.map((p) => p.username);
|
||||
};
|
||||
|
||||
export const getTopPlayerUsernames = async (): Promise<string[]> => {
|
||||
const promises: Promise<string[]>[] = new Array(TOTAL_PLAYERS / LIMIT)
|
||||
.fill(false)
|
||||
.map((_, i) => getPlayerUsernames(LIMIT, i * LIMIT));
|
||||
const playersArr = await Promise.all(promises);
|
||||
return playersArr.reduce((_total, _players) => [..._total, ..._players], []);
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Box, Flex, LoadingState } from '@metafam/ds';
|
||||
import { QuestFragmentFragment, QuestStatus_Enum } from 'graphql/autogen/types';
|
||||
import { getGuild } from 'graphql/getGuild';
|
||||
import { getGuilds } from 'graphql/getGuilds';
|
||||
import { getGuildnames } from 'graphql/getGuilds';
|
||||
import { getQuests } from 'graphql/getQuests';
|
||||
import {
|
||||
GetStaticPaths,
|
||||
@@ -98,10 +98,10 @@ export default GuildPage;
|
||||
type QueryParams = { guildname: string };
|
||||
|
||||
export const getStaticPaths: GetStaticPaths<QueryParams> = async () => {
|
||||
const guilds = await getGuilds();
|
||||
const guildnames = await getGuildnames();
|
||||
|
||||
return {
|
||||
paths: guilds.map(({ guildname }) => ({
|
||||
paths: guildnames.map((guildname) => ({
|
||||
params: { guildname },
|
||||
})),
|
||||
fallback: true,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Box, Flex, LoadingState } from '@metafam/ds';
|
||||
import { PlayerHero } from 'components/Player/Section/PlayerHero';
|
||||
import { getPlayer } from 'graphql/getPlayer';
|
||||
import { getTopPlayers } from 'graphql/getPlayers';
|
||||
import { getTopPlayerUsernames } from 'graphql/getPlayers';
|
||||
import {
|
||||
GetStaticPaths,
|
||||
GetStaticPropsContext,
|
||||
@@ -171,10 +171,10 @@ export default PlayerPage;
|
||||
type QueryParams = { username: string };
|
||||
|
||||
export const getStaticPaths: GetStaticPaths<QueryParams> = async () => {
|
||||
const players = await getTopPlayers();
|
||||
const playerUsernames = await getTopPlayerUsernames();
|
||||
|
||||
return {
|
||||
paths: players.map(({ username }) => ({
|
||||
paths: playerUsernames.map((username) => ({
|
||||
params: { username },
|
||||
})),
|
||||
fallback: true,
|
||||
|
||||
Reference in New Issue
Block a user