Skip to content

Commit

Permalink
fix(client): production issues (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayllyz authored Jul 7, 2024
1 parent cab070a commit d95fdcc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
9 changes: 5 additions & 4 deletions apps/api/src/libs/hiearchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface User {
export function buildHierarchy(users: User[]): TreeNode {
const userMap = new Map<string, User>();
for (const user of users) {
if (!user.roles) {
continue;
}
userMap.set(String(user.id), user);
}

Expand Down Expand Up @@ -64,14 +67,12 @@ export function buildHierarchy(users: User[]): TreeNode {
],
};

const findAndSetUser = (node: TreeNode | undefined, roleName: Role_name) => {
const findAndSetUser = (node: TreeNode | undefined, roleName: Role_name): void => {
if (!node) {
return;
}
const user = users.find((u) => u.roles.some((role) => role.name === roleName));
if (user) {
node.name = `${user.first_name} ${user.last_name}`;
}
node.name = user ? `${user.first_name} ${user.last_name}` : 'N/A';
};

findAndSetUser(treeData, 'PRESIDENT');
Expand Down
5 changes: 4 additions & 1 deletion apps/client/app/(auth)/(members)/members/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ export interface Hierarchy {
}

export async function getHierarchy(): Promise<Hierarchy> {
const API_URL = process.env.ATHLONIX_API_URL;
const API_URL = process.env.ATHLONIX_API_URL as string;
const token = cookies().get('access_token')?.value;
if (!token) {
throw new Error('No token found');
}
const response = await fetch(`${API_URL}/users/hierarchy`, {
headers: { Authorization: `Bearer ${token}` },
});
Expand Down
13 changes: 9 additions & 4 deletions apps/client/app/(auth)/(members)/members/chat/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function getMessages(): Promise<{ data: Message[]; count: number }>
return { data: [], count: 0 };
}
const token = cookies().get('access_token')?.value;
const urlApi = process.env.ATHLONIX_API_URL;
const urlApi = process.env.ATHLONIX_API_URL as string;

const res = await fetch(`${urlApi}/messages?all=true`, {
method: 'GET',
Expand All @@ -25,7 +25,12 @@ export async function getMessages(): Promise<{ data: Message[]; count: number }>
Authorization: `Bearer ${token}`,
},
});
return res.json();

if (!res.ok) {
throw new Error('Failed to fetch messages');
}

return await res.json();
}

export async function sendMessage(message: { id_sender: number; message: string }): Promise<Message | null> {
Expand All @@ -40,7 +45,7 @@ export async function sendMessage(message: { id_sender: number; message: string
},
body: JSON.stringify(message),
});
return res.json();
return await res.json();
}

export async function updateMessage(id: number, message: string): Promise<Message | null> {
Expand All @@ -55,7 +60,7 @@ export async function updateMessage(id: number, message: string): Promise<Messag
},
body: JSON.stringify({ message }),
});
return res.json();
return await res.json();
}

export async function deleteMessage(id: number): Promise<void> {
Expand Down
5 changes: 3 additions & 2 deletions apps/client/app/(auth)/(members)/members/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default function ChatView() {
useEffect(() => {
const fetchMessages = async () => {
const messages = await getMessages();
if (!messages) return;
setMessages(messages);
};
fetchMessages();
Expand All @@ -54,7 +55,7 @@ export default function ChatView() {
if (message) {
setMessages((prev) => ({
...prev,
data: prev.data.map((m) => (m.id === message.id ? message : m)),
data: prev?.data.map((m) => (m.id === message.id ? message : m)),
}));
setEditingId(null);
editForm.reset();
Expand All @@ -66,7 +67,7 @@ export default function ChatView() {
await deleteMessage(messageId);
setMessages((prev) => ({
...prev,
data: prev.data.filter((m) => m.id !== messageId),
data: prev?.data.filter((m) => m.id !== messageId),
}));
};

Expand Down

0 comments on commit d95fdcc

Please sign in to comment.