Improved a bit of error handling

This commit is contained in:
Tiago Ribeiro
2023-05-27 16:29:12 +01:00
parent b5dabe336a
commit 2a86efc97e
2 changed files with 21 additions and 11 deletions

View File

@@ -1,4 +1,5 @@
import {infoButtonStyle} from "@/constants/buttonStyles"; import {infoButtonStyle} from "@/constants/buttonStyles";
import {BAND_SCORES} from "@/constants/ielts";
import {Module} from "@/interfaces"; import {Module} from "@/interfaces";
import {User} from "@/interfaces/user"; import {User} from "@/interfaces/user";
import useExamStore from "@/stores/examStore"; import useExamStore from "@/stores/examStore";
@@ -7,8 +8,8 @@ import axios from "axios";
import clsx from "clsx"; import clsx from "clsx";
import {capitalize} from "lodash"; import {capitalize} from "lodash";
import {useRouter} from "next/router"; import {useRouter} from "next/router";
import {useEffect, useState} from "react"; import {useState} from "react";
import {ToastContainer, toast} from "react-toastify"; import {toast} from "react-toastify";
interface Props { interface Props {
user: User; user: User;
@@ -88,22 +89,23 @@ export default function Diagnostic({onFinish}: Props) {
<span className="font-medium text-lg">{capitalize(module)}</span> <span className="font-medium text-lg">{capitalize(module)}</span>
<input <input
type="number" type="number"
className="input input-bordered bg-white w-24" className={clsx(
"input input-bordered bg-white w-24",
!BAND_SCORES[module as Module].includes(levels[module as keyof typeof levels]) && "input-error",
)}
value={levels[module as keyof typeof levels]} value={levels[module as keyof typeof levels]}
min={0} min={0}
max={9} max={9}
onChange={(e) => step={0.5}
setLevels((prev) => onChange={(e) => setLevels((prev) => ({...prev, [module]: parseFloat(e.target.value)}))}
parseInt(e.target.value) <= 9 && parseInt(e.target.value) >= 0
? {...prev, [module]: parseInt(e.target.value)}
: prev,
)
}
/> />
</div> </div>
))} ))}
</div> </div>
<button onClick={updateUser} className={clsx("btn btn-wide gap-4 relative text-white", infoButtonStyle)}> <button
onClick={updateUser}
className={clsx("btn btn-wide gap-4 relative text-white", infoButtonStyle)}
disabled={!Object.keys(levels).every((module) => BAND_SCORES[module as Module].includes(levels[module as keyof typeof levels]))}>
Next Next
</button> </button>
</div> </div>

8
src/constants/ielts.ts Normal file
View File

@@ -0,0 +1,8 @@
import {Module} from "@/interfaces";
export const BAND_SCORES: {[key in Module]: number[]} = {
reading: [0, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9],
listening: [0, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9],
writing: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
speaking: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
};