handleSort("questionId")} + className={`${styles.clickable} ${sortKey === "questionId" ? styles[sortOrder] : ""}`} + > + ID + | +handleSort("title")} + className={`${styles.clickable} ${sortKey === "title" ? styles[sortOrder] : ""}`} + > + Title + | +Categories | +handleSort("complexity")} + className={`${styles.clickable} ${sortKey === "complexity" ? styles[sortOrder] : ""}`} + > + Complexity + | +Actions | +
---|---|---|---|---|
{question.questionId} | +
+
+
+ |
+
+
+ {question.categories.map((tag, index) => (
+
+ {tag}
+
+ ))}
+
+ |
+ {question.complexity} | +
+ |
+
Welcome to lucKY JiNX!
-Loading questions...
; + // } + + // if (error) { + // return{error}
; + // } + + // Function to truncate long descriptions + const truncateDescription = (description: string, maxLength: number) => { + if (description.length > maxLength) { + return description.substring(0, maxLength) + "..."; + } + return description; + }; + + const handleSort = (key: keyof Question) => { + const order = sortKey === key && sortOrder === "asc" ? "desc" : "asc"; + setSortKey(key); + setSortOrder(order); + + const sortedQuestions = [...questions].sort((a, b) => { + if (key === "complexity") { + const aValue = getComplexityValue(a.complexity); + const bValue = getComplexityValue(b.complexity); + return order === "asc" ? aValue - bValue : bValue - aValue; + } + + if (a[key] < b[key]) { + return order === "asc" ? -1 : 1; + } + if (a[key] > b[key]) { + return order === "asc" ? 1 : -1; + } + return 0; + }); + + setQuestions(sortedQuestions); + }; + + return ( +handleSort("id")} + className={`${styles.clickable} ${sortKey === "id" ? styles[sortOrder] : ""}`} + > + ID + | +handleSort("title")} + className={`${styles.clickable} ${sortKey === "title" ? styles[sortOrder] : ""}`} + > + Title + | +Description | +Category | +handleSort("complexity")} + className={`${styles.clickable} ${sortKey === "complexity" ? styles[sortOrder] : ""}`} + > + Complexity + | +
---|---|---|---|---|
{question.id} | +{question.title} | +{truncateDescription(question.description, 100)} | +
+
+ {question.category.map((tag, index) => (
+
+ {tag}
+
+ ))}
+
+ |
+ {question.complexity} | +