-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from compsci-adl/feat/settings
feat: Add settings page
- Loading branch information
Showing
24 changed files
with
717 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use client'; | ||
|
||
import { useRouter } from 'next/navigation'; | ||
import React, { useState } from 'react'; | ||
import Sidebar from './Sidebar'; | ||
import type { MembershipPayment } from './page'; | ||
import MembershipSettings from './tabs/MembershipSettings'; | ||
|
||
export const TAB_NAMES = ['Account', 'Personal Info', 'Membership', 'Notifications'] as const; | ||
export type TabNames = (typeof TAB_NAMES)[number]; | ||
|
||
export type SettingData = { membershipPayment: MembershipPayment }; | ||
export type SettingTabProps = { settingData: SettingData }; | ||
type SettingTabComponent = ({ settingData }: SettingTabProps) => React.ReactNode; | ||
const SETTING_TABS = { | ||
Account: () => <div>WIP</div>, | ||
'Personal Info': () => <div>WIP</div>, | ||
Membership: MembershipSettings, | ||
Notifications: () => <div>WIP</div>, | ||
} as const satisfies Record<TabNames, SettingTabComponent>; | ||
|
||
export default function Settings({ settingData }: { settingData: SettingData }) { | ||
const [tab, setTab] = useState<TabNames>('Membership'); | ||
const Tab = SETTING_TABS[tab]; | ||
|
||
const { refresh } = useRouter(); | ||
|
||
return ( | ||
<> | ||
<Sidebar | ||
currentTab={tab} | ||
onTabChange={(tab) => { | ||
setTab(tab); | ||
refresh(); | ||
}} | ||
/> | ||
<div className="flex w-full"> | ||
<Tab settingData={settingData} /> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { TAB_NAMES, type TabNames } from './Settings'; | ||
|
||
interface SidebarTabProps { | ||
tabName: TabNames; | ||
currentTab: TabNames; | ||
onTabChange: (tab: TabNames) => void; | ||
} | ||
|
||
function SidebarTab({ tabName, currentTab, onTabChange }: SidebarTabProps) { | ||
const selected = currentTab === tabName; | ||
return ( | ||
<button | ||
className={`text-left ${selected ? 'cursor-default font-bold' : 'hover:underline'}`} | ||
onClick={() => { | ||
if (!selected) { | ||
onTabChange(tabName); | ||
} | ||
}} | ||
> | ||
{tabName} | ||
</button> | ||
); | ||
} | ||
|
||
interface SidebarProps { | ||
currentTab: TabNames; | ||
onTabChange: (tab: TabNames) => void; | ||
} | ||
|
||
export default function Sidebar({ currentTab, onTabChange }: SidebarProps) { | ||
return ( | ||
<div className="flex w-48 flex-col space-y-4 border-r-2 border-grey"> | ||
{TAB_NAMES.map((tab, i) => ( | ||
<SidebarTab | ||
currentTab={currentTab} | ||
onTabChange={onTabChange} | ||
tabName={tab} | ||
key={i} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import FancyRectangle from '@/components/FancyRectangle'; | ||
import Title from '@/components/Title'; | ||
import { db } from '@/db'; | ||
import { checkUserExists } from '@/db/queries'; | ||
import { memberTable } from '@/db/schema'; | ||
import { redisClient } from '@/lib/redis'; | ||
import { squareClient } from '@/lib/square'; | ||
import { currentUser } from '@clerk/nextjs'; | ||
import { eq } from 'drizzle-orm'; | ||
import Link from 'next/link'; | ||
import { notFound } from 'next/navigation'; | ||
import Settings from './Settings'; | ||
|
||
const verifyMembershipPayment = async (clerkId: string) => { | ||
// Get user's membership expiry date from the database | ||
const [{ membershipExpiresAt }] = await db | ||
.select({ | ||
membershipExpiresAt: memberTable.membershipExpiresAt, | ||
}) | ||
.from(memberTable) | ||
.where(eq(memberTable.clerkId, clerkId)); | ||
// If membership expiry date exists, return the existing date | ||
if (membershipExpiresAt) { | ||
return { paid: true as const, membershipExpiresAt }; | ||
} | ||
|
||
const paymentId = await redisClient.hGet(`payment:membership:${clerkId}`, 'paymentId'); | ||
if (!paymentId) { | ||
// Membership payment for the user does not exist | ||
return { paid: false as const }; | ||
} | ||
|
||
const resp = await squareClient.checkoutApi.retrievePaymentLink(paymentId); | ||
const respFields = resp.result; | ||
if (!respFields.paymentLink || respFields.paymentLink.id !== paymentId) { | ||
// Payment has not been made | ||
return { paid: false as const }; | ||
} | ||
|
||
// Set expiry date to be the January 1st of the following year | ||
const now = new Date(); | ||
const expiryDate = new Date(`${now.getFullYear() + 1}-01-01`); | ||
await db | ||
.update(memberTable) | ||
.set({ membershipExpiresAt: expiryDate }) | ||
.where(eq(memberTable.id, clerkId)); | ||
|
||
// Delete key from Redis since it is no longer needed | ||
await redisClient.del(`payment:membership:${clerkId}`); | ||
|
||
return { paid: true as const, membershipExpiresAt: expiryDate }; | ||
}; | ||
export type MembershipPayment = Awaited<ReturnType<typeof verifyMembershipPayment>>; | ||
|
||
export default async function SettingsPage() { | ||
const user = await currentUser(); | ||
if (!user) return notFound(); | ||
|
||
const exists = await checkUserExists(user.id); | ||
const membershipPayment = await verifyMembershipPayment(user.id); | ||
|
||
return ( | ||
<div className="flex flex-col"> | ||
<div className="flex justify-center"> | ||
<Title colour="purple">Settings</Title> | ||
</div> | ||
<section className="mt-12 flex justify-center"> | ||
<FancyRectangle colour="purple" offset="8" filled={true}> | ||
<div className="z-0 flex w-fit gap-8 border-4 border-black bg-white px-8 py-8 text-black md:w-[48rem] md:px-12 md:py-12"> | ||
{exists ? ( | ||
<Settings settingData={{ membershipPayment }} /> | ||
) : ( | ||
<h2 className="text-2xl"> | ||
Please finishing{' '} | ||
<Link href="/join" className="font-bold text-purple"> | ||
signing up | ||
</Link>{' '} | ||
first. | ||
</h2> | ||
)} | ||
</div> | ||
</FancyRectangle> | ||
</section> | ||
</div> | ||
); | ||
} |
Oops, something went wrong.