Skip to content
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
21 changes: 19 additions & 2 deletions client/src/components/AdminView.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useState } from "react";
import { useContext, useState, useEffect } from "react";

// components
import { ConfirmationModal, PageFooter } from "@/components";
Expand All @@ -7,7 +7,7 @@ import { ConfirmationModal, PageFooter } from "@/components";
import { backendAPI } from "@/utils/backendAPI";

// context
import { GlobalDispatchContext } from "@/context/GlobalContext";
import { GlobalDispatchContext, GlobalStateContext } from "@/context/GlobalContext";
import { SET_POLL } from "@/context/types";

interface PollFormInputs {
Expand Down Expand Up @@ -41,6 +41,7 @@ export const AdminView = () => {
});
const [isSubmitting, setIsSubmitting] = useState(false);
const [modalType, setModalType] = useState<"save" | "reset" | null>(null);
const { poll } = useContext(GlobalStateContext);

function handleToggleShowConfirmationModal() {
setShowConfirmationModal(!showConfirmationModal);
Expand Down Expand Up @@ -123,6 +124,22 @@ export const AdminView = () => {
setShowConfirmationModal(true);
};

useEffect(() => {
if (!poll) return;
// pull the question + the answers array
const { question, answers, displayMode } = poll;

setFormData({
question,
answer1: answers[0] || "",
answer2: answers[1] || "",
answer3: answers[2] || "",
answer4: answers[3] || "",
answer5: answers[4] || "",
displayMode: displayMode === "count" ? "count" : "percentage",
});
}, [poll]);

return (
<div className="grid grid-flow-row gap-4 pb-20">
<h3>Create or Update Poll</h3>
Expand Down
15 changes: 15 additions & 0 deletions server/controllers/handleGetPoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,26 @@ import { errorHandler, getCredentials, getDroppedAsset } from "../utils/index.js
export const handleGetPoll = async (req: Request, res: Response) => {
try {
const credentials = getCredentials(req.query);
const { profileId, urlSlug } = credentials;

const droppedAsset = await getDroppedAsset(credentials);

await droppedAsset.fetchDataObject();

await droppedAsset.updateDataObject(
{},
{
analytics: [
{
analyticName: "starts",
profileId,
urlSlug,
uniqueKey: profileId,
},
],
},
);

return res.json({ poll: droppedAsset.dataObject, success: true });
} catch (error) {
return errorHandler({
Expand Down
14 changes: 12 additions & 2 deletions server/controllers/handleUpdatePoll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { errorHandler, getCredentials, getDroppedAsset } from "../utils/index.js
export const handleUpdatePoll = async (req: Request, res: Response) => {
try {
const credentials = getCredentials(req.query);
const { assetId } = credentials;
const { assetId, profileId, urlSlug } = credentials;
const { question, answer1, answer2, answer3, answer4, answer5, displayMode } = req.body;

const droppedAsset = await getDroppedAsset(credentials);
Expand All @@ -28,7 +28,17 @@ export const handleUpdatePoll = async (req: Request, res: Response) => {
},
results: {},
},
{ lock: { lockId, releaseLock: true } },
{
lock: { lockId, releaseLock: true },
analytics: [
{
analyticName: "updates",
profileId,
urlSlug,
uniqueKey: profileId,
},
],
},
);

await droppedAsset.fetchDataObject();
Expand Down
14 changes: 12 additions & 2 deletions server/controllers/handleVote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const handleVote = async (req: Request, res: Response) => {
try {
// Extract credentials and vote details
const credentials = getCredentials(req.query);
const { assetId } = credentials;
const { assetId, urlSlug } = credentials;
const { optionId, profileId } = req.body;
if (optionId === undefined || !profileId) {
return res.status(400).json({ success: false, message: "optionId and profileId are required" });
Expand Down Expand Up @@ -42,7 +42,17 @@ export const handleVote = async (req: Request, res: Response) => {
options: newOptions,
results: newResults,
},
{ lock: { lockId, releaseLock: true } },
{
lock: { lockId, releaseLock: true },
analytics: [
{
analyticName: "completions",
profileId,
urlSlug,
uniqueKey: profileId,
},
],
},
);

await droppedAsset.fetchDataObject();
Expand Down
Loading