Skip to content

Commit 98b8e57

Browse files
committed
additional code review
1 parent 3eb9b85 commit 98b8e57

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ProgressIndicator } from 'office-ui-fabric-react/lib/ProgressIndicator'
1313
import * as strings from 'ControlStrings';
1414
import { IFilePickerResult } from '../filePicker';
1515
import { IUploadImageResult } from '../../common/SPEntities';
16+
import { SPHttpClient } from '@microsoft/sp-http';
1617

1718
const stackTokens: IStackTokens = { childrenGap: 20 };
1819

@@ -261,7 +262,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
261262
let defaultContentType = await spList.contentTypes.select("Id", "Name").get();
262263
contentTypeId = defaultContentType[0]["Id"].StringValue;
263264
}
264-
const listFeilds = await this._spService.getListContentTypeFieldsInfo(listId, contentTypeId, context.pageContext.web.absoluteUrl);
265+
const listFeilds = await this.getFormFields(listId, contentTypeId, context.pageContext.web.absoluteUrl);
265266
const tempFields: IDynamicFieldProps[] = [];
266267
let order: number = 0;
267268
const responseValue = listFeilds['value'];
@@ -272,7 +273,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
272273
field.order = order;
273274
let hiddenName = "";
274275
let termSetId = "";
275-
let lookupListID = "";
276+
let lookupListId = "";
276277
let lookupField = "";
277278
let choices: IDropdownOption[] = [];
278279
let defaultValue = null;
@@ -294,7 +295,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
294295
richText = field["RichText"];
295296
}
296297
else if (fieldType === "Lookup") {
297-
lookupListID = field["LookupList"];
298+
lookupListId = field["LookupList"];
298299
lookupField = field["LookupField"];
299300
if (item !== null) {
300301
defaultValue = await this._spService.getLookupValue(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
@@ -305,7 +306,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
305306

306307
}
307308
else if (fieldType === "LookupMulti") {
308-
lookupListID = field["LookupList"];
309+
lookupListId = field["LookupList"];
309310
lookupField = field["LookupField"];
310311
if (item !== null) {
311312
defaultValue = await this._spService.getLookupValues(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
@@ -395,7 +396,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
395396
newValue: null,
396397
fieldTermSetId: termSetId,
397398
options: choices,
398-
lookupListID: lookupListID,
399+
lookupListID: lookupListId,
399400
lookupField: lookupField,
400401
changedValue: defaultValue,
401402
fieldType: field.TypeAsString,
@@ -455,4 +456,32 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
455456
};
456457
});
457458
}
459+
460+
private getFormFields = async (listId: string, contentTypeId: string | undefined, webUrl?: string): Promise<any[]> => {
461+
try {
462+
const {
463+
context
464+
} = this.props;
465+
const webAbsoluteUrl = !webUrl ? context.pageContext.web.absoluteUrl : webUrl;
466+
let apiUrl = '';
467+
if (contentTypeId !== undefined && contentTypeId !== '') {
468+
apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/contenttypes('${contentTypeId}')/fields?@listId=guid'${encodeURIComponent(listId)}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')`;
469+
}
470+
else {
471+
apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/fields?@listId=guid'${encodeURIComponent(listId)}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')`;
472+
}
473+
const data = await context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
474+
if (data.ok) {
475+
const results = await data.json();
476+
if (results) {
477+
return results;
478+
}
479+
}
480+
481+
return null;
482+
} catch (error) {
483+
console.dir(error);
484+
return Promise.reject(error);
485+
}
486+
}
458487
}

src/services/SPService.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -529,30 +529,4 @@ export default class SPService implements ISPService {
529529

530530
return result;
531531
}
532-
533-
534-
public async getListContentTypeFieldsInfo(listId: string, contentTypeId: string | undefined, webUrl?: string): Promise<any[]> {
535-
try {
536-
const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl;
537-
let apiUrl = '';
538-
if (contentTypeId !== undefined && contentTypeId !== '') {
539-
apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/contenttypes('${contentTypeId}')/fields?@listId=guid'${encodeURIComponent(listId)}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')`;
540-
}
541-
else {
542-
apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/fields?@listId=guid'${encodeURIComponent(listId)}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')`;
543-
}
544-
const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1);
545-
if (data.ok) {
546-
const results = await data.json();
547-
if (results) {
548-
return results;
549-
}
550-
}
551-
552-
return null;
553-
} catch (error) {
554-
console.dir(error);
555-
return Promise.reject(error);
556-
}
557-
}
558532
}

src/webparts/controlsTest/components/ControlsTest_SingleComponent.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ export default class ControlsTest extends React.Component<IControlsTestProps, IC
460460
<div className={styles.controlsTest}>
461461
<DynamicForm
462462
context={this.props.context}
463-
listId={"b1416fca-dc77-4198-a082-62a7657dcfa9"}
464-
listItemId={26}
463+
listId={"2baf2f85-4f31-46c4-8f87-5fed93ecd20c"}
464+
//listItemId={26}
465465
onCancelled={() => { console.log('Cancelled'); }}
466466
onSubmitted={async (listItem) => { console.log(listItem); }}>
467467

0 commit comments

Comments
 (0)