From 56f06af4921b08926b3057a5c0d7faf1e0c3db48 Mon Sep 17 00:00:00 2001 From: chaojun Date: Wed, 4 Sep 2024 16:39:00 +0800 Subject: [PATCH] Show society member warning, #1034 --- .../src/features/society/getSocietyMember.js | 12 +++++++++ .../backend/src/features/society/index.js | 8 ++++++ backend/packages/backend/src/routes.js | 1 + next/components/postCreate/information.js | 24 ++++++++++++------ .../components/postCreate/societyMemberHit.js | 16 ++++++++++++ next/frontedUtils/strategy.js | 3 +++ next/hooks/useIsSocietyMember.js | 25 +++++++++++++++++++ 7 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 backend/packages/backend/src/features/society/getSocietyMember.js create mode 100644 backend/packages/backend/src/features/society/index.js create mode 100644 next/components/postCreate/societyMemberHit.js create mode 100644 next/frontedUtils/strategy.js create mode 100644 next/hooks/useIsSocietyMember.js diff --git a/backend/packages/backend/src/features/society/getSocietyMember.js b/backend/packages/backend/src/features/society/getSocietyMember.js new file mode 100644 index 00000000..d031c916 --- /dev/null +++ b/backend/packages/backend/src/features/society/getSocietyMember.js @@ -0,0 +1,12 @@ +const { + getSocietyMember: _getSocietyMember, +} = require("../../services/node.service/getSocietyMember"); + +async function getSocietyMember(ctx) { + const { network, address } = ctx.params; + ctx.body = await _getSocietyMember(network, address); +} + +module.exports = { + getSocietyMember, +}; diff --git a/backend/packages/backend/src/features/society/index.js b/backend/packages/backend/src/features/society/index.js new file mode 100644 index 00000000..d1b50992 --- /dev/null +++ b/backend/packages/backend/src/features/society/index.js @@ -0,0 +1,8 @@ +const Router = require("koa-router"); +const { getSocietyMember } = require("./getSocietyMember"); + +const router = new Router(); + +router.get("/:network/society/members/:address", getSocietyMember); + +module.exports = router; diff --git a/backend/packages/backend/src/routes.js b/backend/packages/backend/src/routes.js index 1f67d867..7ddae662 100644 --- a/backend/packages/backend/src/routes.js +++ b/backend/packages/backend/src/routes.js @@ -19,6 +19,7 @@ const commonFeatureRouters = [ require("./features/files/routes"), require("./features/notifications/routes"), require("./features/proposals/commonRoutes"), + require("./features/society/index"), ]; async function checkSpaceExisten(ctx, next) { diff --git a/next/components/postCreate/information.js b/next/components/postCreate/information.js index f32f34fb..9fb95b20 100644 --- a/next/components/postCreate/information.js +++ b/next/components/postCreate/information.js @@ -19,6 +19,8 @@ import { proxyBalanceLoadingSelector, } from "../../store/reducers/statusSlice"; import BalanceRow from "@/components/postCreate/BalanceRow"; +import { isOnePersonOnVoteOnly } from "frontedUtils/strategy"; +import SocietyMemberHit from "./societyMemberHit"; const Hint = styled.div` margin-top: 4px !important; @@ -34,7 +36,12 @@ const PostAddressWrapper = styled.div` `; export default function Information({ space }) { - const { proposeThreshold: threshold, decimals, symbol } = space; + const { + proposeThreshold: threshold, + decimals, + symbol, + weightStrategy, + } = space; const dispatch = useDispatch(); const balance = useSelector(targetBalanceSelector); @@ -87,13 +94,16 @@ export default function Information({ space }) { return ( <> - + {!isOnePersonOnVoteOnly(weightStrategy) && ( + + )} {hint} + {space.accessibility === "society" && } {proxyElements} ); diff --git a/next/components/postCreate/societyMemberHit.js b/next/components/postCreate/societyMemberHit.js new file mode 100644 index 00000000..ad5e9c3f --- /dev/null +++ b/next/components/postCreate/societyMemberHit.js @@ -0,0 +1,16 @@ +import { useIsSocietyMember } from "hooks/useIsSocietyMember"; +import styled from "styled-components"; + +const Hint = styled.div` + margin-top: 4px !important; + color: var(--textFeedbackError); +`; + +export default function SocietyMemberHit() { + const isSocietyMember = useIsSocietyMember(); + if (isSocietyMember) { + return null; + } + + return You are not the society member; +} diff --git a/next/frontedUtils/strategy.js b/next/frontedUtils/strategy.js new file mode 100644 index 00000000..b0856f7a --- /dev/null +++ b/next/frontedUtils/strategy.js @@ -0,0 +1,3 @@ +export function isOnePersonOnVoteOnly(strategies) { + return strategies.length === 1 && strategies[0].type !== "one-person-on-vote"; +} diff --git a/next/hooks/useIsSocietyMember.js b/next/hooks/useIsSocietyMember.js new file mode 100644 index 00000000..dc3c5e4f --- /dev/null +++ b/next/hooks/useIsSocietyMember.js @@ -0,0 +1,25 @@ +import { useEffect, useState } from "react"; +import { useSelector } from "react-redux"; +import nextApi from "services/nextApi"; +import { + loginAddressSelector, + loginNetworkSelector, +} from "store/reducers/accountSlice"; + +export function useIsSocietyMember() { + const network = useSelector(loginNetworkSelector); + const loginAddress = useSelector(loginAddressSelector); + const [isSocietyMember, setIsSocietyMember] = useState(false); + useEffect(() => { + if (!loginAddress || !network) { + return; + } + nextApi + .fetch(`${network.network}/society/members/${loginAddress}`) + .then(({ result }) => { + setIsSocietyMember(result?.data !== null); + }); + }, [network, loginAddress]); + + return isSocietyMember; +}