Skip to content

Commit

Permalink
Microsoft and QA mob fixes
Browse files Browse the repository at this point in the history
- fix popup login
- Page --> Pages
- don't clear number metadata
- strings for data type mismatch should match Laserfiche (i.e. string --> text)
- user notification of automatic rename when creating or importing docs
- don't restrict filename, / is the only thing not permitted
  • Loading branch information
alexandria.gomez authored and alexandria.gomez committed Oct 13, 2023
1 parent f0c2dd0 commit ab6715e
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 192 deletions.
19 changes: 19 additions & 0 deletions src/Utils/Funcs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UrlUtils } from '@laserfiche/lf-js-utils';
import { WFieldType } from '@laserfiche/lf-repository-api-client';
import { BaseComponentContext } from '@microsoft/sp-component-base';
import { SPDEVMODE_LOCAL_STORAGE_KEY } from '../webparts/constants';

Expand Down Expand Up @@ -46,3 +47,21 @@ export function getRegion(): string {
const region = spDevModeTrue ? 'a.clouddev.laserfiche.com' : 'laserfiche.com';
return region;
}

export function getCorrespondingTypeFieldName(fieldType: WFieldType): string {
switch (fieldType) {
case WFieldType.Date:
case WFieldType.List:
case WFieldType.Time:
case WFieldType.Number:
return fieldType;
case WFieldType.DateTime:
return 'Date/Time';
case WFieldType.String:
return 'Text';
case WFieldType.ShortInteger:
return 'Integer';
case WFieldType.LongInteger:
return 'Long Integer';
}
}
76 changes: 76 additions & 0 deletions src/extensions/savetoLaserfiche/CommonDialogs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,79 @@ export function SavedToLaserficheSuccessDialogButtons(props: {
</>
);
}

const createPromise: () => Promise<boolean>[] = () => {
let resolver;
return [
new Promise<boolean>((resolve, reject) => {
resolver = resolve;
}),
resolver,
];
};

export const useConfirm: () => [
(text: string) => Promise<unknown>,
(props: { cancelButtonText: string }) => JSX.Element
] = () => {
const [open, setOpen] = React.useState(false);
const [resolver, setResolver] = React.useState({ resolve: null });
const [label, setLabel] = React.useState('');

const getConfirmation: (text: string) => Promise<boolean> = async (text: string) => {
setLabel(text);
setOpen(true);
const [promise, resolve] = await createPromise();
setResolver({ resolve: resolve });
return promise;
};

const onClick: (status: boolean) => Promise<void> = async (status: boolean) => {
setOpen(false);
resolver.resolve(status);
};

const Confirmation: (props: {
cancelButtonText: string;
}) => JSX.Element = (props: { cancelButtonText: string }) => (
<>
{open && (
<>
<div className={`modal-header ${styles.header}`}>
<div className='modal-title' id='ModalLabel'>
<div className={styles.logoHeader}>
<img
src='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAC0CAMAAAAKE/YAAAAAUVBMVEXSXyj////HYzL/+/T/+Or/9d+yaUa9ZT2yaUj/9OG7Zj3SXybRYCj/+/b///3LYS/OYCvEZDS2aEL/89jAZTnMYS3/8dO7Zzusa02+ZTn/78wyF0DsAAABnUlEQVR4nO3ci26CMABGYQcoLRS5OTf2/g86R+KSLYUm2vxcPB8RTYzxkADRajkcAAAAAAAAAADYgbJcusCvqdtLnhfeJR/a96X7vOriarNJ/cUtHeiTnI7p26TsY+XRZ190sXSfVyA6X7rP6xZdzeweREeTGDt3IBIdTeCUR3Q0wQOxLNf3CWSr0ZvcPYiWIFqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVYhWIVqFaBWiVV4zeok/379m9BL2HO1Ckymlky0jRQc3Kqoou4f6YHzdaLX56PRzak757/JjfDS0dbOK6HM6Paf8P3st6lVE/9mAwPOpNcnqokOIJppoookmmmiiiSaaaKKJ3k30OfTFdU3RXZ+lT6qq6rbO+k4VXQ9fvT2OrH30Zo+3u/5rUI17NO3QmdPImIduxoyrUze0khEm5w6uqZNIRKNi91Hl5661dH+tdow6wts5J//BaJPRwH6IT1NxbDJ6vVc+nrXJaAAAAADALn0DBosqnCStFi4AAAAASUVORK5CYII='
width='30'
height='30'
/>
<span className={styles.paddingLeft}>
Document already exists
</span>
</div>
</div>
</div>
<div className={`modal-body ${styles.contentBox}`}>
<span>{label}</span>
</div>
<div className={`modal-footer ${styles.footer}`}>
<button
className={`lf-button primary-button ${styles.actionButton}`}
onClick={() => onClick(true)}
>
Continue
</button>
<button
className='lf-button sec-button'
onClick={() => onClick(false)}
>
{props.cancelButtonText}
</button>
</div>
</>
)}
</>
);

return [getConfirmation, Confirmation];
};
31 changes: 6 additions & 25 deletions src/extensions/savetoLaserfiche/GetDocumentDataDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,32 +416,13 @@ function GetDocumentDialogData(props: {
// automatically trims length to match constraint
spDocFieldValue = spDocFieldValue.slice(0, lfField.length);
}
} else if (lfField.fieldType === WFieldType.ShortInteger) {
} else if (
lfField.fieldType === WFieldType.ShortInteger ||
lfField.fieldType === WFieldType.LongInteger ||
lfField.fieldType === WFieldType.Number
) {
const extractOnlyNumbers = spDocFieldValue.replace(/[^0-9]/g, '');
const valueAsNumber = Number.parseInt(extractOnlyNumbers, 10);
if (valueAsNumber > 64999 || valueAsNumber < 0) {
// TODO invalid field -- should it truncate???
spDocFieldValue = '';
} else {
spDocFieldValue = extractOnlyNumbers;
}
} else if (lfField.fieldType === WFieldType.LongInteger) {
const extractOnlyNumbers = spDocFieldValue.replace(/[^0-9]/g, '');
const valueAsNumber = Number.parseInt(extractOnlyNumbers, 10);
if (valueAsNumber > 3999999999 || valueAsNumber < 0) {
// TODO invalid field -- should it truncate???
spDocFieldValue = '';
} else {
spDocFieldValue = extractOnlyNumbers;
}
} else if (lfField.fieldType === WFieldType.Number) {
const valueRemoveNonNumbers = spDocFieldValue.replace(/[^0-9.]/g, '');
if (!isNaN(Number.parseFloat(valueRemoveNonNumbers))) {
spDocFieldValue = valueRemoveNonNumbers;
} else {
// TODO invalid field -- should it truncate???
spDocFieldValue = '';
}
spDocFieldValue = extractOnlyNumbers;
}
return spDocFieldValue;
}
Expand Down
81 changes: 28 additions & 53 deletions src/extensions/savetoLaserfiche/SaveDocumentToLaserfiche.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
SetFields,
APIServerException,
} from '@laserfiche/lf-repository-api-client';
import { RepositoryClientExInternal } from '../../repository-client/repository-client';
import { IRepositoryApiClientExInternal } from '../../repository-client/repository-client-types';
import { getEntryWebAccessUrl } from '../../Utils/Funcs';
import { ISPDocumentData } from '../../Utils/Types';
Expand All @@ -32,46 +31,35 @@ export interface SavedToLaserficheDocumentData {
}

export class SaveDocumentToLaserfiche {
constructor(private spFileMetadata: ISPDocumentData) {}
constructor(
private spFileMetadata: ISPDocumentData,
private validRepoClient: IRepositoryApiClientExInternal
) {}

async trySaveDocumentToLaserficheAsync(): Promise<
SavedToLaserficheDocumentData
> {
async trySaveDocumentToLaserficheAsync(): Promise<SavedToLaserficheDocumentData> {
const loginComponent: NgElement & WithProperties<LfLoginComponent> =
document.querySelector('lf-login');
const accessToken = loginComponent?.authorization_credentials?.accessToken;
if (accessToken) {
const validRepoClient = await this.tryGetValidRepositoryClientAsync();
const webClientUrl = loginComponent?.account_endpoints.webClientUrl;

if (validRepoClient && this.spFileMetadata) {
if (this.validRepoClient && this.spFileMetadata) {
const spFileData = await this.GetFileData();
const result = await this.saveFileToLaserficheAsync(
spFileData,
validRepoClient,
webClientUrl
);
return result;
} else {
throw Error('You are not signed in or there was an issue retrieving data from SharePoint. Please try again.')
throw Error(
'You are not signed in or there was an issue retrieving data from SharePoint. Please try again.'
);
}
} else {
// user is not logged in
}
}

async tryGetValidRepositoryClientAsync(): Promise<IRepositoryApiClientExInternal> {
const repoClientCreator = new RepositoryClientExInternal();
const newRepoClient = await repoClientCreator.createRepositoryClientAsync();
try {
// test accessToken validity
await newRepoClient.repositoriesClient.getRepositoryList({});
} catch {
return undefined;
}
return newRepoClient;
}

async GetFileData(): Promise<Blob> {
const spFileUrl = this.spFileMetadata.fileUrl;
const fileNameWithExt = this.spFileMetadata.fileName;
Expand Down Expand Up @@ -99,22 +87,19 @@ export class SaveDocumentToLaserfiche {

async saveFileToLaserficheAsync(
spFileData: Blob,
repoClient: IRepositoryApiClientExInternal,
webClientUrl: string
): Promise<SavedToLaserficheDocumentData | undefined> {
if (spFileData && repoClient) {
if (spFileData && this.validRepoClient) {
const laserficheProfileName = this.spFileMetadata.lfProfile;
let result: SavedToLaserficheDocumentData | undefined;
if (laserficheProfileName) {
result = await this.sendToLaserficheWithMappingAsync(
spFileData,
repoClient,
webClientUrl
);
} else {
result = await this.sendToLaserficheNoMappingAsync(
spFileData,
repoClient,
webClientUrl
);
}
Expand All @@ -125,7 +110,6 @@ export class SaveDocumentToLaserfiche {

async sendToLaserficheWithMappingAsync(
fileData: Blob,
repoClient: IRepositoryApiClientExInternal,
webClientUrl: string
): Promise<SavedToLaserficheDocumentData | undefined> {
let request: PostEntryWithEdocMetadataRequest;
Expand All @@ -145,7 +129,7 @@ export class SaveDocumentToLaserfiche {
this.spFileMetadata.documentName.includes('FileName');

const parentEntryId = Number(this.spFileMetadata.entryId);
const repoId = await repoClient.getCurrentRepoId();
const repoId = await this.validRepoClient.getCurrentRepoId();

let fileName: string | undefined;
let fileNameInEdoc: string | undefined;
Expand Down Expand Up @@ -185,7 +169,7 @@ export class SaveDocumentToLaserfiche {

try {
const entryCreateResult: CreateEntryResult =
await repoClient.entriesClient.importDocument(entryRequest);
await this.validRepoClient.entriesClient.importDocument(entryRequest);
const entryId = entryCreateResult.operations.entryCreate.entryId ?? 1;
const fileLink = getEntryWebAccessUrl(
entryId.toString(),
Expand All @@ -211,15 +195,10 @@ export class SaveDocumentToLaserfiche {
pathBack: path,
metadataSaved: true,
fileName,
action: this.spFileMetadata.action
action: this.spFileMetadata.action,
};

await this.tryUpdateFileNameAsync(
repoClient,
repoId,
entryCreateResult,
fileInfo
);
await this.tryUpdateFileNameAsync(repoId, entryCreateResult, fileInfo);
return fileInfo;
} catch (error) {
const conflict409 =
Expand Down Expand Up @@ -260,10 +239,10 @@ export class SaveDocumentToLaserfiche {
metadataSaved: false,
failedMetadata,
fileName,
action: undefined
action: undefined,
};

await this.tryUpdateFileNameAsync(repoClient, repoId, error, fileInfo);
await this.tryUpdateFileNameAsync(repoId, error, fileInfo);
return fileInfo;
} else {
window.localStorage.removeItem(SP_LOCAL_STORAGE_KEY);
Expand Down Expand Up @@ -302,7 +281,6 @@ export class SaveDocumentToLaserfiche {

async sendToLaserficheNoMappingAsync(
fileData: Blob,
repoClient: IRepositoryApiClientExInternal,
webClientUrl: string
): Promise<SavedToLaserficheDocumentData | undefined> {
const fileNameWithExt = this.spFileMetadata.fileName;
Expand All @@ -314,7 +292,7 @@ export class SaveDocumentToLaserfiche {
const parentEntryId = 1;

try {
const repoId = await repoClient.getCurrentRepoId();
const repoId = await this.validRepoClient.getCurrentRepoId();
const electronicDocument: FileParameter = {
fileName: fileNameWithExt,
data: fileData,
Expand All @@ -330,7 +308,7 @@ export class SaveDocumentToLaserfiche {
};

const entryCreateResult: CreateEntryResult =
await repoClient.entriesClient.importDocument(entryRequest);
await this.validRepoClient.entriesClient.importDocument(entryRequest);
const entryId = entryCreateResult.operations.entryCreate.entryId;
const fileLink = getEntryWebAccessUrl(
entryId.toString(),
Expand All @@ -348,14 +326,9 @@ export class SaveDocumentToLaserfiche {
pathBack: path,
metadataSaved: true,
fileName: fileNameWithExt,
action: this.spFileMetadata.action
action: this.spFileMetadata.action,
};
await this.tryUpdateFileNameAsync(
repoClient,
repoId,
entryCreateResult,
fileInfo
);
await this.tryUpdateFileNameAsync(repoId, entryCreateResult, fileInfo);
return fileInfo;
} catch (error) {
window.localStorage.removeItem(SP_LOCAL_STORAGE_KEY);
Expand All @@ -364,16 +337,16 @@ export class SaveDocumentToLaserfiche {
}

private async tryUpdateFileNameAsync(
repoClient: IRepositoryApiClientExInternal,
repoId: string,
entryCreateResult: CreateEntryResult,
fileInfo: SavedToLaserficheDocumentData
): Promise<void> {
try {
const entryInfo: Entry = await repoClient.entriesClient.getEntry({
repoId,
entryId: entryCreateResult.operations.entryCreate.entryId,
});
const entryInfo: Entry =
await this.validRepoClient.entriesClient.getEntry({
repoId,
entryId: entryCreateResult.operations.entryCreate.entryId,
});

fileInfo.fileName = entryInfo.name;
} catch {
Expand All @@ -385,7 +358,9 @@ export class SaveDocumentToLaserfiche {
const response = await this.deleteFileAsync();
window.localStorage.removeItem(SP_LOCAL_STORAGE_KEY);
if (!response.ok) {
throw Error(`An error occurred while deleting file: ${response.statusText}`);
throw Error(
`An error occurred while deleting file: ${response.statusText}`
);
}
}

Expand Down
Loading

0 comments on commit ab6715e

Please sign in to comment.