implements a way to clear relationship field values

This commit is contained in:
James
2020-07-28 20:32:24 -04:00
parent ed56971de6
commit 210295e704
4 changed files with 24 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import AnimateHeight from 'react-animate-height';
import { Draggable } from 'react-beautiful-dnd';

View File

@@ -25,16 +25,18 @@ class Relationship extends Component {
constructor(props) {
super(props);
const { relationTo, hasMultipleRelations } = this.props;
const { relationTo, hasMultipleRelations, required } = this.props;
const relations = hasMultipleRelations ? relationTo : [relationTo];
this.initialOptions = required ? [] : [{ value: 'null', label: 'None' }];
this.state = {
relations,
lastFullyLoadedRelation: -1,
lastLoadedPage: 1,
options: [],
errorLoading: false,
loadedIDs: [],
options: this.initialOptions,
};
}
@@ -59,7 +61,7 @@ class Relationship extends Component {
if (clear) {
this.setState({
options: [],
options: this.initialOptions,
loadedIDs: [],
lastFullyLoadedRelation: -1,
});
@@ -141,7 +143,7 @@ class Relationship extends Component {
if (hasMultipleRelations) {
options.forEach((option) => {
const potentialValue = option.options.find((subOption) => {
const potentialValue = option.options && option.options.find((subOption) => {
if (subOption?.value?.value && value?.value) {
return subOption.value.value === value.value;
}
@@ -259,9 +261,17 @@ class Relationship extends Component {
}
addOptionByID = async (id, relation) => {
const { errorLoading } = this.state;
if (!errorLoading) {
const response = await fetch(`${serverURL}${api}/${relation}/${id}`);
if (response.ok) {
const data = await response.json();
this.addOptions({ docs: [data] }, relation);
} else {
console.log(`There was a problem loading the document with ID of ${id}.`);
}
}
}
handleInputChange = (search) => {

View File

@@ -4,7 +4,6 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Payload</title>
</head>
<body>

View File

@@ -131,9 +131,13 @@ async function performFieldOperations(entityConfig, operation) {
}
};
const createHookPromise = async (data, field) => {
const createHookPromise = async (data, originalDoc, field) => {
const resultingData = data;
if ((field.type === 'relationship' || field.type === 'upload') && (data[field.name] === 'null' || data[field.name] === null)) {
data[field.name] = null;
}
if (hook === 'afterRead') {
if ((field.type === 'relationship' || field.type === 'upload')) {
const hasManyRelations = Array.isArray(field.relationTo);
@@ -262,7 +266,7 @@ async function performFieldOperations(entityConfig, operation) {
}
accessPromises.push(createAccessPromise(data, originalDoc, field));
hookPromises.push(createHookPromise(data, field));
hookPromises.push(createHookPromise(data, originalDoc, field));
if (field.fields) {
if (field.name === undefined) {