Skip to content
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

RSC: server cells lowercase data function #10015

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type Prisma from '@prisma/client'
import type { CellSuccessProps, CellFailureProps } from '@redwoodjs/web'

import { db } from 'api/src/lib/db'
Expand All @@ -9,7 +8,7 @@ interface DataArgs {
id: number
}

export const DATA = async ({ id }: DataArgs) => {
export const data = async ({ id }: DataArgs) => {
const userExample = await db.userExample.findUnique({
where: { id },
})
Expand All @@ -25,8 +24,8 @@ export const Failure = ({ error }: CellFailureProps) => (
<div className="rw-cell-error">{error?.message}</div>
)

export const Success = ({
userExample,
}: CellSuccessProps<{ userExample: Prisma.UserExample }>) => {
type SuccessProps = CellSuccessProps<Awaited<ReturnType<typeof data>>>

export const Success = ({ userExample }: SuccessProps) => {
return <UserExample userExample={userExample} />
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import type { PluginObj, types } from '@babel/core'
const EXPECTED_EXPORTS_FROM_CELL = [
'beforeQuery',
'QUERY',
'DATA',
'data',
'isEmpty',
'afterQuery',
'Loading',
Expand Down Expand Up @@ -122,14 +122,14 @@ export default function ({ types: t }: { types: typeof types }): PluginObj {
},
exit(path) {
const hasQueryOrDataExport =
exportNames.includes('QUERY') || exportNames.includes('DATA')
exportNames.includes('QUERY') || exportNames.includes('data')

// If the file already has a default export then
// 1. It's likely not a cell, or it's a cell that's already been
// wrapped in `createCell`
// 2. If we added another default export we'd be breaking JS module
// rules. There can only be one default export.
// If there's no QUERY or DATA export it's not a valid cell
// If there's no `QUERY` or `data` export it's not a valid cell
if (hasDefaultExport || !hasQueryOrDataExport) {
return
}
Expand Down
8 changes: 4 additions & 4 deletions packages/web/src/components/cell/createServerCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type CreateServerCellProps<CellProps, CellVariables> = Omit<
CreateCellProps<CellProps, CellVariables>,
'QUERY' | 'Failure'
> & {
DATA: (variables?: AnyObj) => any
data: (variables?: AnyObj) => any
Failure?: React.ComponentType<{
error: unknown
queryResult: { refetch: (variables: CellProps) => AnyObj }
Expand All @@ -30,7 +30,7 @@ export function createServerCell<
createCellProps: CreateServerCellProps<CellProps, CellVariables> // 👈 AnyObj, because using CellProps causes a TS error
): React.FC<CellProps> {
const {
DATA,
data: dataFn,
isEmpty = isDataEmpty,
Loading,
Failure,
Expand All @@ -51,15 +51,15 @@ export function createServerCell<
const queryResultWithRefetch = {
refetch: (variables: CellProps | undefined) => {
// TODO (RSC): How do we refresh the page with new data?
return DATA(variables)
return dataFn(variables)
},
}

return <Failure error={error} queryResult={queryResultWithRefetch} />
}

try {
const data = await DATA(variables)
const data = await dataFn(variables)

if (isEmpty(data, { isDataEmpty }) && Empty) {
return <Empty {...props} {...data} />
Expand Down
Loading