Skip to content

Commit

Permalink
Council menu debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Mar 27, 2024
1 parent 5349454 commit dd8655e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 48 deletions.
80 changes: 35 additions & 45 deletions fs-dapp_2.0/src/components/pages/Council.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useAccountContext } from '../../contexts/Account_Context';
import React, { useEffect,useState,useCallback } from 'react';
import { useAppContext } from '../../contexts/AppContext';
import { useConcilSessionContext } from '../../contexts/CouncilSessionContext';
import { DataType } from '@/src/contexts/types';
import BN from 'bn.js';
import { toUnit } from '../shared/utils';
import RolesApp from '../shared/modal';
Expand All @@ -13,19 +14,13 @@ import InfiniteScroll from "react-infinite-scroll-component";
import { Avatar, Divider, List, Skeleton } from "antd";
import { Button } from 'flowbite-react';

interface DataType {
name: string|undefined;
role: string;
address:string;
status:string;
}

export default function Council() {
const { api, blocks, selectedAccount,accounts, dispatch } = useAppContext();
const { role, balance, dispatch0 } = useAccountContext();
const { session_closed,approved,role_in_session,nay,ayes,council_members,selectedProposal,proposals, dispatch1 } = useConcilSessionContext();
const { session_closed,approved,role_in_session,nay,ayes,council_members,selectedProposal,proposals, datas,dispatch1 } = useConcilSessionContext();

const [loading, setLoading] = useState(false);
const [data, setData] = useState<DataType[]>([]);

const [hash0,setHash0] = useState<string[]>([]);

function querryProposals(hash:string) {
Expand All @@ -34,6 +29,7 @@ export default function Council() {

hash,(data_acc:any)=>{
if(!data_acc) return;
console.log(`datas:${data_acc}`);
let acc = data_acc.toPrimitive().args.account as InjectedAccountWithMeta;
let acc0 = data_acc.toPrimitive().args.account as string;
const acc1 = accounts.find((account) => account.address === acc0);
Expand All @@ -49,61 +45,60 @@ export default function Council() {

api.query.rolesModule.requestedRoles(acc1.address,(Prop_raw:any)=>{
let Prop = Prop_raw.toHuman();
console.log(`list of props: ${Prop.role.toString()}`)
if(!Prop) return;
let r_session = Prop.role.toString();
let status = Prop.approved.toString();
let dtype:DataType={name:acc1.meta.name,role:r_session,address:acc.address,status};
let tdata=data;
let referendum = Prop.sessionClosed.toString();
let hash = Prop.proposalHash.toString();
let dtype:DataType={name:acc1.meta.name,role:r_session,address:acc.address,status,referendum,hash};
let tdata=datas

tdata.push(dtype);
setData(tdata);
dispatch1({type:`SET_DATAS`,payload:tdata});
})

}
}
);
}

function clear_proposal(){

accounts.map((x)=>{
api?.query.rolesModule.requestedRoles(x.address,(Prop_raw:any)=>{
let Prop = Prop_raw.toHuman();
if (!Prop ||Prop.approved.toString()===`NO`||Prop.approved.toString()===`YES`){
let list = proposals;
list.filter((l)=>l!==x);
dispatch1({type:`SET_PROPOSALS`,payload:list});
}


})
})
}

/*function updateProposals(){
const update = useCallback(() =>{
let bb =hash0.map((x)=>querryProposals(x));
return bb
}*/
return bb},[hash0,blocks]);



//const update = useCallback(() =>updateProposals(),[hash0,updateProposals]);
const update = useCallback(() =>{
let bb =hash0.map((x)=>querryProposals(x));
return bb},[blocks]);

useEffect(() => {
if (!api || !selectedAccount) return;

api.query.backgroundCouncil.proposals((hash: string[]) => {
if (hash.length > 0) {

setHash0(hash);
update()
console.log(`Number of active proposals: ${proposals.length}`)
update()
let tdata0:DataType[]=[];
datas.forEach(elemnt=>{
if (hash0.includes(elemnt.hash) && !tdata0.includes(elemnt) && elemnt.status!==`AWAITING`){
tdata0.push(elemnt);
console.log(`Number of active proposals: ${tdata0.length}`)
}
if (hash0.includes(elemnt.hash) && !tdata0.includes(elemnt)){
tdata0.push(elemnt);
console.log(`Number of active proposals: ${tdata0.length}`)
}else{tdata0=datas}
})
dispatch1({type:`SET_DATAS`,payload:tdata0});


}
});


}, [hash0,blocks,selectedAccount,api]);
}, [hash0,selectedAccount,api]);



return(<div id="scrollableDiv"
style={{
Expand All @@ -114,7 +109,7 @@ export default function Council() {
}}>

<List
dataSource={data}
dataSource={datas}

renderItem={item => (
<Card
Expand All @@ -133,12 +128,7 @@ export default function Council() {
/>


<Button
type="primary"
className="bg-blue-600 text-white font-bold py-2 text-xl"
onClick={clear_proposal}>
clear Proposals List
</Button>

</div>);

}
13 changes: 10 additions & 3 deletions fs-dapp_2.0/src/contexts/CouncilSessionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, useContext, useReducer, ReactNode } from 'react';
import { CouncilSessionContextState } from './types';
import { CouncilSessionContextState,DataType } from './types';
import { InjectedAccountWithMeta } from '@polkadot/extension-inject/types';

const initialSession: CouncilSessionContextState = {
Expand All @@ -11,6 +11,7 @@ const initialSession: CouncilSessionContextState = {
ayes: 0,
nay: 0,
council_members: [],
datas:[]
};

type Action =
Expand All @@ -21,7 +22,9 @@ type Action =
| { type: 'SET_SESSION_CLOSE'; payload: boolean }
| { type: 'SET_AYES'; payload: number }
| { type: 'SET_NAY'; payload: number }
| { type: 'SET_COUNCIL_MEMBERS'; payload: InjectedAccountWithMeta[] };
| { type: 'SET_COUNCIL_MEMBERS'; payload: InjectedAccountWithMeta[] }
| { type: 'SET_DATAS'; payload: DataType[] };


function reducer(state: CouncilSessionContextState, action: Action): CouncilSessionContextState {
switch (action.type) {
Expand All @@ -41,6 +44,9 @@ function reducer(state: CouncilSessionContextState, action: Action): CouncilSess
return { ...state, nay: action.payload };
case 'SET_COUNCIL_MEMBERS':
return { ...state, council_members: action.payload };

case `SET_DATAS`:
return { ...state, datas: action.payload };
default:
return state;
}
Expand All @@ -57,7 +63,7 @@ type Props = {
children: ReactNode;
};
export function CouncilSessionProvider({ children }: Props) {
const [{ approved,selectedProposal,proposals, role_in_session, session_closed, ayes, nay, council_members }, dispatch1] =
const [{ approved,selectedProposal,proposals, role_in_session, session_closed, ayes, nay, council_members,datas }, dispatch1] =
useReducer(reducer, initialSession);
return (
<CouncilSessionContext.Provider
Expand All @@ -70,6 +76,7 @@ export function CouncilSessionProvider({ children }: Props) {
ayes,
nay,
council_members,
datas,
dispatch1,
}}
>
Expand Down
10 changes: 10 additions & 0 deletions fs-dapp_2.0/src/contexts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ export interface CouncilSessionContextState {
ayes: number;
nay: number;
council_members: InjectedAccountWithMeta[];
datas:DataType[]
}

export interface DataType {
name: string|undefined;
role: string;
address:string;
status:string;
referendum:string;
hash:string;
}

export const isRoleValid = (_role: string): boolean => {
Expand Down

0 comments on commit dd8655e

Please sign in to comment.