Skip to content

Commit

Permalink
Merge pull request #246 from chain4travel/aeddaqqa/fixes
Browse files Browse the repository at this point in the history
fix(partnerConfig): fix fetching partners on messenger and matchig partners
  • Loading branch information
aeddaqqa authored Sep 26, 2024
2 parents da09ebe + a044278 commit abe3b22
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 23 deletions.
10 changes: 6 additions & 4 deletions src/helpers/usePartnerConfig.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers } from 'ethers'
import { useCallback, useEffect } from 'react'
import { useAppDispatch } from '../hooks/reduxHooks'
import { useAppDispatch, useAppSelector } from '../hooks/reduxHooks'
import { getActiveNetwork } from '../redux/slices/network'
import { updateCMAcocuntContract } from '../redux/slices/partner'
import { useSmartContract } from './useSmartContract'

Expand All @@ -17,7 +18,8 @@ export const usePartnerConfig = () => {
CMAccountCreated,
accountReadContract,
} = useSmartContract()

const activeNetwork = useAppSelector(getActiveNetwork)
const auth = useAppSelector(state => state.appConfig.isAuth)
const dispatch = useAppDispatch()

async function CreateConfiguration(state) {
Expand Down Expand Up @@ -142,8 +144,8 @@ export const usePartnerConfig = () => {
}, [account, managerReadContract])

useEffect(() => {
if (wallet) isCMAccount()
}, [wallet])
if (wallet && auth) isCMAccount()
}, [wallet, activeNetwork])

const addServices = useCallback(
async services => {
Expand Down
17 changes: 15 additions & 2 deletions src/helpers/useSmartContract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useEffect,
useState,
} from 'react'
import { useNavigate } from 'react-router'
import store from 'wallet/store'
import {
CONTRACTCMACCOUNTMANAGERADDRESSCAMINO,
Expand Down Expand Up @@ -143,13 +144,25 @@ export const SmartContractProvider: React.FC<SmartContractProviderProps> = ({ ch
console.error('User denied account access:', error)
}
}

const navigate = useNavigate()
const path = window.location.pathname
useEffect(() => {
if (
activeNetwork.name.toLowerCase() === 'columbus' ||
activeNetwork.name.toLowerCase() === 'camino'
)
) {
setAccountReadContract(null)
setAccountWriteContract(null)
if (
contractCMAccountAddress &&
(path.includes('partners/messenger-configuration/supplier') ||
path.includes('partners/messenger-configuration/distribution') ||
path.includes('partners/messenger-configuration/bots'))
)
navigate('/partners/messenger-configuration/mydetails')
setContractCMAccountAddress('')
initializeEthers()
}
}, [activeNetwork, auth])

useEffect(() => {
Expand Down
16 changes: 7 additions & 9 deletions src/redux/services/partners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,7 @@ function getServiceName(fullName: unknown): string {

const getBaseUrl = () => {
const currentPath = typeof window !== 'undefined' ? window.location.hostname : ''
if (
currentPath === 'localhost' ||
currentPath.includes('dev') ||
currentPath.includes('stage')
) {
if (currentPath === 'localhost' || currentPath.includes('dev')) {
return BASE_URLS.dev
} else {
return BASE_URLS.prod
Expand All @@ -163,7 +159,7 @@ export const partnersApi = createApi({

let query = '?populate=*'

if (!isNaN(page)) {
if (!isNaN(page) && !onMessenger) {
query += `&sort[0]=companyName:asc&pagination[page]=${page}&pagination[pageSize]=12`
}
if (businessField) {
Expand All @@ -184,7 +180,7 @@ export const partnersApi = createApi({
}
// Only add the cChainAddress filter if onMessenger is explicitly true
if (onMessenger === true) {
query += `&filters[cChainAddress][$ne]=null`
query += `&sort[0]=companyName:asc&_limit=-1&filters[cChainAddress][$ne]=null`
}

return {
Expand Down Expand Up @@ -283,7 +279,9 @@ export const partnersApi = createApi({
...response.meta,
pagination: {
...response.meta.pagination,
// total: filteredPartners.length,
total: onMessenger
? filteredPartners.length
: response.meta.pagination.total,
},
}

Expand Down Expand Up @@ -389,7 +387,7 @@ export const partnersApi = createApi({
let query = '?populate=*'

if (!isNaN(page)) {
query += `&sort[0]=companyName:asc&pagination[page]=${page}&pagination[pageSize]=12`
query += `&sort[0]=companyName:asc&_limit=-1`
}
if (businessField) {
let filterWith = businessField
Expand Down
15 changes: 13 additions & 2 deletions src/views/partners/ConfigurDistrubitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@mui/material'
import { ethers } from 'ethers'
import React, { useEffect, useMemo, useReducer, useState } from 'react'
import { useParams } from 'react-router'
import { useNavigate, useParams } from 'react-router'
import MainButton from '../../components/MainButton'
import UpdatedSelectComponent from '../../components/Partners/UpdatedSelectComponent'
import {
Expand Down Expand Up @@ -151,13 +151,17 @@ export const BasicWantedServices = () => {
const { data: partner } = useFetchPartnerDataQuery({
companyName: partnerID,
})
const navigate = useNavigate()
useEffect(() => {
if (partner)
dispatchDistrubitorState({
type: actionTypes.UPDATE_WANTED_SERVICES,
payload: { wantedServices: partner.wantedServices, reset: true },
})
}, [partner])
if (!partner) return <></>

if (partner && !partner.contractAddress) navigate('/partners')
return (
<Box
sx={{
Expand All @@ -169,14 +173,21 @@ export const BasicWantedServices = () => {
>
<Configuration>
<Configuration.Title>Wanted Services</Configuration.Title>
<Configuration.Paragraphe>
This page lists all Wanted Services by this partner. A Wanted Service is a
service this partner is looking to buy.
{/* <br /> */}
{/* {supplierState.stepsConfig[1].services.length === 0 && (
<> No bot addresses are currently registered with this Messenger Account.</>
)} */}
</Configuration.Paragraphe>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '16px' }}>
<Configuration.Services
state={distrubitorState}
dispatch={dispatchDistrubitorState}
disabled={true}
/>
</Box>
<Divider />
</Configuration>
</Box>
)
Expand Down
12 changes: 10 additions & 2 deletions src/views/partners/ConfigurSupplier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '@mui/material'
import { ethers } from 'ethers'
import React, { useEffect, useMemo, useReducer, useState } from 'react'
import { useParams } from 'react-router'
import { useNavigate, useParams } from 'react-router'
import MainButton from '../../components/MainButton'
import UpdatedSelectComponent from '../../components/Partners/UpdatedSelectComponent'
import {
Expand Down Expand Up @@ -294,6 +294,7 @@ export const BasicSupportedServices = () => {
const { data: partner } = useFetchPartnerDataQuery({
companyName: partnerID,
})
const navigate = useNavigate()
useEffect(() => {
if (partner) {
dispatchSupplierState({
Expand All @@ -302,6 +303,9 @@ export const BasicSupportedServices = () => {
})
}
}, [partner])
if (!partner) return <></>

if (partner && !partner.contractAddress) navigate('/partners')
return (
<Box
sx={{
Expand All @@ -313,14 +317,18 @@ export const BasicSupportedServices = () => {
>
<Configuration>
<Configuration.Title>Offered Services</Configuration.Title>
<Configuration.Paragraphe>
This page lists all Offered Services by this partner. An Offered Service is a
service that this partner is selling, and all the bots run by this partner
support those services.
</Configuration.Paragraphe>
<Box sx={{ display: 'flex', flexDirection: 'column', gap: '16px' }}>
<Configuration.Services
disabled={true}
state={supplierState}
dispatch={dispatchSupplierState}
/>
</Box>
<Divider />
</Configuration>
</Box>
)
Expand Down
17 changes: 14 additions & 3 deletions src/views/partners/ManageBots.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, Button, CircularProgress, TextField, Typography } from '@mui/material'
import { ethers } from 'ethers'
import React, { useEffect, useState } from 'react'
import { useParams } from 'react-router'
import { useNavigate, useParams } from 'react-router'
import Alert from '../../components/Alert'
import { usePartnerConfig } from '../../helpers/usePartnerConfig'
import { useAppDispatch } from '../../hooks/reduxHooks'
Expand All @@ -14,6 +14,10 @@ export const BasicManageBots = () => {
const { data: partner } = useFetchPartnerDataQuery({
companyName: partnerID,
})
const navigate = useNavigate()
if (!partner) return <></>

if (partner && !partner?.contractAddress) navigate('/partners')
return (
<Box
sx={{
Expand All @@ -24,9 +28,16 @@ export const BasicManageBots = () => {
}}
>
<Configuration>
<Configuration.Title>Manage Bots</Configuration.Title>
<Configuration.Title>Bots</Configuration.Title>
<Configuration.Paragraphe>
List in this page the addresses of all bots using this Messenger Account.
This page lists all bot addresses registered to this Messenger Account.
{partner && partner.bots && partner?.bots?.length === 0 && (
<>
<br />
<br />
No bot addresses are currently registered with this Messenger Account.
</>
)}
</Configuration.Paragraphe>
{partner.bots &&
partner.bots.length > 0 &&
Expand Down
2 changes: 1 addition & 1 deletion src/views/settings/Links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function Links({ type = 'else', partner }: { type?: string; partn
else if (path.includes('bots')) setSecondValue(4)
else setSecondValue(0)
} else setValue(0)
dispatch(changeActiveApp('Network'))
if (!path.includes('partners')) dispatch(changeActiveApp('Network'))
}, [path]) // eslint-disable-line react-hooks/exhaustive-deps
const auth = useAppSelector(state => state.appConfig.isAuth)
const sc = useSmartContract()
Expand Down

0 comments on commit abe3b22

Please sign in to comment.