-
-
Notifications
You must be signed in to change notification settings - Fork 4
(SP: 3) [SHOP] complete phase 3 admin operations surface and admin UI polish #418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0a76233
(SP: 2) [SHOP] complete admin orders filtering, i18n fixes, and blog-…
liudmylasovetovs 986ba46
(SP: 2) [SHOP] polish admin products layout and compact table UI
liudmylasovetovs 4a1c1c5
(SP: 1) [SHOP] enrich admin order detail customer summary
liudmylasovetovs 7baca11
(SP: 1) [SHOP] add admin order detail audit timeline surface
liudmylasovetovs 7997501
(SP: 1) [SHOP] add controlled admin lifecycle actions to order detail
liudmylasovetovs e15753f
(SP: 1) [SHOP] polish admin order detail action surface and friendly …
liudmylasovetovs ab45a85
(SP: 1) [SHOP] Polish admin orders pagination and align admin product…
liudmylasovetovs 7e3454b
(SP: 3) [SHOP] close Phase 3 review follow-ups for admin order detail…
liudmylasovetovs f3bd4fd
(SP: 3) [SHOP] close follow-up review fixes for admin order lifecycle…
liudmylasovetovs 17e71ea
(SP: 1) [SHOP] fix admin order detail account snapshot and type lifec…
liudmylasovetovs 2c3f8d3
(SP: 1) [SHOP] fix admin order detail page polish and stabilize admin…
liudmylasovetovs dce3126
(SP: 1) [SHOP] fix lifecycle error alert contrast in admin order detail
liudmylasovetovs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
frontend/app/[locale]/admin/shop/orders/[id]/CancelPaymentButton.tsx
This file contains hidden or 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,129 @@ | ||
| 'use client'; | ||
|
|
||
| import { useRouter } from 'next/navigation'; | ||
| import { useTranslations } from 'next-intl'; | ||
| import { useId, useState, useTransition } from 'react'; | ||
|
|
||
| type Props = { | ||
| orderId: string; | ||
| disabled: boolean; | ||
| csrfToken: string; | ||
| }; | ||
|
|
||
| function normalizeActionErrorCode(error: unknown): string { | ||
| if (error instanceof TypeError) { | ||
| return 'NETWORK_ERROR'; | ||
| } | ||
|
|
||
| if (error instanceof Error && error.message.trim().length > 0) { | ||
| return error.message; | ||
| } | ||
|
|
||
| return 'NETWORK_ERROR'; | ||
| } | ||
|
|
||
| function mapCancelPaymentError( | ||
| code: string, | ||
| t: (key: string) => string | ||
| ): string { | ||
| switch (code) { | ||
| case 'NETWORK_ERROR': | ||
| return t('errors.network'); | ||
| case 'CSRF_REJECTED': | ||
| return t('errors.security'); | ||
| case 'CANCEL_DISABLED': | ||
| return t('errors.cancelPaymentDisabled'); | ||
| case 'CANCEL_PROVIDER_NOT_MONOBANK': | ||
| case 'CANCEL_NOT_ALLOWED': | ||
| return t('errors.cancelPaymentNotAvailable'); | ||
| case 'CANCEL_MISSING_PROVIDER_REF': | ||
| return t('errors.missingPaymentReference'); | ||
| case 'CANCEL_IN_PROGRESS': | ||
| return t('errors.cancelPaymentInProgress'); | ||
| case 'PSP_UNAVAILABLE': | ||
| return t('errors.providerUnavailable'); | ||
| case 'ADMIN_API_DISABLED': | ||
| return t('errors.adminDisabled'); | ||
| case 'INTERNAL_ERROR': | ||
| case 'HTTP_500': | ||
| return t('errors.generic'); | ||
| default: | ||
| return t('errors.generic'); | ||
| } | ||
| } | ||
|
|
||
| export function CancelPaymentButton({ orderId, disabled, csrfToken }: Props) { | ||
| const router = useRouter(); | ||
| const t = useTranslations('shop.orders.detail.paymentControls'); | ||
| const [isPending, startTransition] = useTransition(); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const errorId = useId(); | ||
|
|
||
| async function onCancelPayment() { | ||
| setError(null); | ||
|
|
||
| let res: Response; | ||
| try { | ||
| res = await fetch(`/api/shop/admin/orders/${orderId}/cancel-payment`, { | ||
| method: 'POST', | ||
| credentials: 'same-origin', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'x-csrf-token': csrfToken, | ||
| }, | ||
| }); | ||
| } catch (err) { | ||
| setError(mapCancelPaymentError(normalizeActionErrorCode(err), t)); | ||
| return; | ||
| } | ||
|
|
||
| let json: any = null; | ||
| try { | ||
| json = await res.json(); | ||
| } catch { | ||
| // ignore | ||
| } | ||
|
|
||
| if (!res.ok) { | ||
| setError( | ||
| mapCancelPaymentError( | ||
| json?.error ?? json?.code ?? json?.message ?? `HTTP_${res.status}`, | ||
| t | ||
| ) | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| startTransition(() => { | ||
| router.refresh(); | ||
| }); | ||
| } | ||
|
|
||
| const isDisabled = disabled || isPending; | ||
|
|
||
| return ( | ||
| <div className="space-y-2"> | ||
| <button | ||
| type="button" | ||
| onClick={onCancelPayment} | ||
| disabled={isDisabled} | ||
| aria-busy={isPending} | ||
| aria-describedby={error ? errorId : undefined} | ||
| className="w-full rounded-lg border border-sky-500/30 bg-sky-500/5 px-3 py-2 text-left text-sm font-medium text-sky-100 transition-colors hover:bg-sky-500/10 disabled:cursor-not-allowed disabled:opacity-50" | ||
| title={disabled ? t('onlyForUnpaidMonobank') : undefined} | ||
| > | ||
| {isPending ? t('cancelingPayment') : t('cancelUnpaidPayment')} | ||
| </button> | ||
|
|
||
| {error ? ( | ||
| <span | ||
| id={errorId} | ||
| role="alert" | ||
| className="block rounded-md border border-amber-500/20 bg-amber-500/5 px-3 py-2 text-xs text-amber-100" | ||
| > | ||
| {error} | ||
| </span> | ||
| ) : null} | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.