Skip to content

Commit

Permalink
Рефакторинг
Browse files Browse the repository at this point in the history
  • Loading branch information
DeDxYk594 committed Dec 14, 2024
1 parent bda5822 commit 3abb45e
Show file tree
Hide file tree
Showing 37 changed files with 248 additions and 293 deletions.
49 changes: 8 additions & 41 deletions src/api/boards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Board } from '@/types/board';
import {
apiGet,
apiPost,
Expand All @@ -11,16 +10,12 @@ import {
HTTP_STATUS_UNAUTHORIZED,
useMocks,
} from '@/api/apiHelper';
import { ActiveBoard, BoardColumn } from '@/types/activeBoard';
import { ActiveBoard, Board, BoardColumn } from '@/types/types';
import { activeBoardMock } from './mocks/activeBoard';
import { myBoardsMock } from './mocks/myBoards';
import {
BoardContentResponse,
BoardInfoResponse,
BoardResponse,
} from './responseTypes';
import { BoardContentResponse, BoardResponse } from './responseTypes';
import { showToast } from '@/stores/toastNotificationStore';
import { decodeCard } from './decode';
import { decodeActiveBoard, decodeBoard, decodeCard } from './decode';

// Получить все доски пользователя
export const getBoards = async (): Promise<Board[]> => {
Expand All @@ -30,18 +25,10 @@ export const getBoards = async (): Promise<Board[]> => {

try {
const response = await apiGet('/boards/my');
const json: BoardInfoResponse[] = await response.body;
const json: BoardResponse[] = await response.body;

if (response.status === HTTP_STATUS_OK) {
return json.map(
(el: BoardInfoResponse): Board => ({
id: el.id,
title: el.name,
lastUpdate: new Date(el.updatedAt),
lastVisit: new Date(el.updatedAt), // TODO избавиться от недоразумения
backgroundImageUrl: el.backgroundImageUrl,
})
);
return json.map((el: BoardResponse): Board => decodeBoard(el));
} else {
handleErrors(response.status, 'Получение списка досок');
}
Expand Down Expand Up @@ -81,15 +68,7 @@ export const getBoardContent = async (
columns[colIdx].cards.push(decodeCard(card));
});

return {
id: boardContentResponse.boardInfo.id,
title: boardContentResponse.boardInfo.name,
columns, // обновленный массив columns
myRole: boardContentResponse.myRole,
lastUpdate: new Date(boardContentResponse.boardInfo.updatedAt),
lastVisit: new Date(boardContentResponse.boardInfo.updatedAt),
backgroundImageUrl: boardContentResponse.boardInfo.backgroundImageUrl,
};
return decodeActiveBoard(boardContentResponse);
} else {
handleErrors(response.status, 'Get board content');
}
Expand All @@ -109,13 +88,7 @@ export const createBoard = async (boardName: string): Promise<Board> => {
response.status === HTTP_STATUS_CREATED
) {
const boardInfo: BoardResponse = response.body;
return {
id: boardInfo.id,
title: boardInfo.name,
lastUpdate: new Date(boardInfo.updatedAt),
lastVisit: new Date(boardInfo.updatedAt), // TODO избавиться от недоразумения
backgroundImageUrl: boardInfo.backgroundImageUrl,
};
return decodeBoard(boardInfo);
} else {
throw new Error('Unexpected error in createBoard');
}
Expand All @@ -138,13 +111,7 @@ export const updateBoard = async (

if (response.status === HTTP_STATUS_OK) {
const updatedBoard: BoardResponse = response.body;
return {
id: updatedBoard.id,
title: updatedBoard.name,
lastUpdate: new Date(updatedBoard.updatedAt),
lastVisit: new Date(updatedBoard.updatedAt), //TODO избавиться от неоднозначности
backgroundImageUrl: updatedBoard.backgroundImageUrl,
};
return decodeBoard(updatedBoard);
} else {
handleErrors(response.status, 'Update board');
throw new Error('Ошибка при обновлении доски');
Expand Down
4 changes: 2 additions & 2 deletions src/api/cardDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
CardComment,
CardDetails,
CheckListField,
} from '@/types/card';
User,
} from '@/types/types';
import {
apiDelete,
apiGet,
Expand Down Expand Up @@ -33,7 +34,6 @@ import {
decodeComment,
decodeUser,
} from './decode';
import { User } from '@/types/user';
import { showToast } from '@/stores/toastNotificationStore';

export const assignUser = async (
Expand Down
3 changes: 1 addition & 2 deletions src/api/columnsCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ import {
HTTP_STATUS_UNAUTHORIZED,
} from './apiHelper';
import { CardResponse } from './responseTypes';
import { BoardColumn } from '@/types/activeBoard';
import { Card } from '@/types/card';
import { BoardColumn, Card } from '@/types/types';
import { CardPatchRequest, CardRequest, ColumnRequest } from './requestTypes';
import { decodeCard } from './decode';

Expand Down
38 changes: 35 additions & 3 deletions src/api/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import {
CardDetails,
CheckListField,
RealCard,
} from '@/types/card';
User,
Board,
ActiveBoard,
BoardColumn,
UserToBoard,
} from '@/types/types';
import {
AttachmentResponse,
BoardContentResponse,
BoardResponse,
CardDetailsResponse,
CardResponse,
CheckListFieldResponse,
CommentResponse,
MemberWithPermissionsResponse,
UserResponse,
} from './responseTypes';
import { User, UserToBoard as Member } from '@/types/user';

export const decodeUser = (user: UserResponse): User => {
return { ...user, pollQuestions: user.pollQuestions };
Expand All @@ -28,7 +34,7 @@ export const decodeCard = (card: CardResponse): RealCard => {
};
};

export const decodeMember = (r: MemberWithPermissionsResponse): Member => {
export const decodeMember = (r: MemberWithPermissionsResponse): UserToBoard => {
return {
user: decodeUser(r.user),
addedBy: {
Expand Down Expand Up @@ -71,3 +77,29 @@ export const decodeAttachment = (r: AttachmentResponse): Attachment => {
...r,
};
};

export const decodeBoard = (r: BoardResponse): Board => {
return {
...r,
title: r.name,
lastUpdate: new Date(r.updatedAt),
lastVisit: new Date(r.updatedAt),
myInviteLinkUuid: r.myInviteLinkUuid,
};
};

export const decodeActiveBoard = (r: BoardContentResponse): ActiveBoard => {
const colIndex = new Map<number, number>();
const columns: BoardColumn[] = r.allColumns.map((cr, idx) => {
colIndex.set(cr.id, idx);
return { cards: [], id: cr.id, isStub: false, title: cr.title };
});
r.allCards.forEach((card) => {
columns[colIndex.get(card.columnId) as number].cards.push(decodeCard(card));
});
return {
columns: columns,
board: decodeBoard(r.boardInfo),
myRole: r.myRole,
};
};
29 changes: 3 additions & 26 deletions src/api/members.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { UserToBoard as Member } from '@/types/user';
import { UserToBoard } from '@/types/types';
import {
apiDelete,
apiGet,
apiPost,
apiPut,
HTTP_STATUS_CONFLICT,
HTTP_STATUS_OK,
} from './apiHelper';
import { showToast } from '@/stores/toastNotificationStore';
Expand All @@ -13,7 +11,7 @@ import { decodeMember } from './decode';

export const getBoardPermissions = async (
boardId: number
): Promise<Member[]> => {
): Promise<UserToBoard[]> => {
const response = await apiGet(`/userPermissions/board_${boardId}`);
switch (response.status) {
case HTTP_STATUS_OK: {
Expand All @@ -26,27 +24,6 @@ export const getBoardPermissions = async (
}
};

export const addMember = async (
boardId: number,
nickname: string
): Promise<Member> => {
const response = await apiPost(`/userPermissions/board_${boardId}`, {
nickname,
});
switch (response.status) {
case HTTP_STATUS_OK: {
const data = response.body as MemberWithPermissionsResponse;
return decodeMember(data);
}
case HTTP_STATUS_CONFLICT:
showToast('Похоже, участник уже есть', 'error');
throw new Error('Неизвестная ошибка');
default:
showToast('Ошибка при добавлении участника', 'error');
throw new Error('Неизвестная ошибка');
}
};

export const removeMember = async (boardId: number, userId: number) => {
const response = await apiDelete(
`/userPermissions/board_${boardId}/user_${userId}`
Expand All @@ -56,7 +33,7 @@ export const removeMember = async (boardId: number, userId: number) => {
return;
}
default:
showToast('Ошибка при изгнании участника', 'error');
showToast('Ошибка при удалении участника', 'error');
throw new Error('Неизвестная ошибка');
}
};
Expand Down
15 changes: 9 additions & 6 deletions src/api/mocks/activeBoard.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { ActiveBoard } from '@/types/activeBoard';
import { ActiveBoard } from '@/types/types';

export const activeBoardMock: ActiveBoard = {
id: 0,
title: 'Моя любимая доска',
board: {
id: 0,
title: 'Моя любимая доска',
backgroundImageUrl: '/static/img/backgroundPicture.png',
myInviteLinkUuid: undefined,
lastVisit: new Date(Date.UTC(2004, 7, 10)),
lastUpdate: new Date(Date.UTC(2024, 7, 10)),
},
columns: [
{
id: 1,
Expand Down Expand Up @@ -32,7 +38,4 @@ export const activeBoardMock: ActiveBoard = {
},
],
myRole: 'admin',
lastVisit: new Date(Date.UTC(2004, 7, 10)),
lastUpdate: new Date(Date.UTC(2024, 7, 10)),
backgroundImageUrl: '/static/img/backgroundPicture.png',
};
3 changes: 2 additions & 1 deletion src/api/mocks/myBoards.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Board } from '@/types/board';
import { Board } from '@/types/types';

export const myBoardsMock: Board[] = [
{
Expand All @@ -7,5 +7,6 @@ export const myBoardsMock: Board[] = [
lastVisit: new Date(Date.UTC(2004, 7, 10)),
lastUpdate: new Date(Date.UTC(2024, 7, 10)),
backgroundImageUrl: '/static/img/backgroundImage.png',
myInviteLinkUuid: undefined,
},
];
2 changes: 1 addition & 1 deletion src/api/mocks/poll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CsatQuestion } from '@/types/poll';
import { CsatQuestion } from '@/types/types';

export const pollMock: CsatQuestion[] = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/api/mocks/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User } from '@/types/user';
import { User } from '@/types/types';

export const userMeMock: User = {
id: 123,
Expand Down
2 changes: 1 addition & 1 deletion src/api/poll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PollResults, PollSubmit } from '@/types/poll';
import { PollResults, PollSubmit } from '@/types/types';
import { apiGet, apiPost, HTTP_STATUS_OK } from './apiHelper';
import { showToast } from '@/stores/toastNotificationStore';

Expand Down
14 changes: 3 additions & 11 deletions src/api/responseTypes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CsatQuestion } from '@/types/poll';
import { CsatQuestion } from "@/types/types";

export interface BoardContentResponse {
myRole: 'viewer' | 'editor' | 'editor_chief' | 'admin';
boardInfo: BoardInfoResponse;
boardInfo: BoardResponse;
allColumns: ColumnResponse[];
allCards: CardResponse[];
}
Expand All @@ -26,22 +26,14 @@ export interface CardResponse {
hasComments: boolean;
}

export interface BoardInfoResponse {
id: number;
name: string;
description: string;
backgroundImageUrl: string;
createdAt: string;
updatedAt: string;
}

export interface BoardResponse {
id: number;
name: string;
description: string;
backgroundImageUrl: string;
createdAt: string;
updatedAt: string;
myInviteLinkUuid?: string;
}

export interface UserResponse {
Expand Down
2 changes: 1 addition & 1 deletion src/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
HTTP_STATUS_CONFLICT,
apiPut,
} from '@/api/apiHelper';
import { User } from '@/types/user';
import { User } from '@/types/types';
import { userMeMock } from './mocks/user';
import { showToast } from '@/stores/toastNotificationStore';
import { UserResponse } from './responseTypes';
Expand Down
4 changes: 2 additions & 2 deletions src/components/BoardCard.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentProps } from '@/jsxCore/types';
import './boardCard.scss';
import { Board } from '@/types/board';
import { Board } from '@/types/types';
import { formatTime } from '@/utils/time';
import { goToUrl } from '@/stores/routerStore';
import { useActiveBoardStore } from '@/stores/activeBoardStore';
Expand All @@ -22,7 +22,7 @@ export const BoardCard = (props: BoardCardProps) => {
<div
className={[
'board-card',
activeBoard?.id === props.board.id ? 'board-card__active' : '',
activeBoard?.board.id === props.board.id ? 'board-card__active' : '',
]}
ON_click={() => {
props.onSelect();
Expand Down
3 changes: 1 addition & 2 deletions src/components/KanbanCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
setActiveBoardStore,
useActiveBoardStore,
} from '@/stores/activeBoardStore';
import { ActiveBoard } from '@/types/activeBoard';
import { ActiveBoard, Card } from '@/types/types';
import { useEffectRefs, useState } from '@/jsxCore/hooks';
import { Button } from './Button';
import { getCardDetails } from '@/api/cardDetails';
import { setCardDetailsStore } from '@/stores/cardDetailsStore';
import { Card } from '@/types/card';
import {
cardHeights,
setEditLock,
Expand Down
4 changes: 2 additions & 2 deletions src/components/KanbanColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@/stores/activeBoardStore';
import { Button } from './Button';
import { EditableText } from './EditableText';
import { ActiveBoard } from '@/types/activeBoard';
import { ActiveBoard } from '@/types/types';
import { showToast } from '@/stores/toastNotificationStore';
import { createCard, deleteColumn, updateColumn } from '@/api/columnsCards';
import { useEffectRefs, useState } from '@/jsxCore/hooks';
Expand Down Expand Up @@ -36,7 +36,7 @@ export const KanbanColumn = (props: KanbanColumnProps) => {
showToast('Длина текста в карточке может быть от 3 символов', 'error');
return;
}
createCard(activeBoard.id, {
createCard(activeBoard.board.id, {
title: newText,
columnId: props.columnId,
}).then((newCard) => {
Expand Down
Loading

0 comments on commit 3abb45e

Please sign in to comment.