diff --git a/helpers/find.team.by.name.js b/helpers/find.team.by.name.js new file mode 100644 index 00000000..8e3a54fc --- /dev/null +++ b/helpers/find.team.by.name.js @@ -0,0 +1,12 @@ +import prisma from '~/prisma/client'; + +export default async function findTeamByName(name) { + return prisma.team.findMany({ + where: { + name: { + equals: name, + mode: 'insensitive', + }, + }, + }); +} diff --git a/src/components/pages/team/hero/team.jsx b/src/components/pages/team/hero/team.jsx index b3042570..53d202a7 100644 --- a/src/components/pages/team/hero/team.jsx +++ b/src/components/pages/team/hero/team.jsx @@ -12,6 +12,7 @@ const Team = ({ info }) => { const session = useSession(); const [contact, setContact] = useState(''); + const [currentTeamName, setCurrentTeamName] = useState(info.team.name); // for disable state update button const [teamName, setTeamName] = useState(info.team.name); const [randomJoin, setRandomJoin] = useState(info.team.allowAutoAssign); @@ -37,20 +38,34 @@ const Team = ({ info }) => { setContact(''); }, [contact]); - const debounce = useDebouncedCallback(async (name, allowAutoAssign) => { - await fetch('/api/change-team', { + const updateTeamApi = (name, allowAutoAssign) => + fetch('/api/change-team', { headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, method: 'POST', - body: JSON.stringify({ - name, - allowAutoAssign, - }), + body: JSON.stringify({ name, allowAutoAssign }), }); + + const debounceRandomJoin = useDebouncedCallback(async (allowAutoAssign) => { + await updateTeamApi(currentTeamName, allowAutoAssign); }, 500); + const updateTeamName = async () => { + const response = await updateTeamApi(teamName, randomJoin); + const json = await response.json(); + + if (response.ok) { + setCurrentTeamName(teamName); + toast.success('Team name updated successfully!'); + } else { + toast.error(json.error); + } + }; + + const handleChangeTeamName = (e) => setTeamName(e.target.value); + const leaveTeam = async () => { if (!currentUser) return; @@ -178,12 +193,19 @@ const Team = ({ info }) => { type="text" name="name" value={teamName} - onChange={(e) => { - setTeamName(e.target.value); - debounce(e.target.value, randomJoin); - }} + onChange={handleChangeTeamName} /> +