Skip to content

Commit 3fdbe42

Browse files
committed
fix: участие в субботнике
1 parent f50cdce commit 3fdbe42

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { useGetPaginatedManyTemplate } from '@hooks/templates/get/useGetPaginatedManyTemplate';
2+
import substituteIdToEndpoint from '@/utils/api/substituteIdToEndpoint';
3+
import { Requirement } from '@models/deleteMeLater.ts';
4+
5+
// Интерфейс для API-модели требования
6+
export interface RequirementApiModel {
7+
id: number;
8+
name: string;
9+
// Можно добавить другие поля, если они есть в API
10+
}
11+
12+
// Константа с эндпоинтом
13+
const GET_CLEANDAY_REQUIREMENTS = '/api/cleandays/{id}/requirements';
14+
15+
/**
16+
* Хук для получения списка требований/условий субботника
17+
* @param cleandayId - ID субботника
18+
* @param params - Параметры запроса (пагинация, сортировка)
19+
*/
20+
export function useGetCleandayRequirements(cleandayId: string | number, params?: Record<string, unknown>) {
21+
const endpoint = substituteIdToEndpoint(cleandayId, GET_CLEANDAY_REQUIREMENTS);
22+
23+
return useGetPaginatedManyTemplate<
24+
RequirementApiModel,
25+
Requirement
26+
>(
27+
['cleanday', cleandayId, 'requirements'],
28+
endpoint,
29+
'contents',
30+
params,
31+
{
32+
// При ошибке не показываем уведомление
33+
onError: (error) => {
34+
console.error('Error fetching cleanday requirements:', error);
35+
}
36+
},
37+
// Функция трансформации API-модели в модель приложения
38+
(apiModel: RequirementApiModel): Requirement => ({
39+
id: apiModel.id,
40+
name: apiModel.name,
41+
})
42+
);
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { usePostTemplate } from '@hooks/templates/create/usePostTemplate';
2+
import substituteIdToEndpoint from '@/utils/api/substituteIdToEndpoint';
3+
import { ParticipationStatus } from '@models/deleteMeLater.ts';
4+
5+
// Интерфейс для запроса на присоединение к субботнику
6+
interface JoinCleandayRequest {
7+
type: string;
8+
requirements?: number[];
9+
}
10+
11+
// Константа с эндпоинтом
12+
const JOIN_CLEANDAY_ENDPOINT = '/api/cleandays/{id}/members';
13+
14+
/**
15+
* Преобразует ParticipationStatus в строку для API
16+
*/
17+
function mapStatusToApiType(status: ParticipationStatus): string {
18+
switch (status) {
19+
case ParticipationStatus.GOING:
20+
return "Точно пойду";
21+
case ParticipationStatus.LATE:
22+
return "Опоздаю";
23+
case ParticipationStatus.MAYBE:
24+
return "Возможно пойду";
25+
case ParticipationStatus.NOT_GOING:
26+
return "Не пойду";
27+
default:
28+
return "Точно пойду";
29+
}
30+
}
31+
32+
/**
33+
* Хук для добавления текущего пользователя в список участников субботника
34+
* @param cleandayId - ID субботника
35+
*/
36+
export function useJoinCleanday(cleandayId: string | number) {
37+
const endpoint = substituteIdToEndpoint(cleandayId, JOIN_CLEANDAY_ENDPOINT);
38+
39+
return usePostTemplate<JoinCleandayRequest, any>(
40+
endpoint,
41+
{
42+
// Обработка ошибок
43+
onError: (error) => {
44+
console.error('Error joining cleanday:', error);
45+
}
46+
}
47+
);
48+
}
49+
50+
/**
51+
* Подготавливает данные для API из ParticipationStatus и списка требований
52+
*/
53+
export function prepareJoinCleandayRequest(status: ParticipationStatus, requirements?: number[]): JoinCleandayRequest {
54+
return {
55+
type: mapStatusToApiType(status),
56+
requirements
57+
};
58+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { useMutation } from '@tanstack/react-query';
2+
import axiosInstance from '@/axiosInstance.ts';
3+
import substituteIdToEndpoint from '@/utils/api/substituteIdToEndpoint';
4+
import { ParticipationStatus } from '@models/deleteMeLater.ts';
5+
import { UPDATE_PARTICIPATION } from '@api/cleanday/endpoints';
6+
7+
// Интерфейс для запроса на обновление участия
8+
interface UpdateParticipationRequest {
9+
type?: string;
10+
requirement_keys?: string[];
11+
}
12+
13+
/**
14+
* Преобразует ParticipationStatus в строку для API
15+
*/
16+
function mapStatusToApiType(status: ParticipationStatus): string {
17+
switch (status) {
18+
case ParticipationStatus.GOING:
19+
return "Точно пойду";
20+
case ParticipationStatus.LATE:
21+
return "Опоздаю";
22+
case ParticipationStatus.MAYBE:
23+
return "Возможно пойду";
24+
case ParticipationStatus.NOT_GOING:
25+
return "Не пойду";
26+
default:
27+
return "Точно пойду";
28+
}
29+
}
30+
31+
/**
32+
* Хук для обновления участия пользователя в субботнике
33+
* @param cleandayId - ID субботника
34+
*/
35+
export function useUpdateParticipation(cleandayId: string | number) {
36+
const endpoint = substituteIdToEndpoint(cleandayId, UPDATE_PARTICIPATION);
37+
38+
return useMutation({
39+
mutationFn: async (data: UpdateParticipationRequest) => {
40+
const response = await axiosInstance.patch(endpoint, data);
41+
return response.data;
42+
},
43+
onError: (error) => {
44+
console.error('Error updating participation:', error);
45+
}
46+
});
47+
}
48+
49+
/**
50+
* Подготавливает данные для API из ParticipationStatus и списка требований
51+
*/
52+
export function prepareUpdateParticipationRequest(
53+
status?: ParticipationStatus,
54+
requirementIds?: number[]
55+
): UpdateParticipationRequest {
56+
const request: UpdateParticipationRequest = {};
57+
58+
if (status) {
59+
request.type = mapStatusToApiType(status);
60+
}
61+
62+
if (requirementIds && requirementIds.length > 0) {
63+
request.requirement_keys = requirementIds.map(id => id.toString());
64+
}
65+
66+
return request;
67+
}

0 commit comments

Comments
 (0)