Skip to content

Commit

Permalink
Can add judges without sending emails (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelZhao21 authored Mar 23, 2024
1 parent a6993e1 commit 2e780c9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
15 changes: 12 additions & 3 deletions client/src/components/admin/add-judges/NewJudgeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@ import TextInput from '../../TextInput';
import TextArea from '../../TextArea';
import { postRequest } from '../../../api';
import { errorAlert } from '../../../util';
import Checkbox from '../../Checkbox';

interface NewJudgeData {
name: string;
email: string;
notes: string;
}

type NewJudgeDataFull = NewJudgeData & { no_send: boolean };

const NewJudgeForm = () => {
const [isSubmitting, setIsSubmitting] = useState(false);
const { register, handleSubmit, reset } = useForm<NewJudgeData>();
const [noSend, setNoSend] = useState(false);

const onSubmit: SubmitHandler<NewJudgeData> = async (data) => {
setIsSubmitting(true);

const res = await postRequest('/judge/new', 'admin', data);

const newdata = { ...data, no_send: noSend }

const res = await postRequest('/judge/new', 'admin', newdata);
if (res.status !== 200) {
errorAlert(res);
return;
}

alert('Judge added successfully!');
reset();
setIsSubmitting(false);
Expand All @@ -39,6 +45,9 @@ const NewJudgeForm = () => {
<TextInput name="email" placeholder="Email" register={register} required />
</div>
<TextArea name="notes" placeholder="Notes (optional)" register={register} />
<Checkbox checked={noSend} onChange={setNoSend}>
Do not send an email
</Checkbox>
<button
className="w-full h-11 px-4 text-2xl text-white bg-primary rounded-full"
disabled={isSubmitting}
Expand Down
7 changes: 4 additions & 3 deletions server/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type IdRequest struct {
}

type AddJudgeRequest struct {
Name string `json:"name"`
Email string `json:"email"`
Notes string `json:"notes"`
Name string `json:"name"`
Email string `json:"email"`
Notes string `json:"notes"`
NoSend bool `json:"no_send"`
}
30 changes: 18 additions & 12 deletions server/router/judge.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package router

import (
"fmt"
"net/http"
"server/crowdbt"
"server/database"
Expand Down Expand Up @@ -48,20 +49,25 @@ func AddJudge(ctx *gin.Context) {
// Create the judge
judge := models.NewJudge(judgeReq.Name, judgeReq.Email, judgeReq.Notes)

// Get hostname from request
hostname := util.GetFullHostname(ctx)
fmt.Println(judgeReq.NoSend)

// Make sure email is right
if !funcs.CheckEmail(judge.Email) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid email"})
return
}
// Send email if no_send is false
if !judgeReq.NoSend {
// Get hostname from request
hostname := util.GetFullHostname(ctx)

// Send email to judge
err = funcs.SendJudgeEmail(judge, hostname)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error sending judge email: " + err.Error()})
return
// Make sure email is right
if !funcs.CheckEmail(judge.Email) {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid email"})
return
}

// Send email to judge
err = funcs.SendJudgeEmail(judge, hostname)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error sending judge email: " + err.Error()})
return
}
}

// Insert the judge into the database
Expand Down

0 comments on commit 2e780c9

Please sign in to comment.