Skip to content

Commit

Permalink
wip(gov): refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanthghs committed Nov 25, 2023
1 parent 6e3bfd0 commit c93a206
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
3 changes: 2 additions & 1 deletion frontend/src/store/features/gov/govService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Axios, { AxiosResponse } from 'axios';
import { convertPaginationToParams, cleanURL } from '../../../utils/util';
import { GetProposalsInVotingResponse, ProposalVote } from '@/types/gov';

const proposalsURL = '/cosmos/gov/v1beta1/proposals';
const proposalTallyURL = (id: string): string =>
Expand Down Expand Up @@ -32,7 +33,7 @@ const fetchProposalTally = (
baseURL: string,
proposalId: string
): Promise<AxiosResponse> => {
let uri = `${cleanURL(baseURL)}${proposalTallyURL(proposalId)}`;
const uri = `${cleanURL(baseURL)}${proposalTallyURL(proposalId)}`;
return Axios.get(uri);
};

Expand Down
33 changes: 25 additions & 8 deletions frontend/src/store/features/gov/govSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import { cloneDeep } from 'lodash';
import { AxiosError } from 'axios';
import { ERR_UNKNOWN } from '@/utils/errors';
import { TxStatus } from '@/types/enums';
import {
ActiveProposal,
GetProposalTallyInputs,
GetProposalsInVotingInputs,
GetVotesInputs,
ProposalTallyData,
VotesData,
} from '@/types/gov';

interface Chain {
active: {
status: string;
status: TxStatus;
errMsg: string;
proposals: ActiveProposal[];
};
Expand Down Expand Up @@ -140,23 +148,32 @@ export const govSlice = createSlice({
.addCase(getProposalsInVoting.fulfilled, (state, action) => {
const chainID = action.payload?.chainID || '';
if (chainID.length > 0) {
let result = {
status: 'idle',
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposals: action.payload?.data?.proposals,
};
state.chains[chainID].active = result;
}
})
.addCase(getProposalsInVoting.rejected, (state, action) => {});
.addCase(getProposalsInVoting.rejected, (state, action) => {
const chainID = action.meta?.arg?.chainID;
const chainProposals = state.chains[chainID].active.proposals || {};
const result = {
status: TxStatus.REJECTED,
errMsg: action.error.message || '',
proposals: chainProposals,
};
state.chains[chainID].active = result;
});

// votes
builder
.addCase(getVotes.pending, () => {})
.addCase(getVotes.fulfilled, (state, action) => {
const chainID = action.payload.chainID;
let result: VotesData = {
status: 'idle',
const result: VotesData = {
status: TxStatus.IDLE,
errMsg: '',
proposals: state.chains[chainID].votes?.proposals || {},
};
Expand All @@ -173,8 +190,8 @@ export const govSlice = createSlice({
.addCase(getProposalTally.pending, () => {})
.addCase(getProposalTally.fulfilled, (state, action) => {
const chainID = action.payload.chainID;
let result = {
status: 'idle',
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposalTally: state.chains[chainID].tally?.proposalTally || {},
};
Expand Down
12 changes: 4 additions & 8 deletions frontend/src/types/gov.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TxStatus } from "./enums";

interface ActiveProposal {
proposal_id: string;
content: {
Expand Down Expand Up @@ -27,12 +29,6 @@ interface ActiveProposal {
voting_end_time: string;
}

interface CosmosHubProposal {
status: string;
errMsg: string;
proposals: ActiveProposal[];
}

interface GovPagination {
next_key: string | undefined;
total: string;
Expand Down Expand Up @@ -60,7 +56,7 @@ interface ProposalVote {
}

interface VotesData {
status: string;
status: TxStatus;
errMsg: string;
proposals: {
[key: string]: ProposalVote;
Expand All @@ -87,7 +83,7 @@ interface GetProposalTallyResponse {
}

interface ProposalTallyData {
status: string;
status: TxStatus;
errMsg: string;
proposalTally: ProposalTally;
}
Expand Down

0 comments on commit c93a206

Please sign in to comment.