api client refactor
This commit is contained in:
@@ -1,82 +1,6 @@
|
||||
/**
|
||||
* TeamDetailsPresenter - Pure data transformer
|
||||
* Transforms API response to view model without DI dependencies.
|
||||
*/
|
||||
import { TeamDetailsDto, TeamMemberDto } from '../dtos';
|
||||
import { TeamDetailsViewModel } from '../view-models';
|
||||
|
||||
import { apiClient, type TeamDetailsViewModel as ApiTeamDetailsViewModel } from '@/lib/apiClient';
|
||||
|
||||
export interface TeamMembershipViewModel {
|
||||
role: string;
|
||||
joinedAt: string;
|
||||
isActive: boolean;
|
||||
}
|
||||
|
||||
export interface TeamInfoViewModel {
|
||||
id: string;
|
||||
name: string;
|
||||
tag?: string | undefined;
|
||||
description?: string | undefined;
|
||||
ownerId: string;
|
||||
leagues?: string[] | undefined;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface TeamDetailsViewModel {
|
||||
team: TeamInfoViewModel;
|
||||
membership: TeamMembershipViewModel | null;
|
||||
canManage: boolean;
|
||||
}
|
||||
|
||||
export interface ITeamDetailsPresenter {
|
||||
reset(): void;
|
||||
getViewModel(): TeamDetailsViewModel | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform API response to view model
|
||||
*/
|
||||
function transformApiResponse(apiResponse: ApiTeamDetailsViewModel): TeamDetailsViewModel {
|
||||
return {
|
||||
team: {
|
||||
id: apiResponse.id,
|
||||
name: apiResponse.name,
|
||||
description: apiResponse.description,
|
||||
ownerId: apiResponse.ownerId,
|
||||
createdAt: new Date().toISOString(), // Would need from API
|
||||
},
|
||||
membership: null, // Would need from API based on current user
|
||||
canManage: false, // Would need from API based on current user
|
||||
};
|
||||
}
|
||||
|
||||
export class TeamDetailsPresenter implements ITeamDetailsPresenter {
|
||||
private viewModel: TeamDetailsViewModel | null = null;
|
||||
|
||||
reset(): void {
|
||||
this.viewModel = null;
|
||||
}
|
||||
|
||||
async fetchAndPresent(teamId: string): Promise<void> {
|
||||
const apiResponse = await apiClient.teams.getDetails(teamId);
|
||||
if (apiResponse) {
|
||||
this.viewModel = transformApiResponse(apiResponse);
|
||||
} else {
|
||||
this.viewModel = null;
|
||||
}
|
||||
}
|
||||
|
||||
getViewModel(): TeamDetailsViewModel | null {
|
||||
return this.viewModel;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience function to fetch and transform team details
|
||||
*/
|
||||
export async function fetchTeamDetails(teamId: string): Promise<TeamDetailsViewModel | null> {
|
||||
const apiResponse = await apiClient.teams.getDetails(teamId);
|
||||
if (!apiResponse) {
|
||||
return null;
|
||||
}
|
||||
return transformApiResponse(apiResponse);
|
||||
}
|
||||
export const presentTeamDetails = (dto: TeamDetailsDto, currentUserId: string): TeamDetailsViewModel => {
|
||||
return new TeamDetailsViewModel(dto, currentUserId);
|
||||
};
|
||||
Reference in New Issue
Block a user