-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(subscribe): Add unsubscribe functionality with confirmation me…
…ssages and localized strings
- Loading branch information
1 parent
cc834ca
commit b2a2f42
Showing
30 changed files
with
423 additions
and
131 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// @ts-ignore | ||
|
||
// API 更新时间: | ||
// API 唯一标识: | ||
import * as announcement from './announcement'; | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use client'; | ||
|
||
import { preUnsubscribe, unsubscribe } from '@/services/user/user'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { Button } from '@workspace/ui/components/button'; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@workspace/ui/components/dialog'; | ||
import { useTranslations } from 'next-intl'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
import { toast } from 'sonner'; | ||
import { Display } from '../display'; | ||
|
||
interface UnsubscribeProps { | ||
id: number; | ||
allowDeduction?: boolean; | ||
} | ||
|
||
export default function Unsubscribe({ id, allowDeduction }: Readonly<UnsubscribeProps>) { | ||
const [open, setOpen] = useState(false); | ||
const router = useRouter(); | ||
const t = useTranslations('subscribe.unsubscribe'); | ||
|
||
const { data } = useQuery({ | ||
enabled: Boolean(open && id && allowDeduction), | ||
queryKey: ['preUnsubscribe', id], | ||
queryFn: async () => { | ||
const { data } = await preUnsubscribe({ id }); | ||
return data.data?.deduction_amount; | ||
}, | ||
}); | ||
|
||
const handleSubmit = async () => { | ||
try { | ||
await unsubscribe( | ||
{ id }, | ||
{ | ||
skipErrorHandler: true, | ||
}, | ||
); | ||
toast.success(t('success')); | ||
router.refresh(); | ||
setOpen(false); | ||
} catch (error) { | ||
toast.error(t('failed')); | ||
} | ||
}; | ||
|
||
if (!allowDeduction) return null; | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant='destructive' size='sm'> | ||
{t('unsubscribe')} | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>{t('confirmUnsubscribe')}</DialogTitle> | ||
<DialogDescription>{t('confirmUnsubscribeDescription')}</DialogDescription> | ||
</DialogHeader> | ||
<p>{t('availableDeductionAmount')}</p> | ||
<p className='text-primary text-2xl font-semibold'> | ||
<Display type='currency' value={data} /> | ||
</p> | ||
<p className='text-muted-foreground text-sm'>{t('deductionNote')}</p> | ||
<DialogFooter> | ||
<Button variant='outline' onClick={() => setOpen(false)}> | ||
{t('cancel')} | ||
</Button> | ||
<Button onClick={handleSubmit}>{t('confirm')}</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
} |
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
Oops, something went wrong.