Skip to content

Commit 9a35810

Browse files
committed
全ユーザーを取得するように
1 parent d0dfc8f commit 9a35810

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

web/api/user.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import type { Hook as UseHook } from "./share/types.ts";
1010

1111
const UserListSchema = z.array(UserSchema);
1212

13+
export function useAll(): Hook<User[]> {
14+
return useCustomizedSWR("users::all", all, UserListSchema);
15+
}
1316
export function useRecommended(): UseHook<User[]> {
1417
const url = endpoints.recommendedUsers;
1518
return useAuthorizedData<User[]>(url);
@@ -28,6 +31,11 @@ export function usePendingFromMe(): Hook<User[]> {
2831
);
2932
}
3033

34+
async function all(): Promise<User[]> {
35+
const res = await credFetch("GET", endpoints.users);
36+
return res.json();
37+
}
38+
3139
async function matched(): Promise<User[]> {
3240
const res = await credFetch("GET", endpoints.matchedUsers);
3341
return res.json();
@@ -131,6 +139,8 @@ export async function deleteAccount(): Promise<void> {
131139
const res = await credFetch("DELETE", endpoints.me);
132140
if (res.status !== 204)
133141
throw new Error(
134-
`failed to delete account: expected status code 204, but got ${res.status} with text ${await res.text()}`,
142+
`failed to delete account: expected status code 204, but got ${
143+
res.status
144+
} with text ${await res.text()}`,
135145
);
136146
}

web/app/search/page.tsx

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,29 @@
11
"use client";
22

3+
import type { User } from "common/types";
34
import type React from "react";
45
import { useEffect, useState } from "react";
5-
import { useRecommended } from "~/api/user";
6+
import { useAll } from "~/api/user";
67

78
import { useModal } from "~/components/common/modal/ModalProvider";
89
import { HumanListItem } from "~/components/human/humanListItem";
910

10-
const SearchPage: React.FC = () => {
11+
export default function SearchPage() {
1112
const [searchWord, setSearchWord] = useState("");
12-
const { data: recommended } = useRecommended();
13+
const {
14+
state: { data },
15+
} = useAll();
1316
const { openModal } = useModal();
14-
const [users, setUsers] = useState<
15-
| {
16-
id: number;
17-
guid: string;
18-
name: string;
19-
gender: string;
20-
grade: string;
21-
faculty: string;
22-
department: string;
23-
intro: string;
24-
pictureUrl: string;
25-
}[]
26-
| null
27-
>(null);
17+
const [users, setUsers] = useState<User[] | null>(null);
2818

2919
useEffect(() => {
30-
if (recommended) {
31-
setUsers(recommended);
20+
if (data) {
21+
setUsers(data);
3222
}
33-
}, [recommended]);
23+
}, [data]);
3424

3525
function searchByUserName() {
36-
const filteredUsers = recommended?.filter((user) =>
26+
const filteredUsers = data?.filter((user) =>
3727
user.name.toLowerCase().includes(searchWord.toLowerCase()),
3828
);
3929
setUsers(filteredUsers || null);
@@ -43,7 +33,6 @@ const SearchPage: React.FC = () => {
4333
<div className="flex min-h-screen items-center justify-center bg-gray-100">
4434
<div className="card w-96 bg-white p-6 shadow-md">
4535
<h2 className="mb-4 font-bold text-2xl">Search</h2>
46-
4736
<div className="form-control mb-4">
4837
<label htmlFor="searchInput" className="label">
4938
<span className="label-text">Enter search term</span>
@@ -56,7 +45,6 @@ const SearchPage: React.FC = () => {
5645
className="input input-bordered w-full"
5746
/>
5847
</div>
59-
6048
<button
6149
type="button"
6250
onClick={searchByUserName}
@@ -77,6 +65,4 @@ const SearchPage: React.FC = () => {
7765
</div>
7866
</div>
7967
);
80-
};
81-
82-
export default SearchPage;
68+
}

0 commit comments

Comments
 (0)