Skip to content
Snippets Groups Projects
Commit 4dbc8269 authored by Saket Agrahari's avatar Saket Agrahari
Browse files

COMP-213: progress bar

parent 6406643a
No related branches found
No related tags found
1 merge request!46COMP-213: progress bar
import React from "react";
function ProgressBar({
completionPercentage,
unansweredPercentage,
pages,
pageTitle,
}) {
const progressBarContainerStyle: React.CSSProperties = {
display: "flex",
flexWrap: "wrap",
height: "10px",
margin: "5px",
width: `${100 / pages}%`,
};
const progressBarFillStyle: React.CSSProperties = {
height: "100%",
transition: "width 0.3s ease",
};
const progressBarFillStyleCopy: React.CSSProperties = {
...progressBarFillStyle,
width: `${completionPercentage}%`,
backgroundColor: "#1ab394",
};
const unansweredProgressBarFillStyle: React.CSSProperties = {
...progressBarFillStyle,
width: `${unansweredPercentage}%`,
backgroundColor: "#9d9d9d",
};
const pageTitleStyle: React.CSSProperties ={
width: "100%",
textAlign: "center"
}
return (
<div style={progressBarContainerStyle}>
<div style={progressBarFillStyleCopy} />
<div style={unansweredProgressBarFillStyle} />
<div style={pageTitleStyle}>{pageTitle}</div>
</div>
);
}
export default ProgressBar;
...@@ -3,11 +3,17 @@ import { Model, Serializer, ComputedUpdater, Question } from "survey-core"; ...@@ -3,11 +3,17 @@ import { Model, Serializer, ComputedUpdater, Question } from "survey-core";
import { Survey } from "survey-react-ui"; import { Survey } from "survey-react-ui";
import "survey-core/modern.min.css"; import "survey-core/modern.min.css";
import './survey.scss'; import './survey.scss';
import ProgressBar from "./ProgressBar";
Serializer.addProperty("itemvalue", "customDescription:text"); Serializer.addProperty("itemvalue", "customDescription:text");
Serializer.addProperty("question", "hideCheckboxLabels:boolean"); Serializer.addProperty("question", "hideCheckboxLabels:boolean");
interface Progress {
completionPercentage: number;
unansweredPercentage: number;
totalPages: number;
pageTitle: string;
}
enum VerificationStatus { enum VerificationStatus {
New = "new", // a question that was not answered last year New = "new", // a question that was not answered last year
Answered = "answered", // a question that was not answered last year but has an answer now Answered = "answered", // a question that was not answered last year but has an answer now
...@@ -18,8 +24,8 @@ enum VerificationStatus { ...@@ -18,8 +24,8 @@ enum VerificationStatus {
function SurveyComponent() { function SurveyComponent() {
const [surveyModel, setSurveyModel] = useState<Model>(); const [surveyModel, setSurveyModel] = useState<Model>();
const [progress, setProgress] = useState<Progress[]>([]);
const verificationStatus = useRef<Map<string, VerificationStatus>>(new Map()); const verificationStatus = useRef<Map<string, VerificationStatus>>(new Map());
function setVerifyButton(question: Question, state: VerificationStatus) { function setVerifyButton(question: Question, state: VerificationStatus) {
...@@ -67,8 +73,7 @@ function SurveyComponent() { ...@@ -67,8 +73,7 @@ function SurveyComponent() {
const survey = new Model(json['model']); const survey = new Model(json['model']);
if (json['data'] !== null) if (json['data'] !== null) {
{
survey.data = json['data']; survey.data = json['data'];
} }
...@@ -104,8 +109,7 @@ function SurveyComponent() { ...@@ -104,8 +109,7 @@ function SurveyComponent() {
}); });
survey.onComplete.add((sender, options) => { survey.onComplete.add((sender, options) => {
console.log(sender.data);
console.log(sender.data)
options.showSaveInProgress(); options.showSaveInProgress();
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
...@@ -134,7 +138,6 @@ function SurveyComponent() { ...@@ -134,7 +138,6 @@ function SurveyComponent() {
console.log(sender.data) console.log(sender.data)
// TODO same as above // TODO same as above
}); });
survey.onAfterRenderQuestion.add(function(survey, options){ survey.onAfterRenderQuestion.add(function(survey, options){
...@@ -169,29 +172,81 @@ function SurveyComponent() { ...@@ -169,29 +172,81 @@ function SurveyComponent() {
if (options.column['indexValue'] == 0 && 'item' in options.row) { if (options.column['indexValue'] == 0 && 'item' in options.row) {
const item = options.row['item'] as object; const item = options.row['item'] as object;
if (item['customDescription'] !== undefined) { if (item['customDescription'] !== undefined) {
options.htmlElement.parentElement?.children[0].setAttribute("title", item['customDescription']); options.htmlElement.parentElement?.children[0].setAttribute(
"title",
item['customDescription']
);
} }
} }
}); });
survey.onCurrentPageChanged.add((sender) => {
console.log("sender--> " + sender);
calculateProgress(sender);
});
setSurveyModel(survey); setSurveyModel(survey);
}
const filterCallback = (question) => {
return question.value !== null && question.value !== undefined;
};
const calculateProgress = (survey) => {
// console.log("survey--> "+ survey);
if (survey && survey.pages) {
console.log("survey.page--> " + survey.pages);
const progressArray: Progress[] = [];
survey.pages.forEach((page) => {
const sectionQuestions = page.questions.filter(
(question) => question.startWithNewLine
);
const questionCount = sectionQuestions.length;
const answeredCount = sectionQuestions.filter(filterCallback).length;
const unansweredCount = questionCount - answeredCount;
const completionPercentage = answeredCount / questionCount;
progressArray.push({
completionPercentage: completionPercentage * 100,
unansweredPercentage: (unansweredCount / questionCount) * 100,
totalPages: survey.pages.length,
pageTitle: page.title,
});
});
setProgress(progressArray);
} }
};
useEffect(() => { useEffect(() => {
getModel(); getModel();
}, []); }, []);
useEffect(() => {
if (surveyModel) if (surveyModel) {
{ calculateProgress(surveyModel);
return (<Survey model={surveyModel} />); }
} }, [surveyModel]);
else
{ if (surveyModel) {
return (<span>loading...</span>); return (
<div className="survey-container">
<div className="survey-progress">
{progress.map((sectionProgress, index) => (
<ProgressBar
key={index}
completionPercentage={sectionProgress.completionPercentage}
unansweredPercentage={sectionProgress.unansweredPercentage}
pages={sectionProgress.totalPages}
pageTitle={sectionProgress.pageTitle}
/>
))}
</div>
<Survey model={surveyModel} />
</div>
);
} else {
return <span>loading...</span>;
} }
} }
export default SurveyComponent; export default SurveyComponent;
...@@ -25,3 +25,7 @@ ...@@ -25,3 +25,7 @@
cursor: auto; cursor: auto;
background-color: transparent; background-color: transparent;
} }
.survey-progress {
display: flex;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment