-
Notifications
You must be signed in to change notification settings - Fork 339
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
135 additions
and
13 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
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,95 @@ | ||
import { useRouter } from 'next/router'; | ||
import * as R from 'ramda'; | ||
import { useCallback, useState } from 'react'; | ||
import { ProposalDetailsQuery, useProposalDetailsQuery } from '@/graphql/types/general_types'; | ||
import type { ProposalState } from './types'; | ||
|
||
// ========================= | ||
// overview | ||
// ========================= | ||
const formatOverview = (data: ProposalDetailsQuery) => { | ||
const DEFAULT_TIME = '0001-01-01T00:00:00'; | ||
let votingStartTime = data?.proposal?.[0]?.votingStartTime ?? DEFAULT_TIME; | ||
votingStartTime = votingStartTime === DEFAULT_TIME ? '' : votingStartTime; | ||
let votingEndTime = data?.proposal?.[0]?.votingEndTime ?? DEFAULT_TIME; | ||
votingEndTime = votingEndTime === DEFAULT_TIME ? '' : votingEndTime; | ||
|
||
const overview = { | ||
proposer: data?.proposal?.[0]?.proposer ?? '', | ||
content: data?.proposal?.[0]?.content ?? '', | ||
title: data?.proposal?.[0]?.title ?? '', | ||
id: data?.proposal?.[0]?.proposalId ?? '', | ||
description: data?.proposal?.[0]?.description ?? '', | ||
status: data?.proposal?.[0]?.status ?? '', | ||
submitTime: data?.proposal?.[0]?.submitTime ?? '', | ||
depositEndTime: data?.proposal?.[0]?.depositEndTime ?? '', | ||
votingStartTime, | ||
votingEndTime, | ||
}; | ||
|
||
return overview; | ||
}; | ||
|
||
// ========================== | ||
// parsers | ||
// ========================== | ||
const formatProposalQuery = (data: ProposalDetailsQuery) => { | ||
const stateChange: Partial<ProposalState> = { | ||
loading: false, | ||
}; | ||
|
||
if (!data.proposal.length) { | ||
stateChange.exists = false; | ||
return stateChange; | ||
} | ||
|
||
stateChange.overview = formatOverview(data); | ||
|
||
return stateChange; | ||
}; | ||
|
||
export const useProposalDetails = () => { | ||
const router = useRouter(); | ||
const [state, setState] = useState<ProposalState>({ | ||
loading: true, | ||
exists: true, | ||
overview: { | ||
proposer: '', | ||
content: { | ||
recipient: '', | ||
amount: [], | ||
}, | ||
title: '', | ||
id: 0, | ||
description: '', | ||
status: '', | ||
submitTime: '', | ||
depositEndTime: '', | ||
votingStartTime: '', | ||
votingEndTime: '', | ||
}, | ||
}); | ||
|
||
const handleSetState = useCallback((stateChange: (prevState: ProposalState) => ProposalState) => { | ||
setState((prevState) => { | ||
const newState = stateChange(prevState); | ||
return R.equals(prevState, newState) ? prevState : newState; | ||
}); | ||
}, []); | ||
|
||
// ========================== | ||
// fetch data | ||
// ========================== | ||
useProposalDetailsQuery({ | ||
variables: { | ||
proposalId: parseFloat((router?.query?.id as string) ?? '0'), | ||
}, | ||
onCompleted: (data) => { | ||
handleSetState((prevState) => ({ ...prevState, ...formatProposalQuery(data) })); | ||
}, | ||
}); | ||
|
||
return { | ||
state, | ||
}; | ||
}; |
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,24 @@ | ||
export interface OverviewType { | ||
title: string; | ||
id: number; | ||
proposer: string; | ||
description: string; | ||
status: string; | ||
submitTime: string; | ||
depositEndTime: string; | ||
votingStartTime: string | null; | ||
votingEndTime: string | null; | ||
content: { | ||
recipient: string; | ||
amount: Array<{ | ||
amount: string; | ||
denom: string; | ||
}>; | ||
}; | ||
} | ||
|
||
export interface ProposalState { | ||
loading: boolean; | ||
exists: boolean; | ||
overview: OverviewType; | ||
} |