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

Traack remove local storage code #138

Merged
merged 7 commits into from
Jul 27, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Warnings:

- You are about to drop the column `xid` on the `User` table. All the data in the column will be lost.

*/
BEGIN TRY

BEGIN TRAN;

-- DropIndex
ALTER TABLE [dbo].[User] DROP CONSTRAINT [User_xid_key];

-- AlterTable
ALTER TABLE [dbo].[User] DROP COLUMN [xid];

COMMIT TRAN;

END TRY
BEGIN CATCH

IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRAN;
END;
THROW

END CATCH
1 change: 0 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ model Session {

model User {
id String @id @default(cuid())
xid String? @unique @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
Expand Down
1 change: 0 additions & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const AuthShowcase: React.FC = () => {
const { data: sessionData } = useSession();

const handleSignOut = async () => {
localStorage.clear();
await signOut();
};

Expand Down
44 changes: 18 additions & 26 deletions src/pages/polissurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,36 @@ import ProgressBar from "../components/ProgressBar";
const PolisSurvey: NextPage = () => {
const router = useRouter();
const { surveyId } = router.query;
const [userID, setUserID] = useState<string>();
const xidDataDB = api.user.getXID.useQuery();
const userID = api.user.getId.useQuery()?.data?.id;

useEffect(() => {
if (xidDataDB.data && xidDataDB.data.xid !== null) {
setUserID(String(xidDataDB.data.xid));
localStorage.polisUserXID = String(xidDataDB.data.xid);
} else if (
localStorage.polisUserXID !== "undefined" &&
localStorage.polisUserXID !== undefined
) {
setUserID(String(localStorage.polisUserXID));
console.log("Existing polisUserXID found:", localStorage.polisUserXID);
} else {
console.log("Assigning new polisUserXID:", userID);
localStorage.polisUserXID = userID;
}
}, [userID, xidDataDB.data?.xid]);
if (userID !== undefined && userID !== "") {
const script = document.createElement("script");

useEffect(() => {
const script = document.createElement("script");
script.src = "https://pol.is/embed.js";
script.async = true;

script.src = "https://pol.is/embed.js";
script.async = true;
document.body.appendChild(script);

document.body.appendChild(script);
return () => {
document.body.removeChild(script);
};
}
}, [userID]);

return () => {
document.body.removeChild(script);
};
}, []);
if (userID === "" || userID === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for this page to load if the user hasn't logged in yet?

If yes, then I propose that if we detect that the userID isn't in the session that we redirect the user back to the home page instead of just saying Loading Surveys. What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we want to redirect the user back to the home page if they're not logged in for all pages, but I'll add that in a separate PR

return (
<>
<h1 className="text-white">Loading Surveys</h1>
</>
);
}
return (
<div className="flex h-full flex-col items-center shadow-xl">
<h1 className="mt-8 mb-4 text-lg font-semibold text-white md:mt-6 md:text-3xl">
Step 6: Fill out the Pol.is survey
</h1>
<ProgressBar completed={100} />

<div
id="polis-container"
className="mx-auto mt-8 h-[80%] w-[80%] overflow-y-scroll"
Expand Down
4 changes: 2 additions & 2 deletions src/server/api/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const userRouter = createTRPCRouter({
},
});
}),
getXID: publicProcedure.query(async ({ ctx }) => {
getId: publicProcedure.query(async ({ ctx }) => {
if (!ctx.session) {
console.log("Not authenticated");
return null;
Expand All @@ -42,7 +42,7 @@ export const userRouter = createTRPCRouter({
return ctx.prisma.user.findUnique({
where: { id: userId },
select: {
xid: true,
id: true,
},
});
}),
Expand Down
Loading