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

Chore query refactor #141

Merged
merged 39 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e0a2ed9
markup for billing page
papistacoding Jan 7, 2025
824abb8
progress
papistacoding Jan 9, 2025
705d93a
test api key
papistacoding Jan 9, 2025
6c8f94d
removing extra state
papistacoding Jan 9, 2025
49d9e80
removign warning and adding placeholders
papistacoding Jan 9, 2025
92c7d47
handling dropdown
papistacoding Jan 9, 2025
d84a534
adding env var
papistacoding Jan 9, 2025
f2046d5
schema upgrade
papistacoding Jan 9, 2025
b8c63a0
a bit of refactoring
papistacoding Jan 9, 2025
fe7304e
connecting and updating billing
papistacoding Jan 9, 2025
bd2820b
a bit of refactoring
papistacoding Jan 10, 2025
0e3dd86
email saving
papistacoding Jan 10, 2025
7af4601
fixing toast
papistacoding Jan 10, 2025
6150cec
removing combobox
papistacoding Jan 10, 2025
e89e26c
new hook and handling predictions close
papistacoding Jan 10, 2025
7c2c434
styles
papistacoding Jan 10, 2025
ca8abfd
progress for time badge
papistacoding Jan 10, 2025
3a9c75c
progress for time badge
papistacoding Jan 10, 2025
d0279b2
change subscription redirect
papistacoding Jan 10, 2025
0a55894
progress
papistacoding Jan 10, 2025
50bdf63
suspense and expired logic
papistacoding Jan 10, 2025
628c89c
clg remove
papistacoding Jan 10, 2025
4b340d4
merge
papistacoding Jan 10, 2025
50c9f75
auth and session provider
papistacoding Jan 11, 2025
40fff71
no refetch on windows focus
papistacoding Jan 11, 2025
936a8da
changing client creation logic, signup improvements, useorg hook inpr…
papistacoding Jan 12, 2025
1f491ea
auth logic
papistacoding Jan 13, 2025
4e521dc
Merge branch 'main' into chore-optimizations
papistacoding Jan 13, 2025
272a8bc
conflicts
papistacoding Jan 13, 2025
bb12228
schema update
papistacoding Jan 13, 2025
5a53ebc
removed newly created api for refreshing token, also cleaned some cod…
papistacoding Jan 13, 2025
3c31c29
removing refrehs api
papistacoding Jan 13, 2025
146960b
adding types
papistacoding Jan 13, 2025
7bfbd83
fixing comments
papistacoding Jan 13, 2025
c4b1d4c
query refactor
papistacoding Jan 13, 2025
d5becb9
lint
papistacoding Jan 14, 2025
bb1dcad
merge
papistacoding Jan 14, 2025
3e72562
fixing naming
papistacoding Jan 14, 2025
4cd6fcb
lint
papistacoding Jan 14, 2025
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
Expand Up @@ -6,12 +6,14 @@ import BillingSettings from '@/components/pages/protected/organization/billing/b
import { pageStyles } from '../general-settings/page.styles'
import { useOrganization } from '@/hooks/useOrganization'
import { LoaderCircle } from 'lucide-react'
import { useGetOrganizationBillingQuery } from '@repo/codegen/src/schema'

const OrganizationContent = () => {
const { wrapper } = pageStyles()
const { currentOrg } = useOrganization()
const { currentOrgId } = useOrganization()
const [data] = useGetOrganizationBillingQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })

if (!currentOrg) {
if (data.fetching) {
return (
<div className="w-100 flex justify-center">
<LoaderCircle className="animate-spin" size={20} />
Expand All @@ -21,7 +23,7 @@ const OrganizationContent = () => {

return (
<>
{currentOrg?.personalOrg ? (
{data.data?.organization.personalOrg ? (
<div className={`flex items-center justify-center min-h-[50vh] text-center`}>
<h2 className="text-xl w-full max-w-2xl">
You're currently logged into your personal organization - you can switch into another organization you are a member of, or create an organization to use paid features of the Openlane
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
'use client'
import React, { useEffect, useRef, useState } from 'react'
import { useLoadScript } from '@react-google-maps/api'
import { Data, useLoadScript } from '@react-google-maps/api'
import { Button } from '@repo/ui/button'
import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from '@repo/ui/dialog'
import { Input } from '@repo/ui/input'
import { Label } from '@repo/ui/label'
import { useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useGetOrganizationSettingQuery, useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useOrganization } from '@/hooks/useOrganization'
import useClickOutside from '@/hooks/useClickOutside'

const libraries: any = ['places']

const BillingContactDialog = () => {
const { currentOrgId, currentOrg } = useOrganization()
const { currentOrgId } = useOrganization()
const [setting] = useGetOrganizationSettingQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })
const [{ fetching: isSubmitting }, updateOrg] = useUpdateOrganizationMutation()
const wrapperRef = useClickOutside(() => setShowPredictions(false))
const { isLoaded } = useLoadScript({
Expand Down Expand Up @@ -105,13 +106,14 @@ const BillingContactDialog = () => {
}

useEffect(() => {
if (!currentOrg) {
if (!setting.data) {
return
}
setAddress(currentOrg?.setting?.billingAddress)
setFullName(currentOrg?.setting?.billingContact || '')
setAddress(setting.data.organization.setting?.billingAddress)

setFullName(setting.data.organization.setting?.billingContact || '')
return () => {}
}, [currentOrg])
}, [setting])

return (
<Dialog aria-describedby={undefined}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import { Input } from '@repo/ui/input'
import { Label } from '@repo/ui/label'
import React, { useEffect, useState } from 'react'
import { useOrganization } from '@/hooks/useOrganization'
import { useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useGetOrganizationSettingQuery, useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useToast } from '@repo/ui/use-toast'

const BillingEmailDialog = () => {
const { currentOrg, currentOrgId } = useOrganization()
const { currentOrgId } = useOrganization()
const [emailInput, setEmailInput] = useState('')
const [isOpen, setIsOpen] = useState(false)
const [{ fetching: isSubmitting }, updateOrg] = useUpdateOrganizationMutation()
const { toast } = useToast()

const [settingData] = useGetOrganizationSettingQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })

useEffect(() => {
if (isOpen) {
setEmailInput(currentOrg?.setting?.billingEmail || '')
setEmailInput(settingData.data?.organization.setting?.billingEmail || '')
}
}, [currentOrg, isOpen])
}, [settingData, isOpen])

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import BillingContactDialog from './billing-contract-dialog'
import { useOrganization } from '@/hooks/useOrganization'
import { billingSettingsStyles } from './billing-settings.styles'
import { cn } from '@repo/ui/lib/utils'
import { useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useGetOrganizationSettingQuery, useUpdateOrganizationMutation } from '@repo/codegen/src/schema'

const BillingSettings: React.FC = () => {
const { panel, section, sectionContent, sectionTitle, emailText, paragraph, switchContainer, text } = billingSettingsStyles()
const { currentOrg, currentOrgId } = useOrganization()
const { currentOrgId } = useOrganization()
const [settingData] = useGetOrganizationSettingQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })
const [{ fetching: isSubmitting }, updateOrg] = useUpdateOrganizationMutation()
const billingAddress = settingData.data?.organization.setting?.billingAddress
const formattedAddress = [billingAddress?.line1, billingAddress?.city, billingAddress?.postalCode].filter(Boolean).join(', ')
const email = settingData.data?.organization.setting?.billingEmail || ''

const billingAddress = currentOrg?.setting?.billingAddress || {}
const formattedAddress = [billingAddress.line1, billingAddress.city, billingAddress.postalCode].filter(Boolean).join(', ')
const email = currentOrg?.setting?.billingEmail || ''

const [notificationsEnabled, setNotificationsEnabled] = useState<boolean>(currentOrg?.setting?.billingNotificationsEnabled || false)
const [notificationsEnabled, setNotificationsEnabled] = useState<boolean>(settingData.data?.organization.setting?.billingNotificationsEnabled || false)

const onToggleNotifications = async (checked: boolean) => {
setNotificationsEnabled(checked)
Expand Down Expand Up @@ -50,7 +50,7 @@ const BillingSettings: React.FC = () => {
<p className={cn(paragraph())}>
{formattedAddress}
<br />
{`${currentOrg?.setting?.billingAddress.country || ''}`}
{`${settingData.data?.organization.setting?.billingAddress?.country || ''}`}
</p>
</div>
<BillingContactDialog />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import { Badge } from '@repo/ui/badge'
import { formatDistanceToNowStrict, parseISO, isBefore } from 'date-fns'
import { CircleCheck, ExternalLink } from 'lucide-react'
import { useOrganization } from '@/hooks/useOrganization'
import { useGetOrganizationBillingQuery } from '@repo/codegen/src/schema'

const PricingPlan = () => {
const { currentOrg } = useOrganization()
const { currentOrgId } = useOrganization()

const [data] = useGetOrganizationBillingQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })

const subscription = data.data?.organization.orgSubscriptions?.[0] ?? {}

const subscription = currentOrg?.orgSubscriptions?.[0] ?? {}
// @ts-ignore TODO: MISSING TYPES FROM CODEGEN
const { expiresAt, subscriptionURL, active, productTier, productPrice = {}, features = [] } = subscription
const { amount: price, interval: priceInterval } = productPrice

const formattedExpiresDate = useMemo(() => {
if (!expiresAt || !active) return 'Expired'
if (!expiresAt && !active) return 'Expired'

try {
const expirationDate = parseISO(expiresAt)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client'
import { useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { useGetBillingEmailQuery, useUpdateOrganizationMutation } from '@repo/codegen/src/schema'
import { Input, InputRow } from '@repo/ui/input'
import { Panel, PanelHeader } from '@repo/ui/panel'
import { useForm } from 'react-hook-form'
Expand All @@ -14,8 +14,10 @@ import { useOrganization } from '@/hooks/useOrganization'
const OrganizationEmailForm = () => {
const [isSuccess, setIsSuccess] = useState(false)
const [{ fetching: isSubmitting }, updateOrg] = useUpdateOrganizationMutation()
const { currentOrg, currentOrgId } = useOrganization()
const { currentOrgId } = useOrganization()

const [setting] = useGetBillingEmailQuery({ pause: !currentOrgId, variables: { organizationId: currentOrgId } })
const billingEmail = setting.data?.organization.setting?.billingEmail
const formSchema = z.object({
email: z.string().email({ message: 'Invalid email address' }),
})
Expand All @@ -28,12 +30,12 @@ const OrganizationEmailForm = () => {
})

useEffect(() => {
if (currentOrg) {
if (billingEmail) {
form.reset({
email: currentOrg.setting?.billingEmail ?? undefined,
email: billingEmail ?? undefined,
})
}
}, [currentOrg])
}, [billingEmail])

const updateOrganization = async ({ email }: { email: string }) => {
await updateOrg({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import { z } from 'zod'
import { Button } from '@repo/ui/button'
import { useEffect, useState } from 'react'
import { RESET_SUCCESS_STATE_MS } from '@/constants'
import { useOrganization } from '@/hooks/useOrganization'

const OrganizationNameForm = () => {
const [isSuccess, setIsSuccess] = useState(false)
const [{ fetching: isSubmitting }, updateOrg] = useUpdateOrganizationMutation()
const { data: sessionData } = useSession()
const currentOrgId = sessionData?.user.activeOrganizationId
const [allOrgs] = useGetAllOrganizationsQuery({ pause: !sessionData })
const currentOrganization = allOrgs.data?.organizations.edges?.filter((org) => org?.node?.id === currentOrgId)[0]?.node
const { currentOrgId, allOrgs } = useOrganization()
const currentOrganization = allOrgs.filter((org) => org?.node?.id === currentOrgId)[0]?.node

const formSchema = z.object({
displayName: z.string().min(2, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { GetOrganizationMembersQuery, GetOrganizationMembersQueryVariables, useGetOrganizationMembersQuery, UserAuthProvider } from '@repo/codegen/src/schema'
import { GetSingleOrganizationMembersQuery, GetSingleOrganizationMembersQueryVariables, useGetSingleOrganizationMembersQuery, UserAuthProvider } from '@repo/codegen/src/schema'
import { useSession } from 'next-auth/react'
import { pageStyles } from './page.styles'
import { useState, useEffect, Dispatch, SetStateAction } from 'react'
Expand All @@ -20,7 +20,7 @@ type MembersTableProps = {
setActiveTab: Dispatch<SetStateAction<string>>
}

type Member = NonNullable<NonNullable<GetOrganizationMembersQuery['organization']>['members']>[number]
type Member = NonNullable<NonNullable<GetSingleOrganizationMembersQuery['organization']>['members']>[number]

export const MembersTable = ({ setActiveTab }: MembersTableProps) => {
const { membersSearchRow, membersSearchField, membersButtons, nameRow, copyIcon } = pageStyles()
Expand All @@ -30,11 +30,11 @@ export const MembersTable = ({ setActiveTab }: MembersTableProps) => {
const [copiedText, copyToClipboard] = useCopyToClipboard()
const { toast } = useToast()

const variables: GetOrganizationMembersQueryVariables = {
const variables: GetSingleOrganizationMembersQueryVariables = {
organizationId: session?.user.activeOrganizationId ?? '',
}

const [{ data, fetching, error }, refetch] = useGetOrganizationMembersQuery({
const [{ data, fetching, error }, refetch] = useGetSingleOrganizationMembersQuery({
variables,
pause: !session,
})
Expand Down
12 changes: 1 addition & 11 deletions apps/console/src/components/pages/protected/program/wizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,7 @@ import { programDetailSchema, ProgramDetailsComponent } from './wizard/step-2-de
import { ProgramInviteComponent, programInviteSchema } from './wizard/step-3-team'
import { ProgramObjectAssociationComponent, programObjectAssociationSchema } from './wizard/step-4-associate'
import { ProgramReviewComponent } from './wizard/step-5-review'
import {
CreateProgramWithMembersInput,
ProgramMembershipRole,
useCreateProgramWithMembersMutation,
useGetAllGroupsQuery,
useGetAllInternalPoliciesQuery,
useGetAllOrganizationMembersQuery,
useGetAllProceduresQuery,
useGetAllRisksQuery,
useGetProgramEdgesForWizardQuery,
} from '@repo/codegen/src/schema'
import { CreateProgramWithMembersInput, ProgramMembershipRole, useCreateProgramWithMembersMutation, useGetProgramEdgesForWizardQuery } from '@repo/codegen/src/schema'
import { useSession } from 'next-auth/react'
import { toast } from '@repo/ui/use-toast'
import { useRouter } from 'next/navigation'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ interface AvatarUploadProps extends AvatarUploadVariants {

const AvatarUpload = ({ className }: AvatarUploadProps) => {
const { toast } = useToast()
const { currentOrg } = useOrganization()
const { allOrgs, currentOrgId } = useOrganization()

const [isCroppingModalOpen, setIsCroppingModalOpen] = useState(false)
const [crop, setCrop] = useState({ x: 0, y: 0 })
Expand All @@ -36,11 +36,12 @@ const AvatarUpload = ({ className }: AvatarUploadProps) => {
</>
)

const selectedOrg = allOrgs.filter((org) => org?.node?.id === currentOrgId).map((org) => org?.node)[0] || null

useEffect(() => {
if (currentOrg?.avatarRemoteURL) {
setAvatarUrl(currentOrg?.avatarRemoteURL)
}
}, [currentOrg])
const avatarRemoteURL = selectedOrg?.avatarRemoteURL
setAvatarUrl(avatarRemoteURL || null)
}, [selectedOrg])

const onDrop = useCallback((acceptedFiles: FileWithPath[]) => {
const file = acceptedFiles[0]
Expand Down Expand Up @@ -97,7 +98,7 @@ const AvatarUpload = ({ className }: AvatarUploadProps) => {
<div className={avatarPreview()}>
<Avatar variant="extra-large">
{avatarUrl && <AvatarImage src={avatarUrl} />}
<AvatarFallback>{currentOrg?.name?.substring(0, 2)}</AvatarFallback>
<AvatarFallback>{selectedOrg?.name?.substring(0, 2)}</AvatarFallback>
</Avatar>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useSession } from 'next-auth/react'
import { switchOrganization } from '@/lib/user'
import { Loading } from '../loading/loading'
import { useOrganization } from '@/hooks/useOrganization'
import { useGetAllOrganizationsWithMembersQuery } from '@repo/codegen/src/schema'

export const OrganizationSelector = () => {
const { data: sessionData, update: updateSession } = useSession()
Expand All @@ -25,8 +26,11 @@ export const OrganizationSelector = () => {
currentOrgName: '',
})

const { currentOrgId, allOrgs: orgs, currentOrg } = useOrganization()
const { currentOrgId } = useOrganization()

const [{ data: organizationsData, fetching, error }] = useGetAllOrganizationsWithMembersQuery()
const orgs = organizationsData?.organizations?.edges ?? []
const currentOrg = orgs.filter((org) => org?.node?.id === currentOrgId)[0]?.node
const { container, logoWrapper, organizationLabel, organizationDropdown, allOrganizationsLink, popoverContent, searchWrapper, orgWrapper, orgInfo, orgTitle, orgSelect } =
organizationSelectorStyles()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import { Panel, PanelHeader } from '@repo/ui/panel'
import { existingOrganizationsStyles } from './existing-organizations.styles'
import { useGetAllOrganizationsQuery } from '@repo/codegen/src/schema'
import { Avatar, AvatarFallback } from '@repo/ui/avatar'
import { Button } from '@repo/ui/button'
import { Tag } from '@repo/ui/tag'
import { switchOrganization } from '@/lib/user'
import { useSession } from 'next-auth/react'
import { useRouter } from 'next/navigation'
import { useGetAllOrganizationsWithMembersQuery } from '@repo/codegen/src/schema'

export const ExistingOrganizations = () => {
const { data: sessionData, update: updateSession } = useSession()
const currentOrg = sessionData?.user.activeOrganizationId

const { container, orgWrapper, orgInfo, orgSelect, orgTitle } = existingOrganizationsStyles()
const [{ data, fetching, error }] = useGetAllOrganizationsQuery({
pause: !sessionData,
})

const [{ data: organizations, fetching, error }] = useGetAllOrganizationsWithMembersQuery()

const { push } = useRouter()

if (!data || fetching || error) {
if (!organizations || fetching || error) {
return null
}

const orgs = data.organizations.edges?.filter((org) => !org?.node?.personalOrg) || []
const orgs = organizations?.organizations.edges?.filter((org) => !org?.node?.personalOrg) || []

if (orgs.length === 0) {
return null
Expand Down
8 changes: 2 additions & 6 deletions apps/console/src/hooks/useOrganization.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ import { useMemo } from 'react'

export const useOrganization = () => {
const { data: sessionData, status } = useSession()
const currentOrgId = useMemo(() => sessionData?.user?.activeOrganizationId, [sessionData])
const currentOrgId = sessionData?.user?.activeOrganizationId

const [allOrgs] = useGetAllOrganizationsQuery({
pause: status === 'loading' || !sessionData,
})
const organizations = allOrgs?.data?.organizations?.edges || []

const currentOrg = useMemo(() => {
return organizations.find((org) => org?.node?.id === currentOrgId)?.node
}, [organizations, currentOrgId])

return { currentOrgId, allOrgs: allOrgs?.data?.organizations?.edges || [], currentOrg }
return { currentOrgId, allOrgs: organizations }
}
Loading
Loading