Compare commits
5 commits
ce5d65e62d
...
4229682d69
Author | SHA1 | Date | |
---|---|---|---|
4229682d69 | |||
cbe50dd028 | |||
100d5e80eb | |||
ea1f032ffc | |||
56ccd61107 |
5 changed files with 27 additions and 19 deletions
|
@ -21,8 +21,6 @@
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import type { UserResponse } from '~/types/interfaces';
|
import type { UserResponse } from '~/types/interfaces';
|
||||||
|
|
||||||
const { fetchMembers } = useApi();
|
|
||||||
|
|
||||||
const props = defineProps<{
|
const props = defineProps<{
|
||||||
user: UserResponse
|
user: UserResponse
|
||||||
}>();
|
}>();
|
||||||
|
|
|
@ -1,24 +1,36 @@
|
||||||
import type { ChannelResponse, GuildMemberResponse, GuildResponse, MessageResponse, StatsResponse, UserResponse } from "~/types/interfaces";
|
import type { ChannelResponse, GuildMemberResponse, GuildResponse, MessageResponse, StatsResponse, UserResponse } from "~/types/interfaces";
|
||||||
|
|
||||||
|
function ensureIsArray(list: any) {
|
||||||
|
if (Array.isArray(list)) {
|
||||||
|
return list
|
||||||
|
} else {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const useApi = () => {
|
export const useApi = () => {
|
||||||
async function fetchGuilds(): Promise<GuildResponse[] | undefined> {
|
async function fetchGuilds(): Promise<GuildResponse[]> {
|
||||||
return await fetchWithApi(`/guilds`);
|
return ensureIsArray(await fetchWithApi(`/guilds`));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchGuild(guildId: string): Promise<GuildResponse | undefined> {
|
async function fetchGuild(guildId: string): Promise<GuildResponse | undefined> {
|
||||||
return await fetchWithApi(`/guilds/${guildId}`);
|
return await fetchWithApi(`/guilds/${guildId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchChannels(guildId: string): Promise<ChannelResponse[] | undefined> {
|
async function fetchMyGuilds(): Promise<GuildResponse[]> {
|
||||||
return await fetchWithApi(`/guilds/${guildId}/channels`);
|
return ensureIsArray(await fetchWithApi(`/me/guilds`));
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchChannels(guildId: string): Promise<ChannelResponse[]> {
|
||||||
|
return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/channels`));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchChannel(channelId: string): Promise<ChannelResponse | undefined> {
|
async function fetchChannel(channelId: string): Promise<ChannelResponse | undefined> {
|
||||||
return await fetchWithApi(`/channels/${channelId}`)
|
return await fetchWithApi(`/channels/${channelId}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchMembers(guildId: string): Promise<GuildMemberResponse[] | undefined> {
|
async function fetchMembers(guildId: string): Promise<GuildMemberResponse[]> {
|
||||||
return await fetchWithApi(`/guilds/${guildId}/members`);
|
return ensureIsArray(await fetchWithApi(`/guilds/${guildId}/members`));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchMember(guildId: string, memberId: string): Promise<GuildMemberResponse | undefined> {
|
async function fetchMember(guildId: string, memberId: string): Promise<GuildMemberResponse | undefined> {
|
||||||
|
@ -34,12 +46,7 @@ export const useApi = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchFriends(): Promise<UserResponse[]> {
|
async function fetchFriends(): Promise<UserResponse[]> {
|
||||||
const response = await fetchWithApi('/me/friends')
|
return ensureIsArray(await fetchWithApi('/me/friends'));
|
||||||
if (Array.isArray(response)) {
|
|
||||||
return response
|
|
||||||
} else {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addFriend(username: string): Promise<void> {
|
async function addFriend(username: string): Promise<void> {
|
||||||
|
@ -82,6 +89,7 @@ export const useApi = () => {
|
||||||
return {
|
return {
|
||||||
fetchGuilds,
|
fetchGuilds,
|
||||||
fetchGuild,
|
fetchGuild,
|
||||||
|
fetchMyGuilds,
|
||||||
fetchChannels,
|
fetchChannels,
|
||||||
fetchChannel,
|
fetchChannel,
|
||||||
fetchMembers,
|
fetchMembers,
|
||||||
|
|
|
@ -95,7 +95,7 @@ const options = [
|
||||||
if (invite.length == 6) {
|
if (invite.length == 6) {
|
||||||
try {
|
try {
|
||||||
const joinedGuild = await api.joinGuild(invite);
|
const joinedGuild = await api.joinGuild(invite);
|
||||||
guilds?.push(joinedGuild);
|
guilds.push(joinedGuild);
|
||||||
return await navigateTo(`/servers/${joinedGuild.uuid}`);
|
return await navigateTo(`/servers/${joinedGuild.uuid}`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
alert(`Couldn't use invite: ${error}`);
|
alert(`Couldn't use invite: ${error}`);
|
||||||
|
@ -151,7 +151,7 @@ const options = [
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const guilds: GuildResponse[] | undefined = await fetchWithApi("/me/guilds");
|
const guilds = await api.fetchMyGuilds();
|
||||||
|
|
||||||
function createDropdown() {
|
function createDropdown() {
|
||||||
const dropdown = h(GuildDropdown, { options });
|
const dropdown = h(GuildDropdown, { options });
|
||||||
|
|
|
@ -5,10 +5,10 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
|
||||||
|
|
||||||
const guildId = to.params.serverId as string;
|
const guildId = to.params.serverId as string;
|
||||||
|
|
||||||
const channels: ChannelResponse[] | undefined = await fetchChannels(guildId);
|
const channels: ChannelResponse[] = await fetchChannels(guildId);
|
||||||
console.log("channels:", channels);
|
console.log("channels:", channels);
|
||||||
|
|
||||||
if (channels && channels.length > 0) {
|
if (channels.length > 0) {
|
||||||
console.log("wah");
|
console.log("wah");
|
||||||
return await navigateTo(`/servers/${guildId}/channels/${channels[0].uuid}`, { replace: true });
|
return await navigateTo(`/servers/${guildId}/channels/${channels[0].uuid}`, { replace: true });
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,7 +64,9 @@ onActivated(async () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
async function setArrayVariables() {
|
async function setArrayVariables() {
|
||||||
members.value = await fetchMembers(route.params.serverId as string);
|
members.value = await fetchMembers(route.params.serverId as string).then((response) => {
|
||||||
|
return response.sort((a, b) => getDisplayName(a.user, a).localeCompare(getDisplayName(b.user, b)))
|
||||||
|
});
|
||||||
const guildUrl = `guilds/${route.params.serverId}`;
|
const guildUrl = `guilds/${route.params.serverId}`;
|
||||||
channels.value = await fetchWithApi(`${guildUrl}/channels`);
|
channels.value = await fetchWithApi(`${guildUrl}/channels`);
|
||||||
console.log("channels:", channels.value);
|
console.log("channels:", channels.value);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue