Skip to content

Commit

Permalink
gov: Add getDepositProps reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Hemanthghs committed Nov 25, 2023
1 parent a455900 commit 86d6de8
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 6 deletions.
99 changes: 96 additions & 3 deletions frontend/src/store/features/gov/govSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,28 @@ import { AxiosError } from 'axios';
import { ERR_UNKNOWN } from '@/utils/errors';
import { TxStatus } from '@/types/enums';
import {
ActiveProposal,
GovProposal,
GetProposalTallyInputs,
GetProposalsInDepositInputs,
GetProposalsInVotingInputs,
GetVotesInputs,
ProposalTallyData,
VotesData,
GovPagination,
} from '@/types/gov';

interface Chain {
active: {
status: TxStatus;
errMsg: string;
proposals: ActiveProposal[];
proposals: GovProposal[];
pagination?: GovPagination;
};
deposit: {
status: TxStatus;
errMsg: string;
proposals: GovProposal[];
pagination?: GovPagination;
};
votes: VotesData;
tally: ProposalTallyData;
Expand All @@ -34,13 +43,31 @@ interface GovState {
defaultState: Chain;
}

const EMPTY_PAGINATION = {
next_key: '',
total: '',
};

const initialState: GovState = {
chains: {},
defaultState: {
active: {
status: TxStatus.INIT,
errMsg: '',
proposals: [],
pagination: {
next_key: '',
total: '',
},
},
deposit: {
status: TxStatus.INIT,
errMsg: '',
proposals: [],
pagination: {
next_key: '',
total: '',
},
},
votes: {
status: TxStatus.INIT,
Expand All @@ -55,6 +82,23 @@ const initialState: GovState = {
},
};

export const getProposalsInDeposit = createAsyncThunk(
'gov/deposit-proposals',
async (data: GetProposalsInDepositInputs) => {
const response = await govService.proposals(
data.baseURL,
data?.key,
data?.limit,
1
);

return {
chainID: data.chainID,
data: response.data,
};
}
);

export const getProposalsInVoting = createAsyncThunk(
'gov/active-proposals',
async (data: GetProposalsInVotingInputs, { rejectWithValue, dispatch }) => {
Expand Down Expand Up @@ -144,14 +188,23 @@ export const govSlice = createSlice({
const chainID = action.meta?.arg?.chainID;
if (!state.chains[chainID])
state.chains[chainID] = cloneDeep(initialState.defaultState);
const chainProposals = state.chains[chainID].active.proposals || {};
const result = {
status: TxStatus.PENDING,
errMsg: '',
proposals: chainProposals,
pagination: EMPTY_PAGINATION,
};
state.chains[chainID].active = result;
})
.addCase(getProposalsInVoting.fulfilled, (state, action) => {
const chainID = action.payload?.chainID || '';
if (chainID.length > 0) {
if (chainID.length) {
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposals: action.payload?.data?.proposals,
pagination: action.payload?.data?.pagination,
};
state.chains[chainID].active = result;
}
Expand All @@ -163,10 +216,50 @@ export const govSlice = createSlice({
status: TxStatus.REJECTED,
errMsg: action.error.message || '',
proposals: chainProposals,
pagination: EMPTY_PAGINATION,
};
state.chains[chainID].active = result;
});

//proposals in deposit period
builder
.addCase(getProposalsInDeposit.pending, (state, action) => {
const chainID = action.meta?.arg?.chainID;
if (!state.chains[chainID])
state.chains[chainID] = cloneDeep(initialState.defaultState);
const chainProposals = state.chains[chainID].deposit.proposals || {};
const result = {
status: TxStatus.PENDING,
errMsg: '',
proposals: chainProposals,
pagination: EMPTY_PAGINATION,
};
state.chains[chainID].deposit = result;
})
.addCase(getProposalsInDeposit.fulfilled, (state, action) => {
const chainID = action.payload?.chainID || '';
if (chainID.length) {
const result = {
status: TxStatus.IDLE,
errMsg: '',
proposals: action.payload?.data?.proposals,
pagination: action.payload?.data?.pagination,
};
state.chains[chainID].deposit = result;
}
})
.addCase(getProposalsInDeposit.rejected, (state, action) => {
const chainID = action.meta?.arg?.chainID;
const chainProposals = state.chains[chainID].deposit.proposals || {};
const result = {
status: TxStatus.REJECTED,
errMsg: action.error.message || '',
proposals: chainProposals,
pagination: EMPTY_PAGINATION,
};
state.chains[chainID].deposit = result;
});

// votes
builder
.addCase(getVotes.pending, () => {})
Expand Down
13 changes: 10 additions & 3 deletions frontend/src/types/gov.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TxStatus } from "./enums";
import { TxStatus } from './enums';

interface ActiveProposal {
interface GovProposal {
proposal_id: string;
content: {
'@type': string;
Expand Down Expand Up @@ -30,7 +30,7 @@ interface ActiveProposal {
}

interface GovPagination {
next_key: string | undefined;
next_key?: string;
total: string;
}

Expand Down Expand Up @@ -96,6 +96,13 @@ interface GetProposalsInVotingInputs {
limit?: number;
}

interface GetProposalsInDepositInputs {
baseURL: string;
chainID: string;
key?: string;
limit?: number;
}

interface GetVotesInputs {
baseURL: string;
proposalId: number;
Expand Down

0 comments on commit 86d6de8

Please sign in to comment.