Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIQM-716-test #757

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/MarcRoute/MarcRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const MarcRoute = ({
const {
marcType,
action,
useMarcActionHandler,
} = routeProps;
const centralTenantId = stripes.user.user?.consortium?.centralTenantId;
const isRequestToCentralTenantFromMember = applyCentralTenantInHeaders(location, stripes, marcType)
Expand Down Expand Up @@ -75,6 +76,7 @@ const MarcRoute = ({
onClose={onClose}
onSave={onSave}
externalRecordPath={externalRecordPath}
useMarcActionHandler={useMarcActionHandler}
onCheckCentralTenantPerm={checkCentralTenantPerm}
/>
</QuickMarcProvider>
Expand Down
25 changes: 22 additions & 3 deletions src/QuickMarc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import {
Switch,
Expand All @@ -13,6 +13,19 @@ import {
MARC_TYPES,
keyboardCommands,
} from './common/constants';
import { useMarcCreate } from './QuickMarcEditor/useMarcCreate';
import { useMarcEdit } from './QuickMarcEditor/useMarcEdit';
import { useMarcDerive } from './QuickMarcEditor/useMarcDerive';

const createUseMarcActionHandler = (action) => {
const marcActionHooks = {
[QUICK_MARC_ACTIONS.CREATE]: useMarcCreate,
[QUICK_MARC_ACTIONS.EDIT]: useMarcEdit,
[QUICK_MARC_ACTIONS.DERIVE]: useMarcDerive,
};

return marcActionHooks[action];
};

const QuickMarc = ({
basePath,
Expand All @@ -33,13 +46,16 @@ const QuickMarc = ({
// .../some-path/create-bibliographic => [, create-bibliographic, create, bibliographic]
const [, page, action] = location.pathname.match(/\/((edit|create|derive)-(bibliographic|authority|holdings))/) || [];

const useMarcActionHandler = useMemo(() => createUseMarcActionHandler(action), [action]);

const editorRoutesConfig = [
{
path: `${basePath}/:action-bibliographic/:externalId?`,
permission: permissionsMap[page],
props: {
action,
marcType: MARC_TYPES.BIB,
useMarcActionHandler,
},
},
{
Expand All @@ -48,22 +64,25 @@ const QuickMarc = ({
props: {
action,
marcType: MARC_TYPES.AUTHORITY,
useMarcActionHandler,
},
},
{
path: `${basePath}/create-holdings/:externalId`,
permission: 'ui-quick-marc.quick-marc-holdings-editor.create',
props: {
action: QUICK_MARC_ACTIONS.CREATE,
action,
marcType: MARC_TYPES.HOLDINGS,
useMarcActionHandler,
},
},
{
path: `${basePath}/edit-holdings/:instanceId/:externalId`,
permission: 'ui-quick-marc.quick-marc-holdings-editor.all',
props: {
action: QUICK_MARC_ACTIONS.EDIT,
action,
marcType: MARC_TYPES.HOLDINGS,
useMarcActionHandler,
},
},
];
Expand Down
15 changes: 13 additions & 2 deletions src/QuickMarcEditor/QuickMarcEditorContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import QuickMarcEditor from './QuickMarcEditor';
import { useAuthorityLinksCount } from '../queries';
import { QuickMarcContext } from '../contexts';
import { useSaveRecord } from './useSaveRecord';

Check warning on line 24 in src/QuickMarcEditor/QuickMarcEditorContainer.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'useSaveRecord' is defined but never used. Allowed unused vars must match /React/u
import {
EXTERNAL_INSTANCE_APIS,
MARC_RECORD_API,
Expand Down Expand Up @@ -69,6 +69,7 @@
location,
externalRecordPath,
stripes,
useMarcActionHandler,

Check failure on line 72 in src/QuickMarcEditor/QuickMarcEditorContainer.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'useMarcActionHandler' is missing in props validation
onCheckCentralTenantPerm,
}) => {
const {
Expand Down Expand Up @@ -216,15 +217,25 @@
setRelatedRecordVersion,
]);

const { onSubmit, httpError, runValidation } = useSaveRecord({
// const { onSubmit, httpError, runValidation } = useSaveRecord({
// linksCount,
// locations,
// fixedFieldSpec,
// mutator,
// refreshPageData: loadData,
// onClose: handleClose,
// onSave: handleSave,
// });

const { onSubmit, httpError, runValidation } = useMarcActionHandler({
linksCount,
locations,
fixedFieldSpec,
mutator,
refreshPageData: loadData,
onClose: handleClose,
onSave: handleSave,
});
})

Check failure on line 238 in src/QuickMarcEditor/QuickMarcEditorContainer.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

Missing semicolon

useEffect(() => {
loadData();
Expand Down
1 change: 1 addition & 0 deletions src/QuickMarcEditor/useMarcCreate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useMarcCreate';
231 changes: 231 additions & 0 deletions src/QuickMarcEditor/useMarcCreate/useMarcCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import {
useCallback,
useContext,
useMemo,
useState,
} from 'react';
import {
useHistory,
useLocation, useParams,

Check warning on line 9 in src/QuickMarcEditor/useMarcCreate/useMarcCreate.js

View workflow job for this annotation

GitHub Actions / ui / Install and lint / Install and lint

'useParams' is defined but never used. Allowed unused vars must match /React/u
} from 'react-router-dom';
import flow from 'lodash/flow';
import isEmpty from 'lodash/isEmpty';
import noop from 'lodash/noop';

import { useStripes } from '@folio/stripes/core';
import { useShowCallout } from '@folio/stripes-acq-components';

import { QuickMarcContext } from '../../contexts';
import {
useAuthorityLinking,
useValidation,
} from '../../hooks';
import {
applyCentralTenantInHeaders,
autopopulateFixedField,
autopopulateIndicators,
autopopulateMaterialCharsField,
autopopulatePhysDescriptionField,
autopopulateSubfieldSection,
cleanBytesFields,
combineSplitFields,
formatLeaderForSubmit,
hydrateMarcRecord,
parseHttpError,
processEditingAfterCreation,
recordHasLinks,
redirectToRecord,
removeDeletedRecords,
removeRowsWithoutContent,
saveLinksToNewRecord,
} from '../utils';
import { MARC_TYPES } from '../../common';
import getQuickMarcRecordStatus from '../getQuickMarcRecordStatus';

const useMarcCreate = ({
linksCount,
locations,
fixedFieldSpec,
mutator,
refreshPageData,
onSave,
}) => {
const history = useHistory();
const location = useLocation();
const stripes = useStripes();
const showCallout = useShowCallout();

const {
action,
marcType,
initialValues,
instance,
validationErrorsRef,
continueAfterSave,
basePath,
} = useContext(QuickMarcContext);

const [httpError, setHttpError] = useState(null);

const isRequestToCentralTenantFromMember = applyCentralTenantInHeaders(location, stripes, marcType);
const centralTenantId = stripes.user.user.consortium?.centralTenantId;
const tenantId = isRequestToCentralTenantFromMember ? centralTenantId : '';

const {
linkableBibFields,
linkingRules,
sourceFiles,
actualizeLinks,
} = useAuthorityLinking({ marcType, action });

const validationContext = useMemo(() => ({
initialValues,
marcType,
action,
linkableBibFields,
linkingRules,
fixedFieldSpec,
instanceId: instance?.id,
sourceFiles,
linksCount,
naturalId: instance?.naturalId,
locations,
}), [
action,
instance?.naturalId,
linksCount,
initialValues,
marcType,
locations,
linkableBibFields,
linkingRules,
sourceFiles,
fixedFieldSpec,
instance?.id,
]);

const { validate } = useValidation(validationContext, tenantId);

const prepareForSubmit = useCallback((formValues) => {
const formValuesForCreate = flow(
removeDeletedRecords,
removeRowsWithoutContent,
autopopulateIndicators,
marcRecord => autopopulateFixedField(marcRecord, marcType, fixedFieldSpec),
autopopulatePhysDescriptionField,
autopopulateMaterialCharsField,
marcRecord => autopopulateSubfieldSection(marcRecord, marcType),
marcRecord => cleanBytesFields(marcRecord, fixedFieldSpec, marcType),
marcRecord => formatLeaderForSubmit(marcType, marcRecord),
combineSplitFields,
)(formValues);

return formValuesForCreate;
}, [marcType, fixedFieldSpec]);

const runValidation = useCallback(async (formValues) => {
const formValuesForValidation = prepareForSubmit(formValues);

return validate(formValuesForValidation.records);
}, [validate, prepareForSubmit]);

const onSubmit = useCallback(async (formValues, _api, complete) => {
// if validation has any issues - cancel submit
if (!isEmpty(validationErrorsRef.current)) {
return complete();
}

const formValuesToProcess = prepareForSubmit(formValues);

let formValuesToHydrate;

try {
if (marcType === MARC_TYPES.BIB) {
formValuesToHydrate = await actualizeLinks(formValuesToProcess);
} else {
formValuesToHydrate = formValuesToProcess;
}
} catch (errorResponse) {
const parsedError = await parseHttpError(errorResponse);

setHttpError(parsedError);

return null;
}

formValuesToHydrate._actionType = 'create';

const formValuesForCreate = hydrateMarcRecord(formValuesToHydrate);

return mutator.quickMarcEditMarcRecord.POST(formValuesForCreate)
.then(async ({ qmRecordId }) => {
const instanceId = formValues.externalId;

showCallout({ messageId: 'ui-quick-marc.record.save.success.processing' });

try {
const { externalId } = await getQuickMarcRecordStatus({
quickMarcRecordStatusGETRequest: mutator.quickMarcRecordStatus.GET,
qmRecordId,
showCallout,
});

showCallout({ messageId: 'ui-quick-marc.record.saveNew.success' });

if (marcType === MARC_TYPES.BIB && recordHasLinks(formValuesForCreate.fields)) {
await saveLinksToNewRecord(mutator, externalId, formValuesForCreate)
.catch(noop);
}

if (continueAfterSave.current) {
await processEditingAfterCreation(
formValues,
externalId,
basePath,
location,
history,
refreshPageData,
marcType,
);

return;
}

await redirectToRecord(externalId, instanceId, marcType, onSave);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
showCallout({
messageId: 'ui-quick-marc.record.saveNew.error',
type: 'error',
});
}
})
.catch(async (errorResponse) => {
const parsedError = await parseHttpError(errorResponse);

setHttpError(parsedError);
});
}, [
showCallout,
prepareForSubmit,
actualizeLinks,
validationErrorsRef,
marcType,
continueAfterSave,
mutator,
basePath,
location,
history,
refreshPageData,
onSave,
]);

return {
onSubmit,
httpError,
runValidation,
};
};

export { useMarcCreate };
1 change: 1 addition & 0 deletions src/QuickMarcEditor/useMarcDerive/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useMarcDerive';
Loading
Loading