Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor changes to UI and logic on canvas #150

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion frontend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ XATA_DATABASE_URL=
# Environment Configuration
NEXT_PUBLIC_PAYMENT_ADDRESS=
NEXT_PUBLIC_APP_ENV=
NODE_ENV=
NODE_ENV=

# Enable development tools (Eruda)
NEXT_PUBLIC_ENABLE_DEVTOOLS=
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@worldcoin/minikit-js": "1.5.0",
"@xata.io/client": "0.0.0-next.va121e4207b94bfe0a3c025fc00b247b923880930",
"dotenv": "^16.4.5",
"eruda": "^3.2.3",
"framer-motion": "^12.0.5",
"jose": "^5.9.6",
"jotai": "^2.11.1",
Expand All @@ -46,6 +45,7 @@
"autoprefixer": "^10.4.20",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"eruda": "^3.2.3",
"eslint": "^9.17.0",
"eslint-config-next": "15.1.3",
"postcss": "^8.4.49",
Expand Down
6 changes: 3 additions & 3 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 39 additions & 3 deletions frontend/src/app/ideology-test/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function IdeologyTest() {
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [isSubmitting, setIsSubmitting] = useState(false);
const [originalAnswers, setOriginalAnswers] = useState<Record<string, number>>({});

const totalQuestions = questions.length;
const progress = ((currentQuestion + 1) / totalQuestions) * 100;
Expand All @@ -49,6 +50,13 @@ export default function IdeologyTest() {
const response = await fetch(`/api/tests/${testId}/progress`);
if (response.ok) {
const data = await response.json();

// Check if test is already completed
if (data.status === "completed") {
router.push(`/insights?testId=${testId}`);
return;
}

if (data.answers && Object.keys(data.answers).length > 0) {
const lastAnsweredId = Object.keys(data.answers).pop();
const lastAnsweredIndex = loadedQuestions.findIndex(
Expand All @@ -61,6 +69,7 @@ export default function IdeologyTest() {
setCurrentQuestion(nextQuestionIndex);
setScores(data.scores || { econ: 0, dipl: 0, govt: 0, scty: 0 });
setUserAnswers(data.answers);
setOriginalAnswers(data.answers);
}
}
} catch (error) {
Expand Down Expand Up @@ -89,9 +98,8 @@ export default function IdeologyTest() {
}, [testId]);

const handleEndTest = async () => {
if (isSubmitting) return; // Prevent multiple submissions
if (isSubmitting) return;

// Check if all questions have been answered
const unansweredQuestions = Object.keys(userAnswers).length;
if (unansweredQuestions < questions.length) {
setError(
Expand All @@ -105,6 +113,7 @@ export default function IdeologyTest() {
setIsSubmitting(true);

try {
// Calculate scores first as we'll need them in both cases
const maxEcon = questions.reduce(
(sum, q) => sum + Math.abs(q.effect.econ),
0,
Expand Down Expand Up @@ -134,6 +143,17 @@ export default function IdeologyTest() {
scty: Math.round(sctyScore),
};

// Check if insights exist
const insightsResponse = await fetch(`/api/insights/${testId}`);
const hasExistingInsights = insightsResponse.ok &&
(await insightsResponse.json()).insights?.length > 0;

// Check if answers have changed
const hasAnswersChanged = Object.keys(originalAnswers).some(
key => originalAnswers[key] !== userAnswers[key]
);

// Save progress and update scores
const response = await fetch(`/api/tests/${testId}/progress`, {
method: "POST",
headers: {
Expand All @@ -151,7 +171,23 @@ export default function IdeologyTest() {
throw new Error("Failed to save final answers");
}

const resultsResponse = await fetch(`/api/tests/${testId}/results`);
// Handle the three scenarios:
// 1. If insights exist and no changes - just redirect
if (hasExistingInsights && !hasAnswersChanged) {
router.push(`/insights?testId=${testId}`);
return;
}

// 2. If no insights exist - create new ones
// 3. If answers changed - rewrite existing insights
const resultsResponse = await fetch(`/api/tests/${testId}/results`, {
method: hasExistingInsights ? "PUT" : "POST", // Use PUT to update existing insights
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ forceUpdate: hasAnswersChanged }),
});

if (!resultsResponse.ok) {
throw new Error("Failed to save final results");
}
Expand Down
Loading
Loading