Skip to content
Snippets Groups Projects
Commit d2c1e3ed authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Add a status message

parent f4d152b0
No related branches found
No related tags found
1 merge request!25Add a question management page
import { Dispatch, FC, FormEvent, SetStateAction } from 'react'; import { Dispatch, FC, FormEvent, SetStateAction } from 'react';
import { StatusMessage } from '@/types/statusMessage';
type NewQuestionFormProps = { type NewQuestionFormProps = {
newQuestion: string; newQuestion: string;
setNewQuestion: Dispatch<SetStateAction<string>>; setNewQuestion: Dispatch<SetStateAction<string>>;
onSubmit: () => Promise<void>; onSubmit: () => Promise<void>;
statusMessage: StatusMessage | undefined;
}; };
const NewQuestionForm: FC<NewQuestionFormProps> = ({ newQuestion, setNewQuestion, onSubmit }) => { const NewQuestionForm: FC<NewQuestionFormProps> = ({
newQuestion,
setNewQuestion,
onSubmit,
statusMessage
}) => {
// Set the colour of the status message
const statusTextColour = statusMessage
? statusMessage.error
? 'text-red-500'
: 'text-green-500'
: '';
const onFormSubmit = (e: FormEvent<HTMLFormElement>): void => { const onFormSubmit = (e: FormEvent<HTMLFormElement>): void => {
e.preventDefault(); e.preventDefault();
onSubmit(); onSubmit();
...@@ -34,6 +48,13 @@ const NewQuestionForm: FC<NewQuestionFormProps> = ({ newQuestion, setNewQuestion ...@@ -34,6 +48,13 @@ const NewQuestionForm: FC<NewQuestionFormProps> = ({ newQuestion, setNewQuestion
onChange={(e) => setNewQuestion(e.target.value)} onChange={(e) => setNewQuestion(e.target.value)}
/> />
{/* Status Message */}
{statusMessage && (
<p className={'mt-1 text-sm leading-6 text-center ' + statusTextColour}>
{statusMessage.message}
</p>
)}
{/* Submit Button */} {/* Submit Button */}
<button <button
type="submit" type="submit"
......
...@@ -4,11 +4,13 @@ import { useUser } from '@/hooks/useUser'; ...@@ -4,11 +4,13 @@ import { useUser } from '@/hooks/useUser';
import { useState } from 'react'; import { useState } from 'react';
import NewQuestionForm from '@/components/questions/NewQuestionForm'; import NewQuestionForm from '@/components/questions/NewQuestionForm';
import QuestionList from '@/components/questions/QuestionList'; import QuestionList from '@/components/questions/QuestionList';
import { StatusMessage } from '@/types/statusMessage';
const Questions = () => { const Questions = () => {
const { user } = useUser(); const { user } = useUser();
const { questions, setRehydrateQuestions } = useQuestions(); const { questions, setRehydrateQuestions } = useQuestions();
const [newQuestion, setNewQuestion] = useState(''); const [newQuestion, setNewQuestion] = useState('');
const [statusMessage, setStatusMessage] = useState<StatusMessage | undefined>(undefined);
const insertNewQuestion = async (newQuestion: String): Promise<void> => { const insertNewQuestion = async (newQuestion: String): Promise<void> => {
const JSONdata = JSON.stringify({ questionText: newQuestion }); const JSONdata = JSON.stringify({ questionText: newQuestion });
...@@ -21,7 +23,27 @@ const Questions = () => { ...@@ -21,7 +23,27 @@ const Questions = () => {
}, },
body: JSONdata body: JSONdata
}; };
await fetch(endpoint, options);
var response = await fetch(endpoint, options);
var message = await response.text();
switch (response.status) {
case 201:
setStatusMessage({
message: 'Question was successfully inserted!',
error: false
} as StatusMessage);
break;
case 400:
setStatusMessage({ message: message, error: true } as StatusMessage);
break;
default:
setStatusMessage({
message: 'Unexpected error, please try again.',
error: true
} as StatusMessage);
break;
}
}; };
const onQuestionSubmit = async (): Promise<void> => { const onQuestionSubmit = async (): Promise<void> => {
...@@ -41,6 +63,7 @@ const Questions = () => { ...@@ -41,6 +63,7 @@ const Questions = () => {
newQuestion={newQuestion} newQuestion={newQuestion}
setNewQuestion={setNewQuestion} setNewQuestion={setNewQuestion}
onSubmit={onQuestionSubmit} onSubmit={onQuestionSubmit}
statusMessage={statusMessage}
/> />
{/* List of Questions */} {/* List of Questions */}
......
export type StatusMessage = {
message: string;
error: boolean;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment