Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
feat: add delete promotion button
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomontalbano committed Jan 17, 2024
1 parent 4591752 commit f544b69
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 27 deletions.
56 changes: 33 additions & 23 deletions packages/app/src/data/ruleBuilder/config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,45 @@ export function usePromotionRules(promotion: Promotion) {
data: ReturnType<typeof toFormLabels>
}>({ isLoading: true, data: null })
useEffect(() => {
promotion.promotion_rules?.forEach((promotionRule) => {
const formLabels = toFormLabels(promotionRule)
if (
promotion.promotion_rules == null ||
promotion.promotion_rules.length === 0
) {
setOutput({
isLoading: false,
data: null
})
} else {
promotion.promotion_rules.forEach((promotionRule) => {
const formLabels = toFormLabels(promotionRule)

const data =
formLabels?.flatMap(async (formLabel) => {
if (formLabel.rel == null) {
return formLabel
}
const data =
formLabels?.flatMap(async (formLabel) => {
if (formLabel.rel == null) {
return formLabel
}

const promise = sdkClient[formLabel.rel]
.list({
filters: { id_in: formLabel.value.split(',').join(',') }
})
.then((data) => data.map((d) => d.name))
.then((values) => ({
...formLabel,
value: values.join(',')
}))
const promise = sdkClient[formLabel.rel]
.list({
filters: { id_in: formLabel.value.split(',').join(',') }
})
.then((data) => data.map((d) => d.name))
.then((values) => ({
...formLabel,
value: values.join(',')
}))

return await promise
}) ?? []
return await promise
}) ?? []

void Promise.all(data).then((data) => {
setOutput({
isLoading: false,
data
void Promise.all(data).then((data) => {
setOutput({
isLoading: false,
data
})
})
})
})
}
}, [promotion])

return output
Expand Down
53 changes: 53 additions & 0 deletions packages/app/src/hooks/useDeleteOverlay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { Promotion } from '#data/dictionaries/promotion'
import { appRoutes } from '#data/routes'
import {
Button,
PageHeading,
useCoreSdkProvider,
useOverlay
} from '@commercelayer/app-elements'
import { useLocation } from 'wouter'

interface OverlayHook {
show: () => void
Overlay: React.FC<{ promotion: Promotion }>
}

export function useDeleteOverlay(): OverlayHook {
const { Overlay: OverlayElement, open, close } = useOverlay()
const { sdkClient } = useCoreSdkProvider()
const [, setLocation] = useLocation()

return {
show: open,
Overlay: ({ promotion }) => {
return (
<OverlayElement>
<PageHeading
title={`Confirm that you want to cancel the promotion ${promotion.name}`}
navigationButton={{
onClick: () => {
close()
},
label: 'Cancel',
icon: 'x'
}}
description='This action cannot be undone, proceed with caution.'
/>

<Button
variant='danger'
fullWidth
onClick={() => {
void sdkClient[promotion.type].delete(promotion.id).then(() => {
setLocation(appRoutes.home.makePath({}))
})
}}
>
Delete
</Button>
</OverlayElement>
)
}
}
}
23 changes: 19 additions & 4 deletions packages/app/src/pages/PromotionDetailsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Promotion } from '#data/dictionaries/promotion'
import { appRoutes } from '#data/routes'
import { getCurrencyCodes, usePromotionRules } from '#data/ruleBuilder/config'
import { usePromotionRules } from '#data/ruleBuilder/config'
import { useDeleteOverlay } from '#hooks/useDeleteOverlay'
import { usePromotion } from '#hooks/usePromotion'
import {
Badge,
Expand All @@ -20,6 +21,7 @@ import {
formatDateRange,
formatDateWithPredicate,
getPromotionDisplayStatus,
goBack,
useCoreSdkProvider,
useTokenProvider
} from '@commercelayer/app-elements'
Expand All @@ -38,7 +40,9 @@ function Page(
const { promotion, isLoading } = usePromotion(props.params.promotionId)
const { data: rules } = usePromotionRules(promotion)

console.log('available currency codes', getCurrencyCodes(promotion))
const { show: showDeleteOverlay, Overlay: DeleteOverlay } = useDeleteOverlay()

// console.log('available currency codes', getCurrencyCodes(promotion))

return (
<PageLayout
Expand All @@ -60,20 +64,31 @@ function Page(
})
)
}}
/>,
<DropdownItem
key='delete'
label='Delete'
onClick={() => {
showDeleteOverlay()
}}
/>
]}
/>
}
mode={mode}
gap='only-top'
navigationButton={{
label: 'All promotions',
label: 'Back',
onClick() {
setLocation(appRoutes.list.makePath({}))
goBack({
setLocation,
defaultRelativePath: appRoutes.home.makePath({})
})
}
}}
>
<SkeletonTemplate isLoading={isLoading}>
<DeleteOverlay promotion={promotion} />
<Spacer top='14'>
<CardStatus promotionId={props.params.promotionId} />
</Spacer>
Expand Down

0 comments on commit f544b69

Please sign in to comment.