Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 768d52f

Browse files
HasithDeAlwisMFarabi619
authored andcommitted
feat(api): update user profile on successfully agreing to terms and conditions
1 parent 193943d commit 768d52f

File tree

5 files changed

+67
-46
lines changed

5 files changed

+67
-46
lines changed

apps/portal/app/routes/terms.tsx

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
11
import type { LoaderData } from '@cuhacking/portal/types/legal'
2-
import type { LoaderFunction } from '@remix-run/node'
3-
import { userFlowActor } from '@/engine/actors/user'
2+
import type { ActionFunction, LoaderFunction } from '@remix-run/node'
43
import { getLegalData } from '@cuhacking/portal/features/legal/api/data'
4+
import { updateTerms } from '@cuhacking/portal/features/legal/api/update-terms'
55
import { getCurrentUser } from '@cuhacking/portal/features/profile/api/user'
66
import { LegalPage } from '@cuhacking/portal/pages/legal'
77
import { json, redirect } from '@remix-run/node'
88
import { useLoaderData } from '@remix-run/react'
9+
import { getSession } from '../sessions'
910

10-
/* export const loader: LoaderFunction = async () => {
11-
* const currentState = userFlowActor.getSnapshot()?.value
12-
*
13-
* switch (currentState) {
14-
* case 'unauthenticated':
15-
* return redirect('/login')
16-
* case 'legal':
17-
* return redirect('/terms')
18-
* case 'profile_incomplete':
19-
* return redirect('/profile')
20-
* case 'dashboard':
21-
* case 'registered':
22-
* return null
23-
* default:
24-
* return redirect('/login')
25-
* }
26-
* } */
27-
28-
export function action() {
29-
try {
30-
/* setIsLoading(true) */
31-
userFlowActor.send({ type: 'TERM_SUCCESS' })
11+
export const loader: LoaderFunction = async ({ request }) => {
12+
const cookie = request.headers.get('Cookie')
13+
const { legalData } = getLegalData()
14+
const user = await getCurrentUser({ cookie })
3215

33-
return redirect('/profile')
34-
}
35-
catch (error) {
36-
console.error('Login failed:', error)
37-
}
38-
finally {
39-
/* setIsLoading(false) */
16+
if (!user) {
17+
return redirect('/login')
4018
}
19+
20+
/* if (user?.agreedToTerms) {
21+
* return redirect('/profile')
22+
* } */
23+
24+
return json<LoaderData>({ legalData, cookie })
4125
}
4226

43-
export const loader: LoaderFunction = async () => {
44-
const { legalData } = getLegalData()
45-
const user = getCurrentUser()
46-
return json<LoaderData>({ legalData, user })
27+
export const action: ActionFunction = async ({ request }) => {
28+
const cookie = request.headers.get('Cookie')
29+
const session = await getSession(cookie)
30+
const userId = session.get('userId')
31+
32+
if (!userId) {
33+
return redirect('/login')
34+
}
35+
return await updateTerms(userId, cookie)
4736
}
4837

4938
export default function Index() {
50-
const { legalData, user } = useLoaderData<LoaderData>()
51-
52-
return <LegalPage legalData={legalData} user={user} />
39+
const { legalData } = useLoaderData<LoaderData>()
40+
return <LegalPage legalData={legalData} />
5341
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import process from 'node:process'
2+
import { json, redirect } from '@remix-run/node'
3+
4+
const API_URL = `${process.env.NODE_ENV === 'development' ? process.env.CUHACKING_2025_AXIOM_LOCAL_URL : process.env.CUHACKING_2025_AXIOM_PUBLIC_URL}`
5+
export async function updateTerms(userId: string, cookie: string | null) {
6+
try {
7+
const response = await fetch(`${API_URL}/api/users/${userId}`, {
8+
method: 'PATCH',
9+
headers: {
10+
'Content-Type': 'application/json',
11+
'Cookie': cookie || '',
12+
},
13+
body: JSON.stringify({ agreedToTerms: true }),
14+
})
15+
16+
if (!response.ok) {
17+
return json(
18+
{ error: 'Failed to update user terms' },
19+
{ status: response.status },
20+
)
21+
}
22+
23+
return redirect('/profile')
24+
}
25+
catch (error) {
26+
// Log the error for debugging purposes
27+
console.error('Error updating user terms:', error)
28+
29+
// Return a generic error response
30+
return json(
31+
{ error: 'An unexpected error occurred' },
32+
{ status: 500 },
33+
)
34+
}
35+
}

libs/portal/features/legal/ui/terms.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ export function Legal({ legalData }: LegalPageProps) {
143143
</Accordion>
144144

145145
<div className="px-4 flex justify-center pb-6">
146-
<Form method="post">
146+
<Form method="patch">
147147
<Button
148-
onClick={() => { }}
149-
disabled={!allChecked || !allScrolled}
148+
disabled={false}
150149
variant="secondary"
151150
aria-label="Redirect to Profile"
152151
type="submit"
@@ -158,7 +157,7 @@ export function Legal({ legalData }: LegalPageProps) {
158157
},
159158
)}
160159
>
161-
<Typography variant="h6">LET'S CREATE YOUR PROFILE</Typography>
160+
<Typography variant="h6"><span>LET'S CREATE YOUR PROFILE</span></Typography>
162161
</Button>
163162
</Form>
164163

libs/portal/pages/legal/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import type { LegalPageProps } from '@cuhacking/portal/types/terms'
22
import { Legal } from '@cuhacking/portal/features/legal'
33
import { Layout } from '@cuhacking/portal/ui/layout'
44

5-
export function LegalPage({ legalData, user }: LegalPageProps) {
5+
export function LegalPage({ legalData }: LegalPageProps) {
66
return (
7-
<Layout user={user}>
8-
<Legal legalData={legalData} user={user} />
7+
<Layout>
8+
<Legal legalData={legalData} />
99
</Layout>
1010
)
1111
}

libs/portal/types/terms.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ export interface LoaderData {
1515
}
1616
export interface LegalPageProps {
1717
legalData: LegalItem[]
18-
user: User
1918
}

0 commit comments

Comments
 (0)