From 05ec10caf28f71e46d8f9ded9a9c973d519f1b62 Mon Sep 17 00:00:00 2001 From: Abishek Bashyal Date: Tue, 10 Oct 2023 00:00:10 +0545 Subject: [PATCH] feat: create group rest api call done --- .../sdk-frontend-react/src/app/app.tsx | 2 +- .../chat/CreateGroup/AddCriteria.tsx | 4 +- .../chat/CreateGroup/CreateGroupType.tsx | 48 +++++++++++-------- .../src/lib/components/chat/types/index.ts | 7 +++ .../src/lib/hooks/chat/useCreateGatedGroup.ts | 43 +++++++++++++++++ .../src/lib/hooks/chat/useCriteriaState.ts | 19 +++++--- .../src/lib/hooks/chat/usePushSendMessage.ts | 2 +- 7 files changed, 96 insertions(+), 29 deletions(-) create mode 100644 packages/uiweb/src/lib/hooks/chat/useCreateGatedGroup.ts diff --git a/packages/examples/sdk-frontend-react/src/app/app.tsx b/packages/examples/sdk-frontend-react/src/app/app.tsx index c4fc798cd..98a5bd2c7 100644 --- a/packages/examples/sdk-frontend-react/src/app/app.tsx +++ b/packages/examples/sdk-frontend-react/src/app/app.tsx @@ -313,7 +313,7 @@ export function App() { - + = [ { @@ -34,8 +36,6 @@ const GROUP_TYPE_OPTIONS: Array = [ }, ]; - - interface AddConditionProps { heading: string; subHeading: string; @@ -140,30 +140,38 @@ export const CreateGroupType = ({ criteriaStateManager }: ModalHeaderProps & GroupTypeState) => { const [checked, setChecked] = useState(true); - const [groupEncryptionType, setGroupEncryptionType] = useState(''); + const [groupEncryptionType, setGroupEncryptionType] = useState(GROUP_TYPE_OPTIONS[0].value); - // const theme = useContext(ThemeContext); - // const isMobile = useMediaQuery(device.mobileL); + const {createGatedGroup} = useCreateGatedGroup(); const groupInfoToast = useToast(); const createGroupService = async () => { - - const groupInfo = { - groupInfo: { ...groupInputDetails }, - groupType: groupEncryptionType, - }; - - const rules = criteriaStateManager.generateRule() - console.log("rules are", JSON.stringify(rules)); - console.log('created group with', groupInfo); + + // TODO:use actual data instead of dummy data + // const groupInfo = { + // groupInfo: { ...groupInputDetails }, + // groupType: groupEncryptionType, + // }; + const dummyGrouInfo:GroupInfoType = { + groupName: 'Push Group Chat 3', + groupDescription: 'This is the oficial group for Push Protocol', + groupImage:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KEg9Q6H9kovIHoCIVQJJCKE1ENFjnAgcaSGC6rEnxBwA04Tx43t2FnvDAfjkNibxgHxnWb2e/u992bee7tCa00YFsffekFY+nUzFtjW0LrvjRXrCDIAaPLlW0nHL0SsZtVoaF98mLrx3pdhOqLtYPHChahZcYYO7KvPFxvRl5XPp1sN3adWiD1ZAqD6XYK1b/dvE5IWryTt2udLFedwc1+9kLp+vbbpoDh+6TklxBeAi9TL0taeWpdmZzQDry0AcO+jQ12RyohqqoYoo8RDwJrU+qXkjWtfi8Xxt58BdQuwQs9qC/afLwCw8tnQbqYAPsgxE1S6F3EAIXux2oQFKm0ihMsOF71dHYx+f3NND68ghCu1YIoePPQN1pGRABkJ6Bus96CutRZMydTl+TvuiRW1m3n0eDl0vRPcEysqdXn+jsQPsrHMquGeXEaY4Yk4wxWcY5V/9scqOMOVUFthatyTy8QyqwZ+kDURKoMWxNKr2EeqVKcTNOajqKoBgOE28U4tdQl5p5bwCw7BWquaZSzAPlwjlithJtp3pTImSqQRrb2Z8PHGigD4RZuNX6JYj6wj7O4TFLbCO/Mn/m8R+h6rYSUb3ekokRY6f/YukArN979jcW+V/S8g0eT/N3VN3kTqWbQ428m9/8k0P/1aIhF36PccEl6EhOcAUCrXKZXXWS3XKd2vc/TRBG9O5ELC17MmWubD2nKhUKZa26Ba2+D3P+4/MNCFwg59oWVeYhkzgN/JDR8deKBoD7Y+ljEjGZ0sosXVTvbc6RHirr2reNy1OXd6pJsQ+gqjk8VWFYmHrwBzW/n+uMPFiRwHB2I7ih8ciHFxIkd/3Omk5tCDV1t+2nNu5sxxpDFNx+huNhVT3/zMDz8usXC3ddaHBj1GHj/As08fwTS7Kt1HBTmyN29vdwAw+/wbwLVOJ3uAD1wi/dUH7Qei66PfyuRj4Ik9is+hglfbkbfR3cnZm7chlUWLdwmprtCohX4HUtlOcQjLYCu+fzGJH2QRKvP3UNz8bWk1qMxjGTOMThZ3kvgLI5AzFfo379UAAAAASUVORK5CYII=', + isPublic: true, + } + + const rules:any = criteriaStateManager.generateRule() + await createGatedGroup(dummyGrouInfo, rules) + alert("group created") + console.log('created group with'); onClose(); }; const verifyAndCreateGroup = async () => { - if (groupEncryptionType.trim() === '') { - showError('Group encryption type is not selected'); - return; - } + // TODO:validate the fields + // if (groupEncryptionType.trim() === '') { + // showError('Group encryption type is not selected'); + // return; + // } await createGroupService(); }; diff --git a/packages/uiweb/src/lib/components/chat/types/index.ts b/packages/uiweb/src/lib/components/chat/types/index.ts index 7f62d7541..f5de3f8cc 100644 --- a/packages/uiweb/src/lib/components/chat/types/index.ts +++ b/packages/uiweb/src/lib/components/chat/types/index.ts @@ -1,5 +1,12 @@ import { DropdownValueType } from '../reusables'; +export interface GrouInfoType{ + groupName: string; + groupDescription:string; + groupImage:string; + isPublic: boolean; +} + export const TYPE = { PUSH: 'PUSH', GUILD: 'GUILD', diff --git a/packages/uiweb/src/lib/hooks/chat/useCreateGatedGroup.ts b/packages/uiweb/src/lib/hooks/chat/useCreateGatedGroup.ts new file mode 100644 index 000000000..ea539842b --- /dev/null +++ b/packages/uiweb/src/lib/hooks/chat/useCreateGatedGroup.ts @@ -0,0 +1,43 @@ +import { useCallback, useState } from 'react'; +import { useChatData } from './useChatData'; +import * as PushAPI from '@pushprotocol/restapi'; +import { GrouInfoType } from '../../components/chat/types'; + +export const useCreateGatedGroup = () => { + const [loading, setLoading] = useState(false); + const [error, setError] = useState(); + const { pgpPrivateKey, env, account } = useChatData(); + + const createGatedGroup = useCallback( + async (groupInfoType:GrouInfoType,rules: any) => { + setLoading(true); + try { + const payload = { + groupName: groupInfoType.groupName, + groupDescription:groupInfoType.groupDescription, + groupImage:groupInfoType.groupImage, + isPublic: groupInfoType.isPublic, + members: [], + admins: [], + account: account || '', + env: env, + pgpPrivateKey: pgpPrivateKey, + rules: rules, + }; + const response = await PushAPI.chat.createGroup(payload); + setLoading(false); + if (!response) { + return false; + } + } catch (error: Error | any) { + setLoading(false); + setError(error.message); + console.log(error); + return error.message; + } + }, + [pgpPrivateKey, account, env] + ); + + return { createGatedGroup, error, loading }; +}; diff --git a/packages/uiweb/src/lib/hooks/chat/useCriteriaState.ts b/packages/uiweb/src/lib/hooks/chat/useCriteriaState.ts index 6c06430f3..e677f4251 100644 --- a/packages/uiweb/src/lib/hooks/chat/useCriteriaState.ts +++ b/packages/uiweb/src/lib/hooks/chat/useCriteriaState.ts @@ -182,6 +182,13 @@ export const useCriteriaStateManager = (): CriteriaStateManagerType => { rules: Rule[][], conditionTypes: ConditionType[] ): Condition[] => { + if(rules.length === 0){ + return [] + } + + console.log(`Generating for ${JSON.stringify(rules)}`) + console.log("condition type",conditionTypes); + return conditionTypes.map((el, idx) => ({ [el]: rules[idx].map((_el) => _el), })) as any; @@ -191,17 +198,17 @@ export const useCriteriaStateManager = (): CriteriaStateManagerType => { return { entry: { conditions: { - [chatCriteria.entryRootCondition]: _generate( - chatCriteria.entryOptionsDataArray, - chatCriteria.entryOptionTypeArray + [entryCriteria.entryRootCondition]: _generate( + entryCriteria.entryOptionsDataArray, + entryCriteria.entryOptionTypeArray ), }, }, chat: { conditions: { - [entryCriteria.entryRootCondition]: _generate( - entryCriteria.entryOptionsDataArray, - entryCriteria.entryOptionTypeArray + [chatCriteria.entryRootCondition]: _generate( + chatCriteria.entryOptionsDataArray, + chatCriteria.entryOptionTypeArray ), }, }, diff --git a/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts b/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts index d19f692d3..4c4ff6173 100644 --- a/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts +++ b/packages/uiweb/src/lib/hooks/chat/usePushSendMessage.ts @@ -11,7 +11,7 @@ interface SendMessageParams { messageType?: 'Text' | 'Image' | 'File' | 'GIF' | 'MediaEmbed'; } -const usePushSendMessage = () => { +const usePushSendMessage = () => { const [error, setError] = useState(); const [loading, setLoading] = useState(false);