Skip to content

Commit

Permalink
Merge branch 'refs/heads/refactor/refactor-base' into refactor/fix/12…
Browse files Browse the repository at this point in the history
…58-1259-1260-1261

# Conflicts:
#	src/renderer/components/blocks/forms/UnitForm.tsx
#	src/renderer/components/blocks/modals/UpsertUnitModal.tsx
  • Loading branch information
wwills2 committed May 23, 2024
2 parents 934d83b + 1c9c801 commit 4683aad
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ dist-ssr
.contentlayer

.package-lock.json

.history
85 changes: 64 additions & 21 deletions src/renderer/components/blocks/forms/UnitForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { forwardRef, useImperativeHandle, useMemo, useRef, useState } from 'react';

import { forwardRef, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
import { Form, Formik, FormikProps } from 'formik';
import * as yup from 'yup';
import { Card, ComponentCenteredSpinner, Field, Label, Select, SelectOption } from '@/components';
Expand All @@ -7,6 +8,36 @@ import { useGetHomeOrgQuery, useGetProjectQuery } from '@/api';
import { PickList } from '@/schemas/PickList.schema';
import { useGetProjectOptionsList } from '@/hooks';

function removeNullFields(obj) {
// Check if the input is an object and not null
if (typeof obj !== 'object' || obj === null) {
return obj;
}

// Create a new object to avoid mutating the original object
const result = Array.isArray(obj) ? [] : {};

// Iterate over each key in the object
for (const key in obj) {
// eslint-disable-next-line no-prototype-builtins
if (obj.hasOwnProperty(key)) {
const value = obj[key];

// Recursively clean nested objects or arrays
if (typeof value === 'object' && value !== null) {
const cleanedValue = removeNullFields(value);
if (cleanedValue !== null && (Array.isArray(cleanedValue) ? cleanedValue.length > 0 : Object.keys(cleanedValue).length > 0)) {
result[key] = cleanedValue;
}
} else if (value !== null) {
result[key] = value;
}
}
}

return result;
}

const validationSchema = yup.object({
unitOwner: yup.string(),
unitBlockStart: yup.string().required('Unit Block Start is required'),
Expand Down Expand Up @@ -102,6 +133,12 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
setSelectedWarehouseProjectId(value);
};

useEffect(() => {
if (!unit?.warehouseProjectId && unit?.issuance?.warehouseProjectId) {
setSelectedWarehouseProjectId(unit.issuance.warehouseProjectId.toString());
}
}, [unit, selectedWarehouseProjectId])

if ((isHomeOrgLoading || isProjectOptionsLoading) && !readonly) {
return <ComponentCenteredSpinner label="Loading Selected Project Data" />;
}
Expand All @@ -111,7 +148,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
}

return (
<Formik innerRef={formikRef} initialValues={unit || {}} validationSchema={validationSchema} onSubmit={() => {}}>
<Formik innerRef={formikRef} initialValues={removeNullFields(unit || {})} validationSchema={validationSchema} onSubmit={() => {}}>
{() => (
<Form>
<div className="flex flex-col gap-4">
Expand All @@ -126,7 +163,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
name="select-project"
onChange={handleSetWarehouseProjectId}
options={projectOptions}
initialValue={unit?.warehouseProjectId?.toString() || ''}
initialValue={selectedWarehouseProjectId || ''}
/>
{error && <p className="text-red-500 text-s italic">{error}</p>}
</div>
Expand All @@ -140,22 +177,22 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
type="picklist"
options={projectLocationOptions}
readonly={readonly}
initialValue={unit?.projectLocationId}
initialValue={unit?.projectLocationId || ''}
/>
<Field
name="unitOwner"
label="Unit Owner"
type="text"
readonly={readonly}
initialValue={unit?.unitOwner}
initialValue={unit?.unitOwner || ''}
/>
<Field
name="unitBlockStart"
label="Unit Block Start"
type="text"
readonly={readonly}
required={true}
initialValue={unit?.unitBlockStart}
initialValue={unit?.unitBlockStart || ''}
/>

<Field
Expand All @@ -164,7 +201,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
type="text"
readonly={readonly}
required={true}
initialValue={unit?.unitBlockEnd}
initialValue={unit?.unitBlockEnd || ''}
/>

<Field
Expand All @@ -173,7 +210,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
type="number"
readonly={readonly}
required={true}
initialValue={unit?.unitCount}
initialValue={unit?.unitCount || ''}
/>

<Field
Expand All @@ -183,7 +220,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
options={picklistOptions?.countries}
readonly={readonly}
required={true}
initialValue={unit?.countryJurisdictionOfOwner}
initialValue={unit?.countryJurisdictionOfOwner || ''}
/>

<Field
Expand All @@ -192,7 +229,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
type="text"
readonly={readonly}
required={true}
initialValue={unit?.inCountryJurisdictionOfOwner}
initialValue={unit?.inCountryJurisdictionOfOwner || ''}
/>

<Field
Expand All @@ -202,7 +239,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
options={picklistOptions?.unitType}
readonly={readonly}
required={true}
initialValue={unit?.unitType}
initialValue={unit?.unitType || ''}
/>

<Field
Expand All @@ -212,15 +249,15 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
options={picklistOptions?.unitStatus}
readonly={readonly}
required={true}
initialValue={unit?.unitStatus}
initialValue={unit?.unitStatus || ''}
/>

<Field
name="unitStatusReason"
label="Unit Status Reason"
type="text"
readonly={readonly}
initialValue={unit?.unitStatusReason}
initialValue={unit?.unitStatusReason || ''}
/>

<Field
Expand All @@ -229,7 +266,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
type="number"
readonly={readonly}
required={true}
initialValue={unit?.vintageYear}
initialValue={unit?.vintageYear || ''}
/>
</div>
<div>
Expand All @@ -238,8 +275,8 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
label="Unit Registry Link"
type="link"
readonly={readonly}
initialValue={unit?.unitRegistryLink || ''}
required={true}
initialValue={unit?.unitRegistryLink}
/>
</div>
</Card>
Expand All @@ -250,23 +287,23 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
label="Marketplace"
type="text"
readonly={readonly}
initialValue={unit?.marketplace}
initialValue={unit?.marketplace || ''}
/>

<Field
name="marketplaceIdentifier"
label="Marketplace Identifier"
type="text"
readonly={readonly}
initialValue={unit?.marketplaceIdentifier}
initialValue={unit?.marketplaceIdentifier || ''}
/>

<Field
name="marketplaceLink"
label="Marketplace Link"
type="text"
readonly={readonly}
initialValue={unit?.marketplaceLink}
initialValue={unit?.marketplaceLink || ''}
/>

<Field
Expand All @@ -276,7 +313,7 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
options={picklistOptions?.correspondingAdjustmentStatus}
readonly={readonly}
required={true}
initialValue={unit?.correspondingAdjustmentStatus}
initialValue={unit?.correspondingAdjustmentStatus || ''}
/>

<Field
Expand All @@ -286,12 +323,18 @@ const UnitForm = forwardRef<UnitFormRef, UnitFormProps>(({ readonly = false, dat
options={picklistOptions?.correspondingAdjustmentDeclaration}
readonly={readonly}
required={true}
initialValue={unit?.correspondingAdjustmentDeclaration}
initialValue={unit?.correspondingAdjustmentDeclaration || ''}
/>
</div>
</Card>
<Card>
<Field name="unitTags" label="Unit Tags" type="tag" readonly={readonly} initialValue={unit?.unitTags} />
<Field
name="unitTags"
label="Unit Tags"
type="tag"
readonly={readonly}
initialValue={unit?.unitTags || ''}
/>
</Card>
</div>
</Form>
Expand Down
Loading

0 comments on commit 4683aad

Please sign in to comment.