feat: search by name

This commit is contained in:
Tristan Yang
2023-06-20 17:38:51 +08:00
parent f07be4c820
commit 521f912b05
3 changed files with 20 additions and 9 deletions
+3 -1
View File
@@ -33,5 +33,7 @@
"search_by_id": "Search by ID", "search_by_id": "Search by ID",
"search_by_id_ph": "Input user ID", "search_by_id_ph": "Input user ID",
"search_by_email": "Search by email", "search_by_email": "Search by email",
"search_by_email_ph": "Input user email" "search_by_email_ph": "Input user email",
"search_by_name": "Search by name",
"search_by_name_ph": "Input user name"
} }
+1 -1
View File
@@ -89,7 +89,7 @@ export const userApi = createApi({
method: "POST" method: "POST"
}) })
}), }),
searchUser: builder.mutation<User, { search_type: "id" | "email"; keyword: string }>({ searchUser: builder.mutation<User, { search_type: "id" | "email" | "name"; keyword: string }>({
query: (input) => ({ query: (input) => ({
url: `/user/search`, url: `/user/search`,
body: input, body: input,
+16 -7
View File
@@ -17,7 +17,7 @@ import Input from "./styled/Input";
type Props = { type Props = {
closeModal: () => void; closeModal: () => void;
}; };
type Type = "id" | "email"; type Type = "id" | "email" | "name";
const SearchUser: FC<Props> = ({ closeModal }) => { const SearchUser: FC<Props> = ({ closeModal }) => {
const [updateContactStatus, { isLoading: adding }] = useUpdateContactStatusMutation(); const [updateContactStatus, { isLoading: adding }] = useUpdateContactStatusMutation();
@@ -25,10 +25,11 @@ const SearchUser: FC<Props> = ({ closeModal }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const navigateTo = useNavigate(); const navigateTo = useNavigate();
const inputRef = useRef(null); const inputRef = useRef(null);
const [type, setType] = useState<Type>("email"); const [type, setType] = useState<Type>("name");
const [input, setInput] = useState({ const [input, setInput] = useState({
id: "", id: "",
email: "" email: "",
name: ""
}); });
const [searchUser, { data, isSuccess, isLoading, reset }] = useSearchUserMutation(); const [searchUser, { data, isSuccess, isLoading, reset }] = useSearchUserMutation();
const handleInput = (evt: ChangeEvent<HTMLInputElement>) => { const handleInput = (evt: ChangeEvent<HTMLInputElement>) => {
@@ -72,19 +73,25 @@ const SearchUser: FC<Props> = ({ closeModal }) => {
// const tmpData = { "avatar_updated_at": 0, "birthday": 0, "create_by": "password", "email": "tristan@alex.com", "gender": 0, "is_admin": true, "is_bot": false, "language": "en-US", "name": "Admin", "uid": 1 }; // const tmpData = { "avatar_updated_at": 0, "birthday": 0, "create_by": "password", "email": "tristan@alex.com", "gender": 0, "is_admin": true, "is_bot": false, "language": "en-US", "name": "Admin", "uid": 1 };
// const showResult = isSuccess && data; // const showResult = isSuccess && data;
const inContact = Boolean(data && usersData[data.uid] && usersData[data.uid].status == "added"); const inContact = Boolean(data && usersData[data.uid] && usersData[data.uid].status == "added");
const inputType = type == "id" ? "number" : "email"; const inputType = type == "id" ? "number" : type == "email" ? "email" : "text";
return ( return (
<Modal> <Modal>
<div className=" relative flex flex-col gap-2 w-96 px-4 py-3 rounded-lg bg-gray-100 dark:bg-gray-900 text-slate-900 dark:text-slate-100"> <div className=" relative flex flex-col gap-2 w-96 px-4 py-3 rounded-lg bg-gray-100 dark:bg-gray-900 text-slate-900 dark:text-slate-100">
<div className="flex items-center gap-2 py-2"> <div className="flex items-center gap-2 py-2">
<StyledButton <StyledButton
className={clsx("mini", type == "id" && "ghost !border-none !shadow-none")} className={clsx("mini", type !== "name" && "ghost !border-none !shadow-none")}
onClick={handleChangeKeyword.bind(null, "name")}
>
{t("search_by_name", { ns: "member" })}
</StyledButton>
<StyledButton
className={clsx("mini", type !== "email" && "ghost !border-none !shadow-none")}
onClick={handleChangeKeyword.bind(null, "email")} onClick={handleChangeKeyword.bind(null, "email")}
> >
{t("search_by_email", { ns: "member" })} {t("search_by_email", { ns: "member" })}
</StyledButton> </StyledButton>
<StyledButton <StyledButton
className={clsx("mini", type == "email" && "ghost !border-none !shadow-none")} className={clsx("mini", type !== "id" && "ghost !border-none !shadow-none")}
onClick={handleChangeKeyword.bind(null, "id")} onClick={handleChangeKeyword.bind(null, "id")}
> >
{t("search_by_id", { ns: "member" })} {t("search_by_id", { ns: "member" })}
@@ -101,7 +108,9 @@ const SearchUser: FC<Props> = ({ closeModal }) => {
placeholder={`${ placeholder={`${
type == "email" type == "email"
? t("search_by_email_ph", { ns: "member" }) ? t("search_by_email_ph", { ns: "member" })
: t("search_by_id_ph", { ns: "member" }) : type == "id"
? t("search_by_id_ph", { ns: "member" })
: t("search_by_name_ph", { ns: "member" })
}...`} }...`}
onChange={handleInput} onChange={handleInput}
/> />