Skip to content

Commit

Permalink
wip: update scope and redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jan 21, 2024
1 parent c87933f commit fd7ba7b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 31 deletions.
2 changes: 2 additions & 0 deletions packages/app/src/actions/team-switcher.action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { cookies } from 'next/headers'
import { z } from 'zod'
import { rhfActionSetScopeSchema } from './teams-switcher.schema'
import { redirect } from 'next/navigation'
import { revalidatePath } from 'next/cache'

export async function rhfActionSetScope(
opts: z.infer<typeof rhfActionSetScopeSchema>
) {
cookies().set('scope', opts)
revalidatePath(opts !== 'personal' ? `/teams/${opts}` : `/dashboard`)
redirect(opts !== 'personal' ? `/teams/${opts}` : `/dashboard`)
}
8 changes: 5 additions & 3 deletions packages/app/src/components/default-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@/styles/globals.css'
import type { ReactNode } from 'react'
import { Suspense, type ReactNode } from 'react'

import { cookies } from 'next/headers'
import { MainNav } from '@/components/main-nav'
Expand All @@ -9,25 +9,27 @@ import { Toaster } from '@/components/ui/toaster'
import { UserNav } from '@/components/user-nav'
import Footer from '@/components/footer'
import TeamSwitcher from '@/components/team-switcher'
import { api } from '@/trpc/server-http'

interface DefaultLayoutProps {
children?: ReactNode | undefined
fallback?: ReactNode
}

export default function DefaultLayout({
export default async function DefaultLayout({
children,
fallback = <MainNav className="mx-6" />
}: DefaultLayoutProps) {
const cookiesList = cookies()
const scope = cookiesList.get('scope')
const user = await api.users.get.query()

return (
<>
<div className="flex-col">
<div className="border-b">
<div className="flex h-16 items-center px-4">
<TeamSwitcher scope={scope?.value} />
<TeamSwitcher scope={scope?.value} user={user} />
{fallback}
<div className="ml-auto flex items-center space-x-4">
<Search />
Expand Down
12 changes: 6 additions & 6 deletions packages/app/src/components/team-switcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ import {
PopoverContent,
PopoverTrigger
} from '@/components/ui/popover'
import { api } from '@/trpc/client'
import { usePathname, useRouter } from 'next/navigation'
import { useRouter } from 'next/navigation'
import { type User } from '@/db/models/users'

export type TeamSwitcherProps = {
user: User | null
className?: string
scope?: string
}

export default function TeamSwitcher({
user,
className,
scope = 'personal'
}: React.PropsWithChildren<TeamSwitcherProps>) {
const user = React.use(api.users.get.query())

const groups = React.useMemo(
() => [
{
Expand All @@ -78,7 +78,6 @@ export default function TeamSwitcher({
],
[user]
)
const pathname = usePathname()
const selectedTeam = React.useMemo(
() =>
groups.flatMap(group => group.teams).find(team => scope === team?.value),
Expand Down Expand Up @@ -107,7 +106,8 @@ export default function TeamSwitcher({

React.useEffect(() => {
if (mutation.status === 'success') {
router.push(`/teams/${mutation.data.slug}/settings`)
setShowNewTeamDialog(false)
updateScope(mutation.data.slug)
}
}, [router, mutation.status, mutation.data])

Expand Down
22 changes: 9 additions & 13 deletions packages/app/src/db/models/users-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import {
PrimaryKey,
DataType,
AutoIncrement,
ForeignKey,
Unique,
Default,
BelongsToMany
ForeignKey
} from 'sequelize-typescript'
import { User } from './users'
import { Role } from './roles'
import { Team } from './teams'

export interface UserRoleAttributes {
id: string
email: string
name: string
emailVerified: string
image?: string
id: bigint
userId: string
roleId: bigint
teamId: string
}

export type UserRoleCreationAttributes = Omit<UserRoleAttributes, 'id'>
Expand All @@ -34,17 +30,17 @@ export class UserRole extends Model<
@PrimaryKey
@AutoIncrement
@Column(DataType.BIGINT)
id!: bigint
declare id: bigint

@ForeignKey(() => User)
@Column(DataType.UUIDV4)
userId?: string
declare userId: string

@ForeignKey(() => Role)
@Column(DataType.BIGINT)
roleId?: bigint
declare roleId: bigint

@ForeignKey(() => Team)
@Column(DataType.UUIDV4)
teamId?: bigint
declare teamId: string
}
9 changes: 9 additions & 0 deletions packages/app/src/db/services/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { z } from 'zod'
import sequelize from '@/db/config/config'
import { UserTeam } from '../models/users-teams'
import { Workload } from '../models/workload'
import { UserRole } from '../models/users-roles'

export type Pagination = {
offset?: number
Expand All @@ -23,6 +24,14 @@ export const createTeam = async (opts: CreateTeamSchema) =>
{ userId: opts.userId, teamId: team.id },
{ transaction }
)
await UserRole.create(
{
userId: opts.userId,
teamId: team.id,
roleId: BigInt(1)
},
{ transaction }
)

return team.dataValues
})
Expand Down
6 changes: 1 addition & 5 deletions packages/app/src/server/routers/actions/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ export const listProfilesQuestions = protectedProcedure.query(
export const listByTeam = protectedProcedure
.use(isAllowed('read'))
.input(ListProfileByTeamSlug)
.query(async opts => {
console.log(opts)

return await listProfileByTeamSlug({ ...opts.input })
})
.query(async opts => await listProfileByTeamSlug({ ...opts.input }))

export const profilesRouter = router({
list: listProfiles,
Expand Down
4 changes: 0 additions & 4 deletions packages/app/src/server/routers/actions/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ export const listWorkloads = protectedProcedure
.input(ListWorkloadByTeamSlug)
.query(async opts => await listWorkloadsByTeamSlug({ ...opts.input }))

// export const addTeam = protectedProcedure
// .input(TeamsCreateSchema)
// .mutation(async opts => await createTeam({}))

export const teamsRouter = router({
list: listTeams,
// add: addTeam,
Expand Down

0 comments on commit fd7ba7b

Please sign in to comment.