Skip to content

Commit

Permalink
refactor: remove all double API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mbappai committed Sep 26, 2023
1 parent 5015baf commit 16fcf3b
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 125 deletions.
54 changes: 29 additions & 25 deletions components/BillingsPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ export default function BillingsView(){
const [selectedBank, setSelelectedOrg] = useState<any|Bank>({})
const [currentFilter, setCurrentStatus] = useState({id:'1',name: 'Verified'})

async function fetchAllBanks(){
const res = await axios({
method:'get',
//@ts-ignore
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank?pageNumber=${pageNumber}&pageSize=${pageSize}`,
headers:{
"Authorization": paseto
}
})

return res.data;
}
// async function fetchAllBanks(){
// const res = await axios({
// method:'get',
// //@ts-ignore
// url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank?pageNumber=${pageNumber}&pageSize=${pageSize}`,
// headers:{
// "Authorization": paseto
// }
// })

// return res.data;
// }
async function fetchBanks(){
const res = await axios({
method:'get',
//@ts-ignore
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank?key=org_id&value=${currentOrg.orgId}&pageNumber=${pageNumber}&pageSize=${pageSize}&key2=status&value2=${currentFilter.id}`,
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank?orgId=${currentOrg.orgId}&pageNumber=${pageNumber}&pageSize=${pageSize}&status=${currentFilter.id}`,
headers:{
"Authorization": paseto
}
Expand All @@ -77,8 +77,8 @@ export default function BillingsView(){
method:'patch',
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank`,
data:{
key:'status',
value: statusNumber, // 0 means de-activated in db
// key:'status',
status: statusNumber, // 0 means de-activated in db
id: id
},
headers:{
Expand Down Expand Up @@ -121,11 +121,11 @@ export default function BillingsView(){
}


const allBanksQuery = useQuery({queryKey:['all-banks'], queryFn:fetchAllBanks, enabled:paseto !== '', staleTime:Infinity})
const allBanksLength = allBanksQuery.data && allBanksQuery.data.data.length
// const allBanksQuery = useQuery({queryKey:['all-banks'], queryFn:fetchAllBanks, enabled:paseto !== '', staleTime:Infinity})
// const allBanksLength = allBanksQuery.data && allBanksQuery.data.data.length


const banksQuery = useQuery({queryKey:['banks', currentFilter], queryFn:fetchBanks, enabled:paseto !== '' && allBanksQuery.isFetched})
const banksQuery = useQuery({queryKey:['banks', currentFilter], queryFn:fetchBanks, enabled:paseto !== '' })
const data = banksQuery.data && banksQuery.data.data
const totalLength = banksQuery.data && banksQuery.data.dataLength;

Expand Down Expand Up @@ -257,7 +257,8 @@ export default function BillingsView(){
<div style={{display:'flex', marginTop:'1rem', marginBottom:'1rem', width:'100%', justifyContent:'space-between', alignItems:'center'}}>
<Title style={{ margin:'0'}} level={2}>Billings</Title>
</div>
{ allBanksQuery.data && allBanksLength == 0? null : <div style={{marginBottom:'1.5em', display:'flex', width:'100%', justifyContent:'space-between', alignItems:'center'}}>
{/* { allBanksQuery.data && allBanksLength == 0? null : */}
<div style={{marginBottom:'1.5em', display:'flex', width:'100%', justifyContent:'space-between', alignItems:'center'}}>
<Radio.Group defaultValue={currentFilter.id} buttonStyle="solid">
{bankFilters.map(bankFilter=>(
<Radio.Button key={bankFilter.id} onClick={()=>setCurrentStatus(bankFilter)} value={bankFilter.id}>{bankFilter.name}</Radio.Button>
Expand All @@ -268,13 +269,16 @@ export default function BillingsView(){
<Button shape="round" style={{marginRight:'1rem'}} loading={banksQuery.isRefetching} onClick={()=>banksQuery.refetch()} icon={<ReloadOutlined rev={undefined} />}>Refresh</Button>
<Button shape='round' type='primary' icon={<PlusOutlined rev={undefined}/>} onClick={()=>router.push('/organizations/billings/new')}>New Bank</Button>
</div>
</div>}
{
</div>
{/* } */}

{/* {
allBanksQuery.data && allBanksLength == 0
?<EmptyState>
<Button shape='round' type='primary' icon={<PlusOutlined rev={undefined}/>} onClick={()=>router.push('/organizations/billings/new')}>New Bank</Button>
</EmptyState>
: <Table
: */}
<Table
style={{width:'100%'}}
scroll={{ x: 'calc(500px + 50%)'}}
rowKey={(record)=>record.id}
Expand All @@ -289,7 +293,7 @@ export default function BillingsView(){
showTotal:(total) => `Total: ${total} items`,
}}
/>
}
{/* } */}

{
isDrawerOpen
Expand Down Expand Up @@ -360,8 +364,8 @@ const deleteDataHandler = async(record:Bank)=>{
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/org-bank`,
data: {
id:record.id,
key:'status',
value: "0"
// key:'status',
status: "0"
},
headers:{
"Authorization": paseto
Expand Down
8 changes: 4 additions & 4 deletions components/Manager/Organizations/EditOrg/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -537,8 +537,8 @@ interface EditableProp{
console.log(logoHash)

const payload = {
key:'logo_image_hash',
value: logoHash,
// key:'logo_image_hash',
logoImageHash: logoHash,
//@ts-ignore
id: selectedOrg.orgId
}
Expand Down Expand Up @@ -655,8 +655,8 @@ interface EditableProp{
console.log(coverImageHash)

const payload = {
key:'cover_image_hash',
value: coverImageHash,
// key:'cover_image_hash',
coverImageHash: coverImageHash,
id: selectedOrg.id
}
setUpdatedCoverImageHash(coverImageHash)
Expand Down
42 changes: 23 additions & 19 deletions pages/organizations/communities/communityVenues/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,18 @@ function CommunityVenues(){



async function fetchAllCommunityVenues(){
const res = await axios({
method:'get',
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/community-venues?communityId=${currentCommunity.id}&pageNumber=${pageNumber}&pageSize=${pageSize}&status=${currentFilter.id}`,
headers:{
"Authorization": paseto
}
})

return res.data;
// async function fetchAllCommunityVenues(){
// const res = await axios({
// method:'get',
// url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/community-venues?communityId=${currentCommunity.id}&pageNumber=${pageNumber}&pageSize=${pageSize}&status=${currentFilter.id}`,
// headers:{
// "Authorization": paseto
// }
// })

// return res.data;

}
// }
async function fetchCommunityVenues(){
const res = await axios({
method:'get',
Expand Down Expand Up @@ -111,8 +111,8 @@ function CommunityVenues(){
const servicesData = res && res.data
const totalLength = res && res.dataLength;

const allCommunityVenuesQuery = useQuery({queryKey:['all-communityVenues',{currentCommunity: currentCommunity.id}], queryFn:fetchAllCommunityVenues, enabled:paseto !== '', staleTime:Infinity})
const allCommunityVenuesLength = allCommunityVenuesQuery.data && allCommunityVenuesQuery.data.dataLength;
// const allCommunityVenuesQuery = useQuery({queryKey:['all-communityVenues',{currentCommunity: currentCommunity.id}], queryFn:fetchAllCommunityVenues, enabled:paseto !== '', staleTime:Infinity})
// const allCommunityVenuesLength = allCommunityVenuesQuery.data && allCommunityVenuesQuery.data.dataLength;



Expand Down Expand Up @@ -244,9 +244,10 @@ function CommunityVenues(){

return (
<div style={{background:'#f7f7f7', minHeight:'100vh'}}>
{ servicesData && allCommunityVenuesLength === 0
{/* { servicesData && allCommunityVenuesLength === 0
? null
: <div style={{marginBottom:'1.5em', display:'flex', width:'100%', flexDirection:'column'}}>
: */}
<div style={{marginBottom:'1.5em', display:'flex', width:'100%', flexDirection:'column'}}>
<div style={{width:'100%', marginBottom:'1rem', marginTop:'2rem', display:'flex', justifyContent:'space-between', alignItems:'center'}}>
<Title style={{margin: '0'}} level={2}>Community Venues</Title>
<div style={{display:'flex'}}>
Expand All @@ -266,13 +267,16 @@ function CommunityVenues(){
{/* <Dropdown.Button trigger={['click']} type="primary" icon={<PlusOutlined/>} menu={{ items, onClick: (item)=>onLaunchButtonClick(item) }}>Launch New ...</Dropdown.Button> */}
</div>

</div>}
{
</div>
{/* } */}

{/* {
servicesData && allCommunityVenuesLength === 0
?<EmptyState>
<Button type="primary" onClick={()=>{router.push('/organizations/communities/communityVenues/new')}} icon={<PlusOutlined rev={undefined}/>} >Add Venue</Button>
</EmptyState>
:<Table
: */}
<Table
style={{width:'100%'}}
scroll={{ x: 'calc(500px + 50%)'}}
rowKey={(record) => record.id}
Expand All @@ -287,7 +291,7 @@ function CommunityVenues(){
onChange={handleChange}
dataSource={servicesData}
/>
}
{/* } */}

{
isDrawerOpen
Expand Down
44 changes: 23 additions & 21 deletions pages/organizations/communities/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ function Communities(){
const urlPrefix = useUrlPrefix()


async function fetchAllCommunities(){
const res = await axios({
method:'get',
//@ts-ignore
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/community?orgId=${currentOrg.orgId}&pageNumber=${pageNumber}&pageSize=${pageSize}&status=${currentFilter.id}`,

headers:{
"Authorization": paseto
}
})

return res.data.data;
// async function fetchAllCommunities(){
// const res = await axios({
// method:'get',
// //@ts-ignore
// url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/community?orgId=${currentOrg.orgId}&pageNumber=${pageNumber}&pageSize=${pageSize}&status=${currentFilter.id}`,

// headers:{
// "Authorization": paseto
// }
// })

// return res.data.data;

}
// }

async function fetchCommunities(){
const res = await axios({
Expand Down Expand Up @@ -120,8 +120,8 @@ function Communities(){
const totalLength = 0;

// @ts-ignore
const allCommunitysQuery = useQuery({queryKey:['all-communities',{currentOrg: currentOrg.orgId}], queryFn:fetchAllCommunities, enabled: paseto !== '', staleTime:Infinity})
const allCommunitysLength = allCommunitysQuery.data && allCommunitysQuery.data.dataLength;
// const allCommunitysQuery = useQuery({queryKey:['all-communities',{currentOrg: currentOrg.orgId}], queryFn:fetchAllCommunities, enabled: paseto !== '', staleTime:Infinity})
// const allCommunitysLength = allCommunitysQuery.data && allCommunitysQuery.data.dataLength;



Expand Down Expand Up @@ -243,9 +243,10 @@ function gotoCommunityItemsPage(community:Community){
<div style={{display:'flex', marginTop:'1rem', marginBottom:'1rem', width:'100%', justifyContent:'space-between', alignItems:'center'}}>
<Title style={{ margin:'0'}} level={2}>Communities</Title>
</div>
{allCommunitysQuery.data && allCommunitysLength === 0
{/* {allCommunitysQuery.data && allCommunitysLength === 0
? null
: <div style={{marginBottom:'1.5em', display:'flex', width:'100%', flexDirection:'column'}}>
: */}
<div style={{marginBottom:'1.5em', display:'flex', width:'100%', flexDirection:'column'}}>
<div style={{width:'100%', marginBottom:'1rem', display:'flex', justifyContent:'space-between', alignItems:'center'}}>
{/* filters */}
<Radio.Group defaultValue={currentFilter.id} buttonStyle="solid">
Expand All @@ -265,14 +266,15 @@ function gotoCommunityItemsPage(community:Community){


</div>
}
{/* } */}

{
{/* {
allCommunitysQuery.data && allCommunitysLength === 0
? <EmptyState>
<Button type="primary" onClick={()=>router.push('/organizations/communities/new')} icon={<PlusOutlined rev={undefined}/>} >Launch Community</Button>
</EmptyState>
: <Table
: */}
<Table
style={{width:'100%'}}
scroll={{ x: 'calc(500px + 50%)'}}
size='large'
Expand All @@ -288,7 +290,7 @@ function gotoCommunityItemsPage(community:Community){
showTotal:(total) => `Total: ${total} items`,
}}
/>
}
{/* } */}

{
isDrawerOpen
Expand Down
37 changes: 19 additions & 18 deletions pages/organizations/communities/staff/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ function CommunityStaff(){

const urlPrefix = useUrlPrefix()

async function fetchAllStaff(){
const res = await axios({
method:'get',
url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/staff/community?communityId=${currentCommunity.id}&pageNumber=${pageNumber}&pageSize=10`,
headers:{
"Authorization": paseto
}
})

return res.data;
}
// async function fetchAllStaff(){
// const res = await axios({
// method:'get',
// url:`${process.env.NEXT_PUBLIC_NEW_API_URL}/${urlPrefix}/staff/community?communityId=${currentCommunity.id}&pageNumber=${pageNumber}&pageSize=10`,
// headers:{
// "Authorization": paseto
// }
// })

// return res.data;
// }
async function fetchStaff(){
const res = await axios({
method:'get',
Expand All @@ -80,8 +80,8 @@ function CommunityStaff(){
const data = staffQuery.data && staffQuery.data.data
const totalLength = staffQuery.data && staffQuery.data.dataLength;

const allStaffQuery = useQuery({queryKey:['all-community-staff'], queryFn:fetchAllStaff, enabled:paseto !== '', staleTime:Infinity})
const allStaffLength = allStaffQuery.data && allStaffQuery.data.dataLength
// const allStaffQuery = useQuery({queryKey:['all-community-staff'], queryFn:fetchAllStaff, enabled:paseto !== '', staleTime:Infinity})
// const allStaffLength = allStaffQuery.data && allStaffQuery.data.dataLength



Expand Down Expand Up @@ -188,7 +188,7 @@ function CommunityStaff(){

return (
<div>
{data && allStaffLength === 0 ? null :
{/* {data && allStaffLength === 0 ? null : */}
<div style={{marginBottom:'1.5em', marginTop:'2rem', display:'flex', width:'100%', flexDirection:'column'}}>
<Title style={{margin:'0', width:'100%'}} level={2}>Staff</Title>
<div style={{display:'flex', justifyContent:'space-between', width:'100%', alignItems:'center'}}>
Expand All @@ -215,12 +215,13 @@ function CommunityStaff(){


</div>
}
{/* } */}

{
{/* {
data && allStaffLength === 0
? <EmptyState onOpenForm={()=>setShowForm(true)}/>
: <Table
: */}
<Table
style={{width:'100%'}}
scroll={{ x: 'calc(500px + 50%)'}}
rowKey={(record)=>record.id}
Expand All @@ -235,7 +236,7 @@ function CommunityStaff(){
showTotal:(total) => `Total: ${total} items`,
}}
/>
}
{/* } */}


{
Expand Down
Loading

0 comments on commit 16fcf3b

Please sign in to comment.