Skip to content

Commit 3a2ffb5

Browse files
avenmiadinoboy197
andauthored
Traack remove local storage code (#138)
* First attempt at removing local storage code * Fixing api call * Only show the pol.is survey if the user xid is not null * Removing xid from user table and using the user's ID * Fixing script appending --------- Co-authored-by: Taylor Raack <[email protected]>
1 parent f62ae81 commit 3a2ffb5

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `xid` on the `User` table. All the data in the column will be lost.
5+
6+
*/
7+
BEGIN TRY
8+
9+
BEGIN TRAN;
10+
11+
-- DropIndex
12+
ALTER TABLE [dbo].[User] DROP CONSTRAINT [User_xid_key];
13+
14+
-- AlterTable
15+
ALTER TABLE [dbo].[User] DROP COLUMN [xid];
16+
17+
COMMIT TRAN;
18+
19+
END TRY
20+
BEGIN CATCH
21+
22+
IF @@TRANCOUNT > 0
23+
BEGIN
24+
ROLLBACK TRAN;
25+
END;
26+
THROW
27+
28+
END CATCH

prisma/schema.prisma

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ model Session {
7171

7272
model User {
7373
id String @id @default(cuid())
74-
xid String? @unique @default(cuid())
7574
name String?
7675
email String? @unique
7776
emailVerified DateTime?

src/pages/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ const AuthShowcase: React.FC = () => {
8484
const { data: sessionData } = useSession();
8585

8686
const handleSignOut = async () => {
87-
localStorage.clear();
8887
await signOut();
8988
};
9089

src/pages/polissurvey.tsx

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,36 @@ import ProgressBar from "../components/ProgressBar";
88
const PolisSurvey: NextPage = () => {
99
const router = useRouter();
1010
const { surveyId } = router.query;
11-
const [userID, setUserID] = useState<string>();
12-
const xidDataDB = api.user.getXID.useQuery();
11+
const userID = api.user.getId.useQuery()?.data?.id;
1312

1413
useEffect(() => {
15-
if (xidDataDB.data && xidDataDB.data.xid !== null) {
16-
setUserID(String(xidDataDB.data.xid));
17-
localStorage.polisUserXID = String(xidDataDB.data.xid);
18-
} else if (
19-
localStorage.polisUserXID !== "undefined" &&
20-
localStorage.polisUserXID !== undefined
21-
) {
22-
setUserID(String(localStorage.polisUserXID));
23-
console.log("Existing polisUserXID found:", localStorage.polisUserXID);
24-
} else {
25-
console.log("Assigning new polisUserXID:", userID);
26-
localStorage.polisUserXID = userID;
27-
}
28-
}, [userID, xidDataDB.data?.xid]);
14+
if (userID !== undefined && userID !== "") {
15+
const script = document.createElement("script");
2916

30-
useEffect(() => {
31-
const script = document.createElement("script");
17+
script.src = "https://pol.is/embed.js";
18+
script.async = true;
3219

33-
script.src = "https://pol.is/embed.js";
34-
script.async = true;
20+
document.body.appendChild(script);
3521

36-
document.body.appendChild(script);
22+
return () => {
23+
document.body.removeChild(script);
24+
};
25+
}
26+
}, [userID]);
3727

38-
return () => {
39-
document.body.removeChild(script);
40-
};
41-
}, []);
28+
if (userID === "" || userID === undefined) {
29+
return (
30+
<>
31+
<h1 className="text-white">Loading Surveys</h1>
32+
</>
33+
);
34+
}
4235
return (
4336
<div className="flex h-full flex-col items-center shadow-xl">
4437
<h1 className="mt-8 mb-4 text-lg font-semibold text-white md:mt-6 md:text-3xl">
4538
Step 6: Fill out the Pol.is survey
4639
</h1>
4740
<ProgressBar completed={100} />
48-
4941
<div
5042
id="polis-container"
5143
className="mx-auto mt-8 h-[80%] w-[80%] overflow-y-scroll"

src/server/api/routers/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const userRouter = createTRPCRouter({
3333
},
3434
});
3535
}),
36-
getXID: publicProcedure.query(async ({ ctx }) => {
36+
getId: publicProcedure.query(async ({ ctx }) => {
3737
if (!ctx.session) {
3838
console.log("Not authenticated");
3939
return null;
@@ -42,7 +42,7 @@ export const userRouter = createTRPCRouter({
4242
return ctx.prisma.user.findUnique({
4343
where: { id: userId },
4444
select: {
45-
xid: true,
45+
id: true,
4646
},
4747
});
4848
}),

0 commit comments

Comments
 (0)