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

Avenmia/remove census tract table #83

Merged
merged 5 commits into from
Mar 15, 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,31 @@
/*
Warnings:

- You are about to drop the `CensusTract` table. If the table is not empty, all the data it contains will be lost.

*/
BEGIN TRY

BEGIN TRAN;

-- DropForeignKey
ALTER TABLE [dbo].[User] DROP CONSTRAINT [User_censusTractId_fkey];

-- AlterTable
ALTER TABLE [dbo].[User] ADD [censustract] NVARCHAR(1000);

-- DropTable
DROP TABLE [dbo].[CensusTract];

COMMIT TRAN;

END TRY
BEGIN CATCH

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

END CATCH
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Warnings:

- You are about to drop the column `censusTractId` 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_censusTractId_key];

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

COMMIT TRAN;

END TRY
BEGIN CATCH

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

END CATCH
8 changes: 1 addition & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ model ZipCode {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model CensusTract {
id String @id
name String
user User?
}

// Necessary for Next auth
model Account {
Expand Down Expand Up @@ -87,12 +82,11 @@ model User {
email String? @unique
emailVerified DateTime?
image String?
censusTractId String? @unique
demoSurveyCompleted Boolean @default(false)
accounts Account[]
sessions Session[]
zipcode ZipCode?
censustract CensusTract? @relation(fields: [censusTractId], references: [id])
censustract String?
UserSurveyAnswers UserSurveyAnswers[]
}

Expand Down
6 changes: 3 additions & 3 deletions src/components/censusmap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ const CensusTractMap: NextPage = () => {

useEffect(() => {
if (censusTractDB && censusTractDB.data) {
if (censusTractDB.data.censusTractId !== null) {
if (censusTractDB.data.censustract !== null) {
setCensusTractComplete(true);
}
setUserCensusTract(censusTractDB.data?.censusTractId);
setUserCensusTract(censusTractDB.data?.censustract);
}
}, [censusTractDB.data?.censusTractId]);
}, [censusTractDB.data?.censustract]);

const geoJsonRef = useRef();
const handleFeature = (feature: Feature<Geometry, any>, layer: Layer) => {
Expand Down
1 change: 0 additions & 1 deletion src/components/survey/demographicssurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default function DemographicsSurvey() {
if (surveyCompletedDB.data.demoSurveyCompleted !== false) {
setSurveyCompleted(true);
}
// setUserCensusTract(censusTractDB.data?.censusTractId);
}
}, [surveyCompletedDB.data?.demoSurveyCompleted]);

Expand Down
22 changes: 16 additions & 6 deletions src/components/survey/textAnswers.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
interface TextAnswerProps {
updateCurrentAnswer: (val: string) => void;
number?:boolean
number?: boolean;
}

export default function TextAnswer({ updateCurrentAnswer, number}: TextAnswerProps) {

export default function TextAnswer({
updateCurrentAnswer,
number,
}: TextAnswerProps) {
return (
<>
<input className="border-2 border-rose-500" type="text" id="textQuestion" name="textQuestion" pattern={number ? "^1?[0-9]{1,2}$1[0-9][0-9]" : ""} title={number ? "Please enter a number." : ""} onChange={(e) => updateCurrentAnswer(e.target.value)} />
<input
className="form-input rounded"
type="text"
id="textQuestion"
name="textQuestion"
pattern={number ? "^1?[0-9]{1,2}$1[0-9][0-9]" : ""}
title={number ? "Please enter a number." : ""}
onChange={(e) => updateCurrentAnswer(e.target.value)}
/>
</>
)
}
);
}
19 changes: 4 additions & 15 deletions src/server/api/routers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,11 @@ export const userRouter = createTRPCRouter({
}
const { id: userId } = ctx.session.user;
const censusTract = input.censusTract;
const existingCensusTracts = await ctx.prisma.censusTract.findMany({
where: { id: censusTract },
});
if (existingCensusTracts.length === 0) {
await ctx.prisma.censusTract.create({
data: {
id: censusTract,
name: censusTract,
},
});
}
// TODO: Since each user is unique check if the user already has a zip code first

return ctx.prisma.user.update({
where: { id: userId },
data: {
censusTractId: censusTract,
censustract: censusTract,
},
});
}),
Expand All @@ -53,7 +42,7 @@ export const userRouter = createTRPCRouter({
return ctx.prisma.user.findUnique({
where: { id: userId },
select: {
censusTractId: true,
censustract: true,
},
});
}),
Expand Down Expand Up @@ -82,7 +71,7 @@ export const userRouter = createTRPCRouter({
return ctx.prisma.user.update({
where: { id: userId },
data: {
censusTractId: null,
censustract: null,
},
});
}),
Expand Down