-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pci-billing): add history billing section
ref: DTCORE-2797 Signed-off-by: LIDRISSI Hamid <[email protected]>
- Loading branch information
1 parent
89b32a0
commit 4182a9e
Showing
22 changed files
with
443 additions
and
90 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { v6 } from '@ovh-ux/manager-core-api'; | ||
import { TCurrentUsage } from './consumption'; | ||
|
||
export type TUsageHistoryPeriod = { | ||
id: string; | ||
lastUpdate: string; | ||
period: { | ||
from: string; | ||
to: string; | ||
}; | ||
}; | ||
|
||
export const getUsageHistoryPeriod = async ( | ||
projectId: string, | ||
from: string, | ||
to: string, | ||
): Promise<TUsageHistoryPeriod[]> => { | ||
const { data } = await v6.get<TUsageHistoryPeriod[]>( | ||
`/cloud/project/${projectId}/usage/history?from=${from}&to=${to}`, | ||
); | ||
|
||
return data; | ||
}; | ||
|
||
export type THistoryUsage = TCurrentUsage; | ||
|
||
export const getUsageHistory = async ( | ||
projectId: string, | ||
usageId: string, | ||
): Promise<THistoryUsage> => { | ||
const { data } = await v6.get<THistoryUsage>( | ||
`/cloud/project/${projectId}/usage/history/${usageId}`, | ||
); | ||
|
||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
packages/manager/apps/pci-billing/src/api/hook/useHistory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { isSameMonth } from 'date-fns'; | ||
import { useComputeDate } from '@/components/history/useComputeDate.hook'; | ||
import { getCurrentUsage } from '../data/consumption'; | ||
import { | ||
getUsageHistory, | ||
getUsageHistoryPeriod, | ||
TUsageHistoryPeriod, | ||
} from '../data/history'; | ||
import { getConsumptionDetails } from './useConsumption'; | ||
|
||
export const useUsageHistoryPeriod = ( | ||
projectId: string, | ||
from: string, | ||
to: string, | ||
) => | ||
useQuery({ | ||
queryKey: [projectId, from, to, 'history', 'usage'], | ||
queryFn: async () => { | ||
const historyPeriod = await getUsageHistoryPeriod(projectId, from, to); | ||
return historyPeriod?.length | ||
? historyPeriod[0] | ||
: ({} as TUsageHistoryPeriod); | ||
}, | ||
}); | ||
|
||
export const useGetUsageHistory = ( | ||
projectId: string, | ||
periodDetail: TUsageHistoryPeriod, | ||
) => { | ||
const { billingDate } = useComputeDate(); | ||
|
||
return useQuery({ | ||
queryKey: [projectId, periodDetail?.id, 'history', 'usage'], | ||
queryFn: async () => { | ||
const historyUsage = await getUsageHistory(projectId, periodDetail?.id); | ||
let monthlyDetails; | ||
|
||
if (isSameMonth(new Date(), billingDate)) { | ||
const currentUsage = await getCurrentUsage(projectId); | ||
monthlyDetails = currentUsage?.monthlyUsage; | ||
} else if ( | ||
new Date(periodDetail.period.to).getUTCMonth() === | ||
billingDate.getMonth() | ||
) { | ||
monthlyDetails = historyUsage?.monthlyUsage; | ||
} | ||
|
||
return { ...historyUsage, monthlyUsage: monthlyDetails }; | ||
}, | ||
enabled: !!periodDetail?.id, | ||
select: (data) => getConsumptionDetails(data), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/manager/apps/pci-billing/src/components/history/HistoryHeader.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { ODS_THEME_COLOR_INTENT } from '@ovhcloud/ods-common-theming'; | ||
import { | ||
ODS_BUTTON_SIZE, | ||
ODS_BUTTON_TYPE, | ||
ODS_BUTTON_VARIANT, | ||
ODS_ICON_NAME, | ||
ODS_ICON_SIZE, | ||
ODS_TEXT_LEVEL, | ||
ODS_TEXT_SIZE, | ||
} from '@ovhcloud/ods-components'; | ||
import { OsdsButton, OsdsIcon, OsdsText } from '@ovhcloud/ods-components/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { isSameDay, startOfMonth } from 'date-fns'; | ||
import { useComputeDate } from './useComputeDate.hook'; | ||
|
||
export default function HistoryHeader() { | ||
const { t } = useTranslation('history'); | ||
|
||
const { | ||
billingDate, | ||
prevMonthDate, | ||
nextMonthDate, | ||
translationValues, | ||
} = useComputeDate(); | ||
|
||
const isLimitReached = isSameDay(billingDate, startOfMonth(new Date())); | ||
|
||
const navigate = useNavigate(); | ||
const navigateToMonth = (date: Date) => { | ||
navigate(`../history/${date.getFullYear()}/${date.getMonth() + 1}`); | ||
}; | ||
|
||
return ( | ||
<div className="mb-5 flex justify-between items-center"> | ||
<OsdsButton | ||
color={ODS_THEME_COLOR_INTENT.primary} | ||
type={ODS_BUTTON_TYPE.button} | ||
size={ODS_BUTTON_SIZE.sm} | ||
variant={ODS_BUTTON_VARIANT.ghost} | ||
onClick={() => navigateToMonth(prevMonthDate)} | ||
className="flex" | ||
> | ||
<OsdsIcon | ||
name={ODS_ICON_NAME.CHEVRON_LEFT} | ||
size={ODS_ICON_SIZE.xxs} | ||
color={ODS_THEME_COLOR_INTENT.primary} | ||
className="mr-4" | ||
/> | ||
{t('cpbh_previous_month')} | ||
</OsdsButton> | ||
|
||
<OsdsText | ||
size={ODS_TEXT_SIZE._500} | ||
level={ODS_TEXT_LEVEL.heading} | ||
color={ODS_THEME_COLOR_INTENT.text} | ||
slot="summary" | ||
> | ||
{t('cpbh_current_month', { ...translationValues })} | ||
</OsdsText> | ||
|
||
<OsdsButton | ||
color={ODS_THEME_COLOR_INTENT.primary} | ||
type={ODS_BUTTON_TYPE.button} | ||
size={ODS_BUTTON_SIZE.sm} | ||
variant={ODS_BUTTON_VARIANT.ghost} | ||
onClick={() => navigateToMonth(nextMonthDate)} | ||
disabled={isLimitReached || undefined} | ||
className="flex" | ||
> | ||
{t('cpbh_next_month')} | ||
<OsdsIcon | ||
name={ODS_ICON_NAME.CHEVRON_RIGHT} | ||
size={ODS_ICON_SIZE.xxs} | ||
color={ODS_THEME_COLOR_INTENT.primary} | ||
className="ml-4" | ||
/> | ||
</OsdsButton> | ||
</div> | ||
); | ||
} |
67 changes: 0 additions & 67 deletions
67
packages/manager/apps/pci-billing/src/components/history/HistoryLinks.component.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.