Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prevent duplicated team name #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions helpers/find.team.by.name.js
Original file line number Diff line number Diff line change
@@ -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',
},
},
});
}
44 changes: 33 additions & 11 deletions src/components/pages/team/hero/team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -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}
/>
</label>
<div className="ml-3 inline-block border border-gray-1 px-4 text-center">
<button
className="border-0 p-0"
disabled={!teamName || currentTeamName == teamName}
type="button"
onClick={updateTeamName}
>
Update
</button>
</div>
</div>
<div className="mb-5">
<label>
Expand All @@ -200,7 +222,7 @@ const Team = ({ info }) => {
checked={randomJoin}
onClick={() => {
setRandomJoin(!randomJoin);
debounce(teamName, !randomJoin);
debounceRandomJoin(!randomJoin);
}}
/>
</label>
Expand Down
26 changes: 23 additions & 3 deletions src/pages/api/change-team.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
import findTeamByName from '~/helpers/find.team.by.name';
import findUserAndTeam from '~/helpers/find.user.and.team';
import prisma from '~/prisma/client';

export default async function handler(req, res) {
if (!req.body.name || req.body.name.length < 2 || !req.body.hasOwnProperty('allowAutoAssign')) {
res.status(400).send('Invalid parameters');
res.status(400).json({
success: false,
error: 'Invalid parameters',
});
return;
}

const teams = await findTeamByName(req.body.name);
if (teams.length) {
res.status(400).json({
success: false,
error: 'Team name already used!',
});
return;
}

const { admin, team } = await findUserAndTeam(req, res);
if (!admin && !team) {
res.status(400).send('Invalid call');
res.status(400).json({
success: false,
error: 'Invalid call',
});
return;
}

Expand All @@ -22,5 +39,8 @@ export default async function handler(req, res) {
},
});

res.status(200).send('changed');
res.status(200).json({
success: true,
message: 'Changed',
});
}