Skip to content

Commit da1dad7

Browse files
committed
Add delete button
Button has no functionality for now
1 parent 4fc34f2 commit da1dad7

File tree

3 files changed

+167
-96
lines changed

3 files changed

+167
-96
lines changed

peerprep/api/gateway.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,83 @@
11
import { Question, StatusBody, QuestionFullBody } from "./structs";
22

33
const questions: { [key: string]: Question } = {
4-
"0" : {
5-
"id": 0,
6-
"difficulty": 2,
7-
"title": "Two Sum",
8-
"description": "Given an array of integers, return indices of the two numbers such that they add up to a specific target.",
9-
"test_cases": {
10-
"[2, 7, 11, 15], 9" : "[0, 1]",
11-
"[3, 2, 4], 6" : "[1, 2]",
12-
"[3, 3], 6" : "[0, 1]"
13-
}
4+
"0": {
5+
id: 0,
6+
difficulty: 2,
7+
title: "Two Sum",
8+
description:
9+
"Given an array of integers, return indices of the two numbers such that they add up to a specific target.",
10+
categories: ["Hash Table", "Array"],
11+
test_cases: {
12+
"[2, 7, 11, 15], 9": "[0, 1]",
13+
"[3, 2, 4], 6": "[1, 2]",
14+
"[3, 3], 6": "[0, 1]",
15+
},
16+
},
17+
"1": {
18+
id: 1,
19+
difficulty: 1,
20+
title: "Reverse Integer",
21+
description: "Given a 32-bit signed integer, reverse digits of an integer.",
22+
categories: ["Math"],
23+
test_cases: {
24+
"123": "321",
25+
"1": "1",
26+
"22": "22",
27+
},
1428
},
15-
"1" : {
16-
"id": 1,
17-
"difficulty": 1,
18-
"title": "Reverse Integer",
19-
"description": "Given a 32-bit signed integer, reverse digits of an integer.",
20-
"test_cases": {
21-
"123" : "321",
22-
"1" : "1",
23-
"22" : "22"
24-
}
25-
}
2629
};
2730

28-
export async function fetchQuestion(questionId: string): Promise<Question|StatusBody> {
31+
export async function fetchQuestion(
32+
questionId: string
33+
): Promise<Question | StatusBody> {
2934
// remove this when services are up
3035
if (process.env.DEV_ENV === "dev") {
3136
return questions[questionId] === undefined
32-
? {error: "Question not found", status: 404}
33-
: questions[questionId];
37+
? { error: "Question not found", status: 404 }
38+
: questions[questionId];
3439
}
3540
try {
36-
const response = await fetch(`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`);
41+
const response = await fetch(
42+
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions/solve/${questionId}`
43+
);
3744
if (!response.ok) {
3845
return {
3946
...(await response.json()),
40-
status: response.status
47+
status: response.status,
4148
};
4249
}
43-
return await response.json() as Question;
50+
return (await response.json()) as Question;
4451
} catch (err: any) {
45-
return { error: err.message, status: 0};
52+
return { error: err.message, status: 0 };
4653
}
4754
}
4855

4956
export async function addQuestion(body: QuestionFullBody): Promise<StatusBody> {
5057
try {
5158
const response = await fetch(
52-
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
53-
{
54-
method: "POST",
55-
body: JSON.stringify(body).replace(/(\"difficulty\":)\"([1-3])\"/, `$1$2`),
56-
headers: {
57-
"Content-type": "application/json; charset=UTF-8"
58-
}
59-
}
59+
`${process.env.NEXT_PUBLIC_QUESTION_SERVICE}/questions`,
60+
{
61+
method: "POST",
62+
body: JSON.stringify(body).replace(
63+
/(\"difficulty\":)\"([1-3])\"/,
64+
`$1$2`
65+
),
66+
headers: {
67+
"Content-type": "application/json; charset=UTF-8",
68+
},
69+
}
6070
);
6171
if (response.ok) {
6272
return {
63-
status: response.status
73+
status: response.status,
6474
};
6575
}
6676
return {
6777
error: (await response.json())["Error adding question: "],
68-
status: response.status
78+
status: response.status,
6979
};
7080
} catch (err: any) {
71-
return { error: err.message, status: 0};
81+
return { error: err.message, status: 0 };
7282
}
7383
}
Lines changed: 98 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,166 @@
1-
'use client';
2-
import { useState, ChangeEvent, MouseEvent, FormEvent } from 'react';
3-
import { QuestionBody, Difficulty, QuestionFullBody } from '@/api/structs';
4-
import { addQuestion } from '@/api/gateway';
1+
"use client";
2+
import { useState, ChangeEvent, MouseEvent, FormEvent } from "react";
3+
import { QuestionBody, Difficulty, QuestionFullBody } from "@/api/structs";
4+
import { addQuestion } from "@/api/gateway";
55

6-
type Props = {}
6+
type Props = {};
77

88
interface Mapping {
9-
key: string,
10-
value: string
9+
key: string;
10+
value: string;
1111
}
1212

1313
function NewQuestion({}: Props) {
14-
const [testCases, setTestCases] = useState<Mapping[]>([{
15-
key: "", value: ""
16-
}]);
14+
const [testCases, setTestCases] = useState<Mapping[]>([
15+
{
16+
key: "",
17+
value: "",
18+
},
19+
]);
1720
const [formData, setFormData] = useState<QuestionBody>({
1821
title: "",
1922
difficulty: Difficulty.Easy,
2023
description: "",
24+
categories: [],
2125
});
2226

23-
const handleTextInput = (e: ChangeEvent<HTMLInputElement|HTMLTextAreaElement>) => setFormData({
24-
...formData,
25-
[e.target.name]: e.target.value
26-
});
27+
const handleTextInput = (
28+
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
29+
) =>
30+
setFormData({
31+
...formData,
32+
[e.target.name]: e.target.value,
33+
});
2734

28-
const handleTestCaseInput = (e: ChangeEvent<HTMLInputElement>, idx: number) => {
35+
const handleTestCaseInput = (
36+
e: ChangeEvent<HTMLInputElement>,
37+
idx: number
38+
) => {
2939
const values = [...testCases];
3040
values[idx] = {
3141
...values[idx],
32-
[e.target.name]: e.target.value
42+
[e.target.name]: e.target.value,
3343
};
34-
setTestCases(values);
35-
}
44+
setTestCases(values);
45+
};
3646

3747
const handleAddField = (e: MouseEvent<HTMLElement>) =>
38-
setTestCases([...testCases, { key: "", value: ""}]);
48+
setTestCases([...testCases, { key: "", value: "" }]);
3949

4050
const handleDeleteField = (e: MouseEvent<HTMLElement>, idx: number) => {
4151
const values = [...testCases];
4252
values.splice(idx, 1);
4353
setTestCases(values);
44-
}
45-
54+
};
55+
4656
const handleSubmission = async (e: FormEvent<HTMLFormElement>) => {
4757
e.preventDefault();
4858
const question: QuestionFullBody = {
4959
...formData,
50-
test_cases: testCases.map((elem: Mapping) => ({
51-
[elem.key]: elem.value
52-
})).reduce((res, item) => ({...res, ...item}), {})
53-
}
60+
test_cases: testCases
61+
.map((elem: Mapping) => ({
62+
[elem.key]: elem.value,
63+
}))
64+
.reduce((res, item) => ({ ...res, ...item }), {}),
65+
};
5466
const status = await addQuestion(question);
5567
if (status.error) {
5668
console.log("Failed to add question.");
5769
console.log(`Code ${status.status}: ${status.error}`);
5870
return;
5971
}
6072
console.log(`Successfully added the question.`);
61-
}
73+
};
6274

6375
return (
6476
<div>
65-
<form style={{color: "black", padding: "5px"}} onSubmit={handleSubmission}>
66-
<input type="text" name="title" value={formData.title} onChange={handleTextInput}/><br/>
67-
<input type="radio" id="easy" name="difficulty" value={1} onChange={handleTextInput} />
68-
<label htmlFor="easy">Easy</label><br/>
69-
<input type="radio" id="med" name="difficulty" value={2} onChange={handleTextInput} />
70-
<label htmlFor="med">Medium</label><br/>
71-
<input type="radio" id="hard" name="difficulty" value={3} onChange={handleTextInput} />
72-
<label htmlFor="hard">Hard</label><br/>
73-
<textarea name="description" value={formData.description} onChange={handleTextInput}/><br/>
77+
<form
78+
style={{ color: "black", padding: "5px" }}
79+
onSubmit={handleSubmission}
80+
>
81+
<input
82+
type="text"
83+
name="title"
84+
value={formData.title}
85+
onChange={handleTextInput}
86+
/>
87+
<br />
88+
<input
89+
type="radio"
90+
id="easy"
91+
name="difficulty"
92+
value={1}
93+
onChange={handleTextInput}
94+
/>
95+
<label htmlFor="easy">Easy</label>
96+
<br />
97+
<input
98+
type="radio"
99+
id="med"
100+
name="difficulty"
101+
value={2}
102+
onChange={handleTextInput}
103+
/>
104+
<label htmlFor="med">Medium</label>
105+
<br />
106+
<input
107+
type="radio"
108+
id="hard"
109+
name="difficulty"
110+
value={3}
111+
onChange={handleTextInput}
112+
/>
113+
<label htmlFor="hard">Hard</label>
114+
<br />
115+
<textarea
116+
name="description"
117+
value={formData.description}
118+
onChange={handleTextInput}
119+
/>
120+
<br />
74121
{testCases.map((elem, idx) => (
75122
<>
76123
<input
77124
name="key"
78125
type="text"
79126
id={`key_${idx.toLocaleString()}`}
80127
value={elem.key}
81-
onChange={e => handleTestCaseInput(e, idx)} />
128+
onChange={(e) => handleTestCaseInput(e, idx)}
129+
/>
82130
<input
83131
name="value"
84132
type="text"
85133
id={`val_${idx.toLocaleString()}`}
86134
value={elem.value}
87-
onChange={e => handleTestCaseInput(e, idx)} />
135+
onChange={(e) => handleTestCaseInput(e, idx)}
136+
/>
88137
<input
89138
type="button"
90139
name="del_entry"
91140
value="Delete..."
92-
onClick={e => handleDeleteField(e, idx)}
93-
style={{ backgroundColor: "white"}} />
94-
<br/>
141+
onClick={(e) => handleDeleteField(e, idx)}
142+
style={{ backgroundColor: "white" }}
143+
/>
144+
<br />
95145
</>
96146
))}
97147
<input
98148
type="button"
99149
name="add_entry"
100150
value="Add..."
101151
onClick={handleAddField}
102-
style={{ backgroundColor: "white"}} />
152+
style={{ backgroundColor: "white" }}
153+
/>
103154
<button
104155
type="submit"
105156
name="submit"
106-
style={{ backgroundColor: "white"}}>Submit</button>
157+
style={{ backgroundColor: "white" }}
158+
>
159+
Submit
160+
</button>
107161
</form>
108162
</div>
109-
)
163+
);
110164
}
111165

112-
export default NewQuestion;
166+
export default NewQuestion;

0 commit comments

Comments
 (0)