Skip to content

Commit 8c62b32

Browse files
author
Florian Kroenert
committed
Implemented handling of image upload on not saved record
1 parent 5ec41c8 commit 8c62b32

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

src/web/Xrm.Oss.HtmlTemplating/HTMLWYSIWYGEDITOR/ControlManifest.Input.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<manifest>
3-
<control namespace="oss" constructor="HTMLWYSIWYGEDITOR" version="0.0.57" display-name-key="HTMLWYSIWYGEDITOR" description-key="HTMLWYSIWYGEDITOR description" control-type="virtual" >
3+
<control namespace="oss" constructor="HTMLWYSIWYGEDITOR" version="0.0.59" display-name-key="HTMLWYSIWYGEDITOR" description-key="HTMLWYSIWYGEDITOR description" control-type="virtual" >
44
<!--external-service-usage node declares whether this 3rd party PCF control is using external service or not, if yes, this control will be considered as premium and please also add the external domain it is using.
55
If it is not using any external service, please set the enabled="false" and DO NOT add any domain below. The "enabled" will be false by default.
66
Example1:

src/web/Xrm.Oss.HtmlTemplating/components/App.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface FormContext {
3636

3737
export interface FunctionContext {
3838
editorRef: EditorRef,
39-
formContext: FormContext,
39+
getFormContext: () => FormContext,
4040
webApiClient: typeof WebApiClient
4141
}
4242

@@ -86,10 +86,10 @@ export const App: React.FC<AppProps> = React.memo((props) => {
8686
const [designContext, dispatchDesign] = React.useReducer(designStateReducer, { design: { json: "", html: "" }, isLocked: false } as DesignState);
8787
const [isFullScreen, setIsFullScreen] = React.useState(false);
8888

89-
const formContext: FormContext = {
89+
const getFormContext: () => FormContext = () => ({
9090
entityId: (props.pcfContext.mode as any).contextInfo.entityId,
9191
entity: (props.pcfContext.mode as any).contextInfo.entityTypeName
92-
};
92+
});
9393

9494
// Init once initially and every time fullscreen activates / deactivates
9595
React.useEffect(() => { init(); }, [ isFullScreen ]);
@@ -169,7 +169,7 @@ export const App: React.FC<AppProps> = React.memo((props) => {
169169

170170
const functionContext: FunctionContext = {
171171
editorRef: editorRef.current!,
172-
formContext: formContext,
172+
getFormContext: getFormContext,
173173
webApiClient: WebApiClient
174174
};
175175

@@ -230,7 +230,7 @@ export const App: React.FC<AppProps> = React.memo((props) => {
230230
try {
231231
const funcRef = getExternalScript(props.pcfContext.parameters.customScriptInitFunc.raw);
232232

233-
const funcResult = await funcRef({ editorProps: properties, formContext: formContext, webApiClient: WebApiClient });
233+
const funcResult = await funcRef({ editorProps: properties, getFormContext: getFormContext, webApiClient: WebApiClient });
234234

235235
if (funcResult && funcResult.editorProps) {
236236
propertiesToSet = funcResult.editorProps;

src/web/Xrm.Oss.HtmlTemplating/domain/FileUploader.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { FormContext, FunctionContext, ImageUploadSettings } from "../components
44

55

66
// https://learn.microsoft.com/en-us/power-apps/developer/data-platform/file-column-data?tabs=webapi#upload-files
7-
export const registerFileUploader = ({uploadEntity, uploadEntityFileNameField, uploadEntityBodyField, parentLookupName}: ImageUploadSettings, { editorRef, formContext, webApiClient }: FunctionContext) => {
7+
export const registerFileUploader = ({uploadEntity, uploadEntityFileNameField, uploadEntityBodyField, parentLookupName}: ImageUploadSettings, { editorRef, getFormContext, webApiClient }: FunctionContext) => {
88
editorRef.registerCallback('image', async function (file, done) {
99
var img = file.attachments[0];
1010

@@ -15,8 +15,16 @@ export const registerFileUploader = ({uploadEntity, uploadEntityFileNameField, u
1515
}
1616
};
1717

18-
if (parentLookupName && formContext?.entity && formContext?.entityId) {
19-
(fileRequest as any).entity[`${parentLookupName}@odata.bind`] = `${webApiClient.GetSetName(formContext.entity)}(${formContext.entityId})`;
18+
const formContext = getFormContext();
19+
20+
if (parentLookupName) {
21+
if (formContext?.entity && formContext?.entityId) {
22+
(fileRequest as any).entity[`${parentLookupName}@odata.bind`] = `${webApiClient.GetSetName(formContext.entity)}(${formContext.entityId})`;
23+
}
24+
else {
25+
(window as any).Xrm.Navigation.openAlertDialog("Your record is not yet created. Please save to be able to upload images");
26+
throw new Error("Image upload not possible, record has to be saved first");
27+
}
2028
}
2129

2230
try {

0 commit comments

Comments
 (0)