Skip to content

Commit

Permalink
fix: Modal bugs & queries errors (#1036)
Browse files Browse the repository at this point in the history
* fixes

* comment removed

* small fix
  • Loading branch information
ncomerci committed Jun 21, 2023
1 parent db7ada3 commit 03ad98b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 18 deletions.
11 changes: 2 additions & 9 deletions src/clients/Governance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { QuarterBudgetAttributes } from '../entities/QuarterBudget/types'
import { SubscriptionAttributes } from '../entities/Subscription/types'
import { Topic } from '../entities/SurveyTopic/types'
import { ProjectHealth, UpdateAttributes } from '../entities/Updates/types'
import { ProjectHealth, UpdateAttributes, UpdateResponse } from '../entities/Updates/types'
import { Vote, VotedProposal } from '../entities/Votes/types'
import Time from '../utils/date/Time'

Expand Down Expand Up @@ -229,14 +229,7 @@ export class Governance extends API {
}

async getProposalUpdates(proposal_id: string) {
const result = await this.fetch<
ApiResponse<{
publicUpdates: UpdateAttributes[]
pendingUpdates: UpdateAttributes[]
nextUpdate: UpdateAttributes
currentUpdate: UpdateAttributes | null
}>
>(`/proposals/${proposal_id}/updates`)
const result = await this.fetch<ApiResponse<UpdateResponse>>(`/proposals/${proposal_id}/updates`)
return result.data
}

Expand Down
7 changes: 7 additions & 0 deletions src/entities/Updates/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,10 @@ export enum ProjectHealth {
AtRisk = 'atRisk',
OffTrack = 'offTrack',
}

export type UpdateResponse = {
publicUpdates: UpdateAttributes[]
pendingUpdates: UpdateAttributes[]
nextUpdate: UpdateAttributes
currentUpdate: UpdateAttributes | null
}
4 changes: 3 additions & 1 deletion src/hooks/useProposalUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ export default function useProposalUpdate(updateId?: string | null) {
data: update,
isLoading: isLoadingUpdate,
isError: isErrorOnUpdate,
refetch: refetchUpdate,
} = useQuery({
queryKey: [`proposalUpdate#${updateId}`],
queryFn: async () => {
if (!updateId) {
return undefined
return null
}
return Governance.get().getProposalUpdate(updateId)
},
Expand All @@ -24,5 +25,6 @@ export default function useProposalUpdate(updateId?: string | null) {
update,
isLoadingUpdate,
isErrorOnUpdate,
refetchUpdate,
}
}
3 changes: 2 additions & 1 deletion src/hooks/useProposalUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from '@tanstack/react-query'

import { Governance } from '../clients/Governance'
import { UpdateResponse } from '../entities/Updates/types'

import { DEFAULT_QUERY_STALE_TIME } from './constants'

Expand All @@ -14,7 +15,7 @@ export default function useProposalUpdates(proposalId?: string | null) {
queryKey: [`proposalUpdates#${proposalId}`],
queryFn: async () => {
if (!proposalId) {
return undefined
return {} as UpdateResponse
}
return Governance.get().getProposalUpdates(proposalId)
},
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useProposalVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const useProposalVotes = (proposalId?: ProposalAttributes['id']) => {
queryKey: [`proposalVotes#${proposalId}`],
queryFn: async () => {
if (!proposalId) {
return undefined
return null
}
return Governance.get().getProposalVotes(proposalId)
},
Expand Down
16 changes: 13 additions & 3 deletions src/pages/proposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ export default function ProposalPage() {
const subscriptionsQueryKey = `subscriptions#${proposal?.id || ''}`
const { data: subscriptions, isLoading: isSubscriptionsLoading } = useQuery({
queryKey: [subscriptionsQueryKey],
queryFn: () => Governance.get().getSubscriptions(proposal!.id),
queryFn: () => {
if (proposal) {
return Governance.get().getSubscriptions(proposal.id)
}
return []
},
staleTime: DEFAULT_QUERY_STALE_TIME,
})
const { budgetWithContestants, isLoadingBudgetWithContestants } = useBudgetWithContestants(proposal?.id)
Expand Down Expand Up @@ -224,6 +229,7 @@ export default function ProposalPage() {
}
},
onSuccess: (updatedSubscriptions) => {
updatePageState({ confirmSubscription: false })
if (!proposal) return
queryClient.setQueryData([subscriptionsQueryKey], updatedSubscriptions)
},
Expand Down Expand Up @@ -263,8 +269,12 @@ export default function ProposalPage() {
}, [params])

useEffect(() => {
updatePageStateRef.current({ showUpdateSuccessModal: params.get('newUpdate') === 'true' })
}, [params])
const isNewUpdate = params.get('newUpdate') === 'true'
if (isNewUpdate) {
refetchUpdates()
}
updatePageStateRef.current({ showUpdateSuccessModal: isNewUpdate })
}, [params, refetchUpdates])

useEffect(() => {
if (proposalPageState.showVotingError) {
Expand Down
5 changes: 4 additions & 1 deletion src/pages/submit/grant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ export default function SubmitGrant() {
const params = new URLSearchParams(location.search)
let category: NewGrantCategory | null = null
try {
category = toNewGrantCategory(params.get('category'))
const categoryParam = params.get('category')
if (categoryParam) {
category = toNewGrantCategory(categoryParam)
}
} catch (error) {
console.error(error)
} finally {
Expand Down
4 changes: 2 additions & 2 deletions src/pages/submit/update.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default function Update({ isEdit }: Props) {
const updateId = params.get('id') || ''
const [isPreviewMode, setPreviewMode] = useState(false)
const [projectHealth, setProjectHealth] = useState(initialState.health)
const { update, isLoadingUpdate, isErrorOnUpdate } = useProposalUpdate(updateId)
const { update, isLoadingUpdate, isErrorOnUpdate, refetchUpdate } = useProposalUpdate(updateId)
const proposalId = useMemo(() => params.get('proposalId') || update?.proposal_id || '', [update])
const [isEditModalOpen, setIsEditModalOpen] = useState(false)
const [isEditAccepted, setIsEditAccepted] = useState(false)
Expand Down Expand Up @@ -217,7 +217,7 @@ export default function Update({ isEdit }: Props) {
} else {
await Governance.get().createProposalUpdate(newUpdate)
}

await refetchUpdate()
navigate(locations.proposal(proposalId, { newUpdate: 'true' }), { replace: true })
} catch (err) {
if (err instanceof Error) {
Expand Down

0 comments on commit 03ad98b

Please sign in to comment.