Skip to content

Commit

Permalink
Merge pull request CS3219-AY2324S1#255 from CS3219-AY2324S1/dev-lhy
Browse files Browse the repository at this point in the history
Bugfix: resolve table page length bug
  • Loading branch information
JrmCkh authored Nov 12, 2023
2 parents ed95c7e + f34773d commit 637f646
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 55 deletions.
24 changes: 1 addition & 23 deletions frontend/src/components/Question/QuestionContent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import DOMPurify from 'dompurify';
import { renderTags, getComplexityColor } from'./index';
import '../../css/QuestionContent.css';

const QuestionContent = ({ question }) => {
Expand All @@ -8,29 +9,6 @@ const QuestionContent = ({ question }) => {
return DOMPurify.sanitize(html);
};

const renderTags = (tags) => {
return tags?.map((tag, index) => {
return (
<span key={index} className='badge bg-secondary'>
{tag}
</span>
);
});
};

const getComplexityColor = (complexity) => {
switch (complexity) {
case 'Easy':
return 'bg-success';
case 'Medium':
return 'bg-warning';
case 'Hard':
return 'bg-danger';
default:
return 'bg-primary';
}
};

return (
<div className='question-content-container'>
<div className='card-body'>
Expand Down
34 changes: 6 additions & 28 deletions frontend/src/components/Question/QuestionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import $ from 'jquery';

import React, { useState, useEffect, useRef } from 'react';
import { useNavigate } from 'react-router-dom';
import { renderTags, getComplexityColor } from './index';
import Spinner from '../Spinner';
import { getQuestions } from '../../api/QuestionApi';
import { errorHandler } from '../../utils/errors';
Expand All @@ -21,8 +22,8 @@ const QuestionList = () => {
const tbConsts = Tables.Questions;

// The '-pre' postfix is needed, do not change
$.fn.dataTable.ext.type.order[ tbConsts.CustomSort.ColumnName + '-pre']
= function ( a ) {
$.fn.dataTable.ext.type.order[tbConsts.CustomSort.ColumnName + '-pre']
= function (a) {
return $.inArray(a, tbConsts.CustomSort.SortOrder);
};

Expand Down Expand Up @@ -52,7 +53,7 @@ const QuestionList = () => {

// Initialize DataTables
const pageLengthPref =
sessionStorage.getItem('question-table-page-length') ||
parseInt(sessionStorage.getItem('question-table-page-length')) ||
tbConsts.DEFAULT_PAGE_LENGTH;
dataTableRef.current = $(tableRef.current).DataTable({
pageLength: pageLengthPref,
Expand All @@ -63,7 +64,7 @@ const QuestionList = () => {
});

// Attach listener: on table pageLength change
$(tableRef.current).on('length.dt', function ( e, settings, len ) {
$(tableRef.current).on('length.dt', function (e, settings, len) {
sessionStorage.setItem('question-table-page-length', len);
});
}
Expand All @@ -77,36 +78,13 @@ const QuestionList = () => {
navigate('/question/new');
};

const RenderTags = (tags) => {
return tags?.map((tag, index) => {
return (
<span key={index} className='badge bg-secondary'>
{tag}
</span>
);
});
};

const getComplexityColor = (complexity) => {
switch (complexity) {
case 'Easy':
return 'bg-success';
case 'Medium':
return 'bg-warning';
case 'Hard':
return 'bg-danger';
default:
return 'bg-primary';
}
};

const questionList = tableData.map((question, index) => (
<tr key={question._id} onClick={() => handleRowClick(question._id)}>
<th scope='row'>{index + 1}</th>
<td style={{ maxWidth: '300px', wordWrap: 'break-word' }}>
{question.title}
</td>
<td>{RenderTags(question.tags)}</td>
<td>{renderTags(question.tags)}</td>
<td>
<span className={`badge ${getComplexityColor(question?.complexity)}`}>
{question.complexity}
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/components/Question/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
export { default as QuestionList } from './QuestionList';
export { default as QuestionContent } from './QuestionContent';
export { default as QuestionForm } from './QuestionForm';

export function renderTags(tags) {
return tags?.map((tag, index) => {
return (
<span key={index} className='badge bg-secondary'>
{tag}
</span>
);
});
};

export function getComplexityColor(complexity) {
switch (complexity) {
case 'Easy': return 'bg-success';
case 'Medium': return 'bg-warning';
case 'Hard': return 'bg-danger';
default: return 'bg-primary';
}
};
4 changes: 2 additions & 2 deletions frontend/src/components/SubmissionHistory/SubmissionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ const SubmissionList = () => {

// Initialize DataTables
const pageLengthPref =
sessionStorage.getItem('submission-table-page-length') ||
parseInt(sessionStorage.getItem('submission-table-page-length')) ||
Tables.Submissions.DEFAULT_PAGE_LENGTH;
dataTableRef.current = $(tableRef.current).DataTable({
pageLength: pageLengthPref
});

// Attach listener: on table pageLength change
$(tableRef.current).on('length.dt', function ( e, settings, len ) {
$(tableRef.current).on('length.dt', function (e, settings, len) {
sessionStorage.setItem('submission-table-page-length', len);
});
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/User/UserList.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ const UserList = () => {

// Initialize DataTables
const pageLengthPref =
sessionStorage.getItem('user-table-page-length') ||
parseInt(sessionStorage.getItem('user-table-page-length')) ||
Tables.Users.DEFAULT_PAGE_LENGTH;
dataTableRef.current = $(tableRef.current).DataTable({
pageLength: pageLengthPref
});

// Attach listener: on table pageLength change
$(tableRef.current).on('length.dt', function ( e, settings, len ) {
$(tableRef.current).on('length.dt', function (e, settings, len) {
sessionStorage.setItem('user-table-page-length', len);
});
}
Expand Down

0 comments on commit 637f646

Please sign in to comment.