mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Add public user profile page at /users/:id
This commit is contained in:
56
web/frontend/app/features/users/users.server.ts
Normal file
56
web/frontend/app/features/users/users.server.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { data } from 'react-router';
|
||||
|
||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
export interface PublicUserProfile {
|
||||
userAccountId: string;
|
||||
username: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the public-facing slice of a user account. `GET /api/user/{id}` also returns
|
||||
* `email` and `dateOfBirth`, but those stay out of anything rendered on a public profile.
|
||||
*/
|
||||
export const getPublicUserProfile = async (
|
||||
userAccountId: string,
|
||||
): Promise<PublicUserProfile | null> => {
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(`${API_BASE_URL}/api/user/${encodeURIComponent(userAccountId)}`);
|
||||
} catch {
|
||||
throw data('The user service is unreachable right now. Please try again in a moment.', {
|
||||
status: 503,
|
||||
statusText: 'Service Unavailable',
|
||||
});
|
||||
}
|
||||
|
||||
if (res.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
throw data(`Failed to load user profile (${res.status}).`, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
});
|
||||
}
|
||||
|
||||
const account: {
|
||||
userAccountId: string;
|
||||
username: string;
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
createdAt: string;
|
||||
} = await res.json();
|
||||
|
||||
return {
|
||||
userAccountId: account.userAccountId,
|
||||
username: account.username,
|
||||
firstName: account.firstName,
|
||||
lastName: account.lastName,
|
||||
createdAt: account.createdAt,
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user