You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
...
{name: 'referrer',label: {zh: '推荐人',en: 'Referred By',},type: 'relationship',relationTo: 'referrers',admin: {description: 'The referrer of this transaction.',},},
...
Currently, I am trying to create a custom UI field that only display the number of referrals in the referrers listing and edit page.
components/RefereeCountCell.tsx
import{useConfig}from'payload/components/utilities';importtype{Props}from'payload/components/views/Cell';importtype{PaginatedDocs}from'payload/dist/mongoose/types';importtype{Transaction}from'payload/generated-types';importqsfrom'qs';importReact,{useEffect,useState}from'react';constRefereeCountCell=({rowData: { id }}: Props)=>{const[totalDocs,setTotalDocs]=useState(0);constconfig=useConfig();useEffect(()=>{constfetchTotalReferrals=async()=>{constquery={depth: 1,where: {referrer: {equals: id},},};constresp=awaitfetch(`${config.serverURL}/api/transactions${qs.stringify(query,{addQueryPrefix: true,})}`);constpaginatedDocs=(awaitresp.json())asPaginatedDocs<Transaction>;setTotalDocs(paginatedDocs.totalDocs);};fetchTotalReferrals();},[]);return<span>{totalDocs}</span>;};exportdefaultRefereeCountCell;
components/RefereeCountField.tsx
import{TextInput}from'payload/components/forms';import{useConfig,useDocumentInfo}from'payload/components/utilities';importtype{PaginatedDocs}from'payload/dist/mongoose/types';importtype{Transaction}from'payload/generated-types';importtype{UIField}from'payload/types';importqsfrom'qs';importReact,{useEffect,useState}from'react';constRefereeCountField=({ name }: UIField)=>{const[totalDocs,setTotalDocs]=useState(0);const{ id }=useDocumentInfo();constconfig=useConfig();useEffect(()=>{constfetchTotalReferrals=async()=>{constquery={depth: 1,where: {referrer: {equals: id},},};constresp=awaitfetch(`${config.serverURL}/api/transactions${qs.stringify(query,{addQueryPrefix: true,})}`);constpaginatedDocs=(awaitresp.json())asPaginatedDocs<Transaction>;setTotalDocs(paginatedDocs.totalDocs);};fetchTotalReferrals();},[]);return(<div><TextInputname={name}path={name}readOnlyvalue={`${totalDocs}`}description="Total referrals by this referrer."/></div>);};exportdefaultRefereeCountField;
So far, things seems to be working but I have a few questions:
Is this the recommended approach on building the Field / Cell component?
For me, the API looks a bit awkward especially where I need to import import type { PaginatedDocs } from 'payload/dist/mongoose/types'; , typing props with import type { Props } from 'payload/components/views/Cell';. I had to dig out the internals of Payload to inspect which type to use. I've also tried directly importing the Payload instance using the Local API but it does not work. If I understand it correctly, this is due to the Admin portal being in the client side but not server side rendered hence it cannot work.
It is also not that easy to reuse existing Payload's component. I've tried using TextInput component but I have no idea what the prop path is supposed to be.
Overall the DX is not as good as it seems when I tried to create custom UI component.
In the listing page, I cannot sort by the total number of referrers like how Payload's default cell does. (And I do not know how to implement it.)
Should we update the documentation to include some examples on this, maybe here as well?
Anyway, this is still an incredibly good CMS and I appreciate that. If there is better way of implementing this please let me know.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Greetings,
Thank you for writing up this amazing CMS.
Consider the following collections,
Whenever a new transaction is created, it can have an optional referrer field, e.g,
collections/Referrers.ts
colletions/Transaction.ts
Currently, I am trying to create a custom UI field that only display the number of referrals in the referrers listing and edit page.
components/RefereeCountCell.tsx
components/RefereeCountField.tsx
So far, things seems to be working but I have a few questions:
For me, the API looks a bit awkward especially where I need to import
import type { PaginatedDocs } from 'payload/dist/mongoose/types';
, typing props withimport type { Props } from 'payload/components/views/Cell';
. I had to dig out the internals of Payload to inspect which type to use. I've also tried directly importing the Payload instance using the Local API but it does not work. If I understand it correctly, this is due to the Admin portal being in the client side but not server side rendered hence it cannot work.Imagine I am able to do it like so:
It is also not that easy to reuse existing Payload's component. I've tried using
TextInput
component but I have no idea what the proppath
is supposed to be.Overall the DX is not as good as it seems when I tried to create custom UI component.
Anyway, this is still an incredibly good CMS and I appreciate that. If there is better way of implementing this please let me know.
Beta Was this translation helpful? Give feedback.
All reactions