try to do better
This commit is contained in:
75
frontend/src/services/inviteService.ts
Normal file
75
frontend/src/services/inviteService.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { inviteLinkApi } from '../api/client';
|
||||
import { CreateInviteLinkRequest, InviteLinkResponse, ValidateInviteResponse, JoinFamilyResponse } from '../types';
|
||||
import { handleApiError } from '../utils/errorHandler';
|
||||
|
||||
export const inviteService = {
|
||||
async create(data: CreateInviteLinkRequest): Promise<InviteLinkResponse> {
|
||||
try {
|
||||
const res = await inviteLinkApi.create(data);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
handleApiError(error);
|
||||
}
|
||||
},
|
||||
|
||||
async getMyLinks(): Promise<InviteLinkResponse[]> {
|
||||
try {
|
||||
const res = await inviteLinkApi.getMyLinks();
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
handleApiError(error);
|
||||
}
|
||||
},
|
||||
|
||||
async delete(token: string): Promise<void> {
|
||||
try {
|
||||
await inviteLinkApi.delete(token);
|
||||
} catch (error) {
|
||||
handleApiError(error);
|
||||
}
|
||||
},
|
||||
|
||||
async validate(token: string): Promise<ValidateInviteResponse> {
|
||||
try {
|
||||
const res = await inviteLinkApi.validate(token);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
handleApiError(error);
|
||||
}
|
||||
},
|
||||
|
||||
async join(token: string): Promise<JoinFamilyResponse> {
|
||||
try {
|
||||
const res = await inviteLinkApi.join(token);
|
||||
return res.data;
|
||||
} catch (error) {
|
||||
handleApiError(error);
|
||||
}
|
||||
},
|
||||
|
||||
isExpired(expiresAt: string | null): boolean {
|
||||
if (!expiresAt) return false;
|
||||
return new Date(expiresAt) < new Date();
|
||||
},
|
||||
|
||||
isMaxUsesReached(link: InviteLinkResponse): boolean {
|
||||
if (link.max_uses === null) return false;
|
||||
return link.uses_count >= link.max_uses;
|
||||
},
|
||||
|
||||
isActive(link: InviteLinkResponse): boolean {
|
||||
return !this.isExpired(link.expires_at) && !this.isMaxUsesReached(link);
|
||||
},
|
||||
|
||||
formatExpiresAt(expiresAt: string | null): string {
|
||||
if (!expiresAt) return 'Never';
|
||||
const date = new Date(expiresAt);
|
||||
return date.toLocaleString('ru-RU', {
|
||||
day: '2-digit',
|
||||
month: '2-digit',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user