Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
chore: lints DynamicPriceSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsfletch committed May 16, 2023
1 parent 696db44 commit ff02472
Showing 1 changed file with 25 additions and 44 deletions.
69 changes: 25 additions & 44 deletions src/collections/Forms/DynamicPriceSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@
'use client'

import React, { useEffect, useState } from 'react';
import { Text, useWatchForm } from 'payload/components/forms';
import { Props as TextFieldType } from 'payload/dist/admin/components/forms/field-types/Text/types';
import { Data } from 'payload/dist/admin/components/forms/Form/types';
import { useLocale } from "payload/components/utilities";
import React, { useEffect, useState } from 'react'
import { Text, useWatchForm } from 'payload/components/forms'
import { useLocale } from 'payload/components/utilities'
import { Props as TextFieldType } from 'payload/dist/admin/components/forms/field-types/Text/types'
import { Data } from 'payload/dist/admin/components/forms/Form/types'

type FieldWithID = {
id: string
name: string
};
}

export const DynamicPriceSelector: React.FC<TextFieldType> = (props) => {
const {
path,
label
} = props;
export const DynamicPriceSelector: React.FC<TextFieldType> = props => {
const { path, label } = props

const {
fields,
getDataByPath,
getData
} = useWatchForm();
const { fields, getDataByPath, getData } = useWatchForm()

const locale = useLocale();
const locale = useLocale()

const [isNumberField, setIsNumberField] = useState<boolean>();
const [valueType, setValueType] = useState<'static' | 'valueOfField'>();
const [isNumberField, setIsNumberField] = useState<boolean>()
const [valueType, setValueType] = useState<'static' | 'valueOfField'>()

// only number fields can use 'valueOfField`
useEffect(() => {
if (path) {
const parentPath = path.split('.').slice(0, -1).join('.')
const paymentFieldData: any = getDataByPath(parentPath);
const paymentFieldData: any = getDataByPath(parentPath)

if (paymentFieldData) {
const {
fieldToUse,
valueType
} = paymentFieldData;
const { fieldToUse, valueType } = paymentFieldData

setValueType(valueType);
setValueType(valueType)

const { fields: allFields }: Data = getData();
const field = allFields.find((field: FieldWithID) => field.name === fieldToUse);
const { fields: allFields }: Data = getData()
const field = allFields.find((field: FieldWithID) => field.name === fieldToUse)

if (field) {
const { blockType } = field;
setIsNumberField(blockType === 'number');
const { blockType } = field
setIsNumberField(blockType === 'number')
}
}
}
}, [
fields,
path,
getDataByPath,
getData
]);
}, [fields, path, getDataByPath, getData])

// TODO: make this a number field, block by Payload
if (valueType === 'static') {
return (
<Text {...props} />
)
return <Text {...props} />
}

const localLabels = typeof label === 'object' ? label : { [locale]: label };
const labelValue = localLabels[locale] || localLabels['en'] || '';
const localLabels = typeof label === 'object' ? label : { [locale]: label }
const labelValue = localLabels[locale] || localLabels['en'] || ''

if (valueType === 'valueOfField' && !isNumberField) {
return (
<div>
<div>
{labelValue}
</div>
<div>{labelValue}</div>
<div
style={{
color: '#9A9A9A',
Expand All @@ -86,4 +67,4 @@ export const DynamicPriceSelector: React.FC<TextFieldType> = (props) => {
}

return null
};
}

0 comments on commit ff02472

Please sign in to comment.