-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Render the buy server action result as a promise
This is now possible because facebook/react#25634 got merged.
- Loading branch information
1 parent
dfa61e2
commit c64532c
Showing
4 changed files
with
45 additions
and
62 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
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,22 @@ | ||
import {clsx} from 'clsx'; | ||
import * as React from 'react'; | ||
|
||
export type NotificationProps = React.PropsWithChildren<{ | ||
readonly status: `success` | `error`; | ||
}>; | ||
|
||
export function Notification({ | ||
children, | ||
status, | ||
}: NotificationProps): JSX.Element { | ||
return ( | ||
<p | ||
className={clsx( | ||
`my-2`, | ||
status === `success` ? `text-cyan-600` : `text-red-600`, | ||
)} | ||
> | ||
{children} | ||
</p> | ||
); | ||
} |
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,30 +1,26 @@ | ||
'use server'; | ||
|
||
import * as React from 'react'; | ||
import {printInnerWidth} from '../client-functions/print-inner-width.js'; | ||
import {Notification} from '../components/shared/notification.js'; | ||
|
||
export interface BuyResult { | ||
readonly message: React.ReactNode; | ||
readonly printInnerWidth: () => void; | ||
} | ||
|
||
export async function buy(quantity: number): Promise<BuyResult> { | ||
export async function buy(quantity: number): Promise<React.ReactNode> { | ||
const itemOrItems = quantity === 1 ? `item` : `items`; | ||
|
||
try { | ||
await new Promise((resolve, reject) => | ||
setTimeout(Math.random() > 0.2 ? resolve : reject, 500), | ||
); | ||
|
||
return { | ||
message: ( | ||
<span> | ||
Bought <strong>{quantity}</strong> {itemOrItems}. | ||
</span> | ||
), | ||
printInnerWidth, | ||
}; | ||
return ( | ||
<Notification status="success"> | ||
Bought <strong>{quantity}</strong> {itemOrItems}. | ||
</Notification> | ||
); | ||
} catch { | ||
throw new Error(`Could not buy ${quantity} ${itemOrItems}, try again.`); | ||
return ( | ||
<Notification status="error"> | ||
Could not buy <strong>{quantity}</strong> {itemOrItems}, try again. | ||
</Notification> | ||
); | ||
} | ||
} |
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