Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/MOVIEJOJO7/cat-talk into Fe…
Browse files Browse the repository at this point in the history
…ature/#2
  • Loading branch information
hhjs2 committed Nov 9, 2023
2 parents cb30f68 + d9cfff3 commit c51ee63
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 6 deletions.
28 changes: 28 additions & 0 deletions Components/Search/OpenChatPicture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { User } from '@/app/search/search.type';

const OpenChatPicture = ({ openChatUsers }: { openChatUsers: User[] }) => {
let userCount = 0;

return (
<>
<ol>
{openChatUsers.map((user) => {
userCount++;

if (userCount > 4) {
return null; // 사진이 4개 이상인 경우 렌더링을 하지 않음
}

return (
<li key={user.id}>
<img src={user.picture} alt="user picture" />
</li>
);
})}
</ol>
</>
);
};

export default OpenChatPicture;
14 changes: 14 additions & 0 deletions Components/Search/OpenChatText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import { Chat } from '@/app/search/search.type';

const OpenChatText = ({ openChat }: { openChat: Chat }) => {
return (
<>
<h2>{openChat.name}</h2>
<span>{openChat.users.length}</span>
<span>{openChat.updatedAt.toString()}</span>
</>
);
};

export default OpenChatText;
29 changes: 29 additions & 0 deletions Components/Search/SearchOpenChat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use client';

import React, { useState } from 'react';
import { AllOpenChat } from '@/app/search/search.type';
import ShowAllOpenChat from './ShowAllOpenChat';

const SearchOpenChat = ({ allOpenChat }: { allOpenChat: AllOpenChat }) => {
const [userInput, setUserInput] = useState('');
const [openChats] = useState<AllOpenChat>(allOpenChat);

const getUserInput = (e: React.ChangeEvent<HTMLInputElement>) => {
setUserInput(e.target.value.toLowerCase().replace(/(\s*)/g, ''));
};

const searched = openChats.filter((item) =>
item.name.toLowerCase().replace(/(\s*)/g, '').includes(userInput),
);

return (
<>
<input onChange={getUserInput} />
{searched.map((item) => (
<ShowAllOpenChat key={item.id} openChat={item} />
))}
</>
);
};

export default SearchOpenChat;
19 changes: 19 additions & 0 deletions Components/Search/ShowAllOpenChat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { Chat } from '@/app/search/search.type';
import OpenChatText from './OpenChatText';
import OpenChatPicture from './OpenChatPicture';

const ShowAllOpenChat = ({ openChat }: { openChat: Chat }) => {
return (
<ul>
{
<li key={openChat.id}>
<OpenChatText openChat={openChat} />
<OpenChatPicture openChatUsers={openChat.users} />
</li>
}
</ul>
);
};

export default ShowAllOpenChat;
6 changes: 3 additions & 3 deletions app/search/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import React from 'react';
import { fetchAllOpenChat } from './search.utils';
import SearchOpenChat from '../../Components/Search/SearchOpenChat';

const accessToken = process.env.ACCESSTOKEN as string; // 임시 access token
const accessToken = process.env.NEXT_PUBLIC_ACCESSTOKEN as string; // 임시 access token

const Search = async () => {
const allOpenChat = await fetchAllOpenChat(accessToken);
console.log(allOpenChat);

return (
<>
<h1>Search 페이지</h1>
<SearchOpenChat allOpenChat={allOpenChat} />
</>
);
};
Expand Down
6 changes: 4 additions & 2 deletions app/search/search.type.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// search.type.ts

export type AllOpenChat = Chat[];

export type AllOpenChatJSON = {
chats: AllOpenChat;
};

type Chat = {
export type Chat = {
id: string;
name: string;
users: User[];
Expand All @@ -13,7 +15,7 @@ type Chat = {
updatedAt: Date;
};

type User = {
export type User = {
id: string;
name: string;
picture: string;
Expand Down
2 changes: 1 addition & 1 deletion app/search/search.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const fetchAllOpenChat = async (accessToken: string) => {
method: GET,
headers: {
'content-type': CONTENT_TYPE,
serverId: process.env.SERVER_ID as string, // 서버 아이디 임시 사용
serverId: process.env.NEXT_PUBLIC_SERVER_ID as string, // 서버 아이디 임시 사용
Authorization: `Bearer ${accessToken}`,
},
};
Expand Down

0 comments on commit c51ee63

Please sign in to comment.