Files
Checkmate/client/src/pages/teams/TeamsConfig.tsx
T

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2025-11-13 13:47:26 -08:00
import { TeamsForm } from "@/pages/teams/TeamsForm";
import { useNavigate, useParams } from "react-router";
import { z } from "zod";
import { teamSchema } from "@/validation/zod";
2025-12-15 23:36:50 +00:00
import { useGet, usePatch, useGetOnDemand } from "@/hooks/UseApi";
2025-11-13 13:47:26 -08:00
import type { ApiResponse } from "@/hooks/UseApi";
2025-12-15 23:36:50 +00:00
import type { IUser } from "@/types/user";
import { useAppDispatch } from "@/hooks/AppHooks";
import { setUser } from "@/features/authSlice";
2025-12-16 18:15:47 +00:00
import { mutate } from "swr";
2025-11-13 13:47:26 -08:00
const TeamsConfigPage = () => {
type FormValues = Omit<z.infer<typeof teamSchema>, "roleId">;
const navigate = useNavigate();
const { response } = useGet<ApiResponse<any>>("/roles?type=team");
const { patch, loading } = usePatch<FormValues, ApiResponse<any>>();
2025-12-15 23:36:50 +00:00
const { get: getOnDemand } = useGetOnDemand<IUser>();
const dispatch = useAppDispatch();
2025-11-13 13:47:26 -08:00
const { id } = useParams();
const { response: teamResponse } = useGet<ApiResponse<any>>(`/teams/${id}`);
const team = teamResponse?.data || null;
const roles = response?.data || [];
const onSubmit = async (data: FormValues) => {
const toSubmit = {
name: data.name,
description: data.description,
};
await patch(`/teams/${id}`, toSubmit);
2025-12-16 18:15:47 +00:00
mutate("/teams/joined");
2025-12-15 23:36:50 +00:00
const me = await getOnDemand("/me");
if (me?.data) {
dispatch(setUser(me.data));
}
2025-11-13 13:47:26 -08:00
navigate("/teams");
};
return (
<TeamsForm
mode="config"
initialData={{ ...team, roleId: roles[0]?._id }}
onSubmit={onSubmit}
loading={loading}
roles={roles}
/>
);
};
export default TeamsConfigPage;