Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: user search #82

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ services:
cb-db:
container_name: cb-db
image: postgres:15.4-alpine
restart: always
restart: unless-stopped
environment:
POSTGRES_PASSWORD: password
volumes:
- ./prisma/db:/var/lib/postgresql/data
ports:
- 5432:5432
- 6432:5432
cb-app:
container_name: cb-app
build:
context: .
dockerfile: app.dockerfile
restart: always
restart: unless-stopped
depends_on:
- cb-db
ports:
Expand Down
13 changes: 13 additions & 0 deletions src/lib/dtos/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type aboutInfo = {
bio: string;
tagline: string;
};

export type User = {
id: string;
name: string;
lastOnline: string;
profilePicture?: string;
aboutInfo?: aboutInfo;
gameIds?: number[];
};
29 changes: 3 additions & 26 deletions src/routes/(auth)/login/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,18 @@
import { guildedMediaLink } from '$lib/utils/guilded-media';
import { Avatar } from '@skeletonlabs/skeleton';
import { onMount } from 'svelte';
import type { User } from '$lib/dtos/user';

let userSearch = '';

type aboutInfo = {
bio: string;
tagline: string;
};

type User = {
id: string;
name: string;
lastOnline: string;
profilePicture?: string;
aboutInfo?: aboutInfo;
gameIds?: number[];
};

let users: User[] = [];

const typingDelay = 750;
var typingTimer: NodeJS.Timeout;

const searchForUsername = async () => {
fetch(
`https://www.guilded.gg/api/search?query=${userSearch}&entityType=user&maxResultsPerType=20`,
{ method: 'GET' }
).then(async (res) => {
const data = await res.json();
users = data.results.users;
users = users.map((user: User) => {
return {
...user,
profilePicture: guildedMediaLink(user.profilePicture ?? '/poop.png')
};
});
fetch(`/api/v2/search?query=${userSearch}`, { method: 'GET' }).then(async (res) => {
users = await res.json();
if (userSearch == '') {
users = [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import { guildedMediaLink } from '$lib/utils/guilded-media';
storePopup.set({ computePosition, autoUpdate, flip, shift, offset, arrow });
initializeStores();
const drawerStore = getDrawerStore();
const setTheme: SubmitFunction = ({ data }) => {
const theme = data.get('theme')?.toString();
getDrawerStore();
const setTheme: SubmitFunction = ({ formData }) => {
const theme = formData.get('theme')?.toString();
if (theme) {
document.body.setAttribute('data-theme', theme);
$storeTheme = theme;
Expand Down
12 changes: 11 additions & 1 deletion src/routes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@ export const actions: Actions = {
const formData = await request.formData();
const theme = formData.get('theme')?.toString() ?? 'skeleton';
// Sets the selected theme to the cookie
cookies.set('theme', theme, { path: '/' });
cookies.set('theme', theme, {
path: '/',
httpOnly: true,
// only requests from same site can send cookies
// https://developer.mozilla.org/en-US/docs/Glossary/CSRF
sameSite: 'lax',
// only sent over HTTPS in production
secure: process.env.NODE_ENV === 'production',
// set cookie to expire after a month
maxAge: 60 * 60 * 24 * 30
});
return { theme };
}
};
23 changes: 23 additions & 0 deletions src/routes/api/v2/search/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { User } from '$lib/dtos/user';
import { guildedMediaLink } from '$lib/utils/guilded-media';
import { error, json } from '@sveltejs/kit';

export const GET = async ({ url }) => {
const userSearch = url.searchParams.get('query');
console.log(userSearch);
let users: User[] = [];
const data = await (
await fetch(
`https://www.guilded.gg/api/search?query=${userSearch}&entityType=user&maxResultsPerType=20`,
{ method: 'GET' }
)
).json();
users = data.results.users;
users = users.map((user: User) => {
return {
...user,
profilePicture: guildedMediaLink(user.profilePicture ?? '/poop.png')
};
});
return json(users);
};
Loading