Skip to content

Commit

Permalink
update status store; add marc store
Browse files Browse the repository at this point in the history
  • Loading branch information
SKarolFolio committed Nov 28, 2024
1 parent 073fb44 commit f821afa
Show file tree
Hide file tree
Showing 35 changed files with 191 additions and 179 deletions.
7 changes: 3 additions & 4 deletions src/common/hooks/useCommonStatus.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useSetRecoilState } from 'recoil';
import { UserNotificationFactory } from '@common/services/userNotification';
import state from '@state';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

export const useCommonStatus = () => {
const setCommonStatus = useSetRecoilState(state.status.commonMessages);
const { addStatusMessages } = useStoreSelector().status;

return {
set: (l10nId: string, type: StatusType) => {
setCommonStatus(currentStatus => [...currentStatus, UserNotificationFactory.createMessage(type, l10nId)]);
addStatusMessages(UserNotificationFactory.createMessage(type, l10nId));
},
};
};
5 changes: 3 additions & 2 deletions src/common/hooks/useComplexLookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import state from '@state';
import { useModalControls } from './useModalControls';
import { useMarcData } from './useMarcData';
import { useServicesContext } from './useServicesContext';
import { useStoreSelector } from './useStoreSelectors';

export const useComplexLookup = ({
entry,
Expand All @@ -29,11 +30,11 @@ export const useComplexLookup = ({
const schema = useRecoilValue(state.config.schema);
const marcPreviewMetadata = useRecoilValue(state.data.marcPreviewMetadata);
const [selectedEntries, setSelectedEntries] = useRecoilState(state.config.selectedEntries);
const resetMarcPreviewData = useResetRecoilState(state.data.marcPreviewData);
const { setData, resetData: resetMarcPreviewData } = useStoreSelector().marcPreview;
const resetMarcPreviewMetadata = useResetRecoilState(state.data.marcPreviewMetadata);
const resetIsMarcPreviewOpen = useResetRecoilState(state.ui.isMarcPreviewOpen);
const { isModalOpen, setIsModalOpen, openModal } = useModalControls();
const { fetchMarcData } = useMarcData(state.data.marcPreviewData);
const { fetchMarcData } = useMarcData(setData);
const { uuid, linkedEntry } = entry;
const linkedField = getLinkedField({ schema, linkedEntry });

Expand Down
10 changes: 6 additions & 4 deletions src/common/hooks/useContainerEvents.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import state from '@state';

import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useRecoilValue } from 'recoil';
import { IS_EMBEDDED_MODE } from '@common/constants/build.constants';
import { dispatchEventWrapper, getWrapperAsWebComponent } from '@common/helpers/dom.helper';
import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { ROUTES } from '@common/constants/routes.constants';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';
import state from '@state';

type IUseContainerEvents =
| {
Expand All @@ -15,7 +17,7 @@ type IUseContainerEvents =

export const useContainerEvents = ({ onTriggerModal, watchEditedState = false }: IUseContainerEvents = {}) => {
const hasNavigationOrigin = useRecoilValue(state.config.hasNavigationOrigin);
const isEdited = useRecoilValue(state.status.recordIsEdited);
const { isEditedRecord: isEdited } = useStoreSelector().status;
const {
BLOCK_NAVIGATION,
UNBLOCK_NAVIGATION,
Expand Down
17 changes: 5 additions & 12 deletions src/common/hooks/useFetchSearchData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback } from 'react';
import { useSetRecoilState, useRecoilState, SetterOrUpdater, useResetRecoilState } from 'recoil';
import { useSetRecoilState, useRecoilState, useResetRecoilState } from 'recoil';
import { getByIdentifier } from '@common/api/search.api';
import { SearchIdentifiers, SearchSegment } from '@common/constants/search.constants';
import { SearchableIndexQuerySelector } from '@common/constants/complexLookup.constants';
Expand All @@ -9,6 +9,7 @@ import { normalizeLccn } from '@common/helpers/validations.helper';
import { UserNotificationFactory } from '@common/services/userNotification';
import state from '@state';
import { useSearchContext } from './useSearchContext';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

export const useFetchSearchData = () => {
const {
Expand All @@ -29,8 +30,7 @@ export const useFetchSearchData = () => {
const [data, setData] = useRecoilState(state.search.data);
const resetData = useResetRecoilState(state.search.data);
const setPageMetadata = useSetRecoilState(state.search.pageMetadata);
const setStatusMessages = useSetRecoilState(state.status.commonMessages);
const resetStatusMessage = useResetRecoilState(state.status.commonMessages);
const { addStatusMessages, resetStatusMessages } = useStoreSelector().status;

const validateAndNormalizeQuery = useCallback(
(type: SearchIdentifiers, query: string) => {
Expand Down Expand Up @@ -130,13 +130,6 @@ export const useFetchSearchData = () => {
});
};

const handleFetchError = (setStatusMessages: SetterOrUpdater<StatusEntry[]>) => {
setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'),
]);
};

const fetchData = useCallback(
async ({
query,
Expand All @@ -145,7 +138,7 @@ export const useFetchSearchData = () => {
selectedSegment,
baseQuerySelector = SearchableIndexQuerySelector.Query,
}: FetchDataParams) => {
resetStatusMessage();
resetStatusMessages();
const selectedNavigationSegment = selectedSegment ?? navigationSegment?.value;

data && resetData();
Expand Down Expand Up @@ -189,7 +182,7 @@ export const useFetchSearchData = () => {
setData(content);
setPageMetadata({ totalPages, totalElements: totalRecords, prev, next });
} catch {
handleFetchError(setStatusMessages);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'));
} finally {
setIsLoading(false);
}
Expand Down
16 changes: 6 additions & 10 deletions src/common/hooks/useMarcData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { RecoilState, useResetRecoilState, useSetRecoilState } from 'recoil';
import { useSetRecoilState } from 'recoil';
import { getMarcRecord } from '@common/api/records.api';
import { StatusType } from '@common/constants/status.constants';
import { UserNotificationFactory } from '@common/services/userNotification';
import state from '@state';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

export const useMarcData = <T>(marcState: RecoilState<T>) => {
const setMarcPreviewData = useSetRecoilState(marcState);
const clearMarcData = useResetRecoilState(marcState);
export const useMarcData = (setMarcPreviewData: (value: any) => void) => {
const setIsLoading = useSetRecoilState(state.loadingState.isLoading);
const setStatus = useSetRecoilState(state.status.commonMessages);
const { addStatusMessages } = useStoreSelector().status;

const fetchMarcData = async (recordId?: string, endpointUrl?: string): Promise<MarcDTO | undefined> => {
if (!recordId) return undefined;
Expand All @@ -22,16 +21,13 @@ export const useMarcData = <T>(marcState: RecoilState<T>) => {

setMarcPreviewData(marcData);
} catch (error) {
setStatus(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.cantLoadMarc'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.cantLoadMarc'));
} finally {
setIsLoading(false);
}

return marcData;
};

return { fetchMarcData, clearMarcData };
return { fetchMarcData };
};
10 changes: 4 additions & 6 deletions src/common/hooks/useProcessedRecordAndSchema.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { UserNotificationFactory } from '@common/services/userNotification';
import { StatusType } from '@common/constants/status.constants';
import state from '@state';
import { useServicesContext } from './useServicesContext';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

type IGetProcessedRecordAndSchema = {
baseSchema: Schema;
Expand All @@ -22,7 +23,7 @@ type IGetProcessedRecordAndSchema = {

export const useProcessedRecordAndSchema = () => {
const setRecord = useSetRecoilState(state.inputs.record);
const setStatusMessages = useSetRecoilState(state.status.commonMessages);
const { addStatusMessages } = useStoreSelector().status;
const { formatMessage } = useIntl();
const { userValuesService, schemaWithDuplicatesService, recordNormalizingService, recordToSchemaMappingService } =
useServicesContext() as Required<ServicesParams>;
Expand Down Expand Up @@ -72,10 +73,7 @@ export const useProcessedRecordAndSchema = () => {
} catch (error) {
console.error(error);

setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorLoadingResource'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.errorLoadingResource'));
}

return {
Expand All @@ -90,7 +88,7 @@ export const useProcessedRecordAndSchema = () => {
recordToSchemaMappingService,
schemaWithDuplicatesService,
setRecord,
setStatusMessages,
addStatusMessages,
userValuesService,
],
);
Expand Down
3 changes: 2 additions & 1 deletion src/common/hooks/useProfileSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { useRecoilState, useSetRecoilState } from 'recoil';
import state from '@state';
import { useServicesContext } from './useServicesContext';
import { deleteFromSetImmutable } from '@common/helpers/common.helper';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

export const useProfileSchema = () => {
const { selectedEntriesService, schemaWithDuplicatesService } = useServicesContext() as Required<ServicesParams>;
const [schema, setSchema] = useRecoilState(state.config.schema);
const setSelectedEntries = useSetRecoilState(state.config.selectedEntries);
const setCollapsibleEntries = useSetRecoilState(state.ui.collapsibleEntries);
const setIsEdited = useSetRecoilState(state.status.recordIsEdited);
const { setIsEditedRecord: setIsEdited } = useStoreSelector().status;
const setUserValues = useSetRecoilState(state.inputs.userValues);

const getSchemaWithCopiedEntries = (entry: SchemaEntry, selectedEntries: string[]) => {
Expand Down
46 changes: 12 additions & 34 deletions src/common/hooks/useRecordControls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useContainerEvents } from './useContainerEvents';
import { ApiErrorCodes, ExternalResourceIdType } from '@common/constants/api.constants';
import { checkHasErrorOfCodeType } from '@common/helpers/api.helper';
import { useRecordGeneration } from './useRecordGeneration';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

type SaveRecordProps = {
asRefToNewRecord?: boolean;
Expand All @@ -52,10 +53,7 @@ export const useRecordControls = () => {
const setUserValues = useSetRecoilState(state.inputs.userValues);
const setSelectedProfile = useSetRecoilState(state.config.selectedProfile);
const [record, setRecord] = useRecoilState(state.inputs.record);
const setIsEdited = useSetRecoilState(state.status.recordIsEdited);
const setRecordStatus = useSetRecoilState(state.status.recordStatus);
const setLastSavedRecordId = useSetRecoilState(state.status.lastSavedRecordId);
const setStatusMessages = useSetRecoilState(state.status.commonMessages);
const { setRecordStatus, setLastSavedRecordId, setIsEditedRecord: setIsEdited, addStatusMessages } = useStoreSelector().status;
const setCurrentlyEditedEntityBfid = useSetRecoilState(state.ui.currentlyEditedEntityBfid);
const setCurrentlyPreviewedEntityBfid = useSetRecoilState(state.ui.currentlyPreviewedEntityBfid);
const [selectedRecordBlocks, setSelectedRecordBlocks] = useRecoilState(state.inputs.selectedRecordBlocks);
Expand Down Expand Up @@ -130,10 +128,9 @@ export const useRecordControls = () => {
dispatchUnblockEvent();
!asRefToNewRecord && setRecord(parsedResponse);

setStatusMessages(currentStatus => [
...currentStatus,
addStatusMessages(
UserNotificationFactory.createMessage(StatusType.success, recordId ? 'ld.rdUpdateSuccess' : 'ld.rdSaveSuccess'),
]);
);

// isEdited state update is not immediately reflected in the <Prompt />
// blocker component, forcing <Prompt /> to block the navigation call below
Expand Down Expand Up @@ -178,10 +175,7 @@ export const useRecordControls = () => {
} catch (error) {
console.error('Cannot save the resource description', error);

setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.cantSaveRd'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.cantSaveRd'));
} finally {
setIsLoading(false);
}
Expand Down Expand Up @@ -217,19 +211,13 @@ export const useRecordControls = () => {
await deleteRecordRequest(currentRecordId as unknown as string);
deleteRecordLocally(profile, currentRecordId as unknown as string);
discardRecord();
setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.success, 'ld.rdDeleted'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.success, 'ld.rdDeleted'));

navigate(ROUTES.SEARCH.uri);
} catch (error) {
console.error('Cannot delete the resource description', error);

setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.cantDeleteRd'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.cantDeleteRd'));
}
};

Expand All @@ -240,10 +228,7 @@ export const useRecordControls = () => {
const contents = record?.resource?.[uriSelector];

if (!contents) {
setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.cantSelectReferenceContents'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.cantSelectReferenceContents'));

return navigate(ROUTES.RESOURCE_CREATE.uri);
}
Expand All @@ -263,10 +248,7 @@ export const useRecordControls = () => {
} catch (e) {
console.error('Error fetching record and selecting entity values: ', e);

setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'));
}
};

Expand All @@ -283,10 +265,7 @@ export const useRecordControls = () => {

return recordData;
} catch (_err) {
setStatusMessages(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, errorMessage ?? 'ld.errorFetching'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, errorMessage ?? 'ld.errorFetching'));
}
};

Expand All @@ -313,10 +292,9 @@ export const useRecordControls = () => {
if (checkHasErrorOfCodeType(err as ApiError, ApiErrorCodes.AlreadyExists)) {
setIsDuplicateImportedResourceModalOpen(true);
} else {
setStatusMessages(currentStatus => [
...currentStatus,
addStatusMessages(
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetchingExternalResourceForEditing'),
]);
);
}
} finally {
setIsLoading(false);
Expand Down
5 changes: 2 additions & 3 deletions src/common/hooks/useRecordStatus.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import state from '@state';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';
import { useParams } from 'react-router-dom';
import { useRecoilValue } from 'recoil';

export const useRecordStatus = () => {
const lastSavedRecordId = useRecoilValue(state.status.lastSavedRecordId);
const { lastSavedRecordId } = useStoreSelector().status;
const { resourceId } = useParams();

const hasBeenSaved = resourceId && resourceId === lastSavedRecordId;
Expand Down
7 changes: 3 additions & 4 deletions src/common/hooks/useResetRecordStatus.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { RecordStatus } from '@common/constants/record.constants';
import state from '@state';
import { useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import { useRecoilState } from 'recoil';
import { RecordStatus } from '@common/constants/record.constants';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

export const useResetRecordStatus = () => {
const [recordStatus, setRecordStatus] = useRecoilState(state.status.recordStatus);
const { recordStatus, setRecordStatus } = useStoreSelector().status;
const [prevResourceId, setPrevResourceId] = useState<string | null>(null);
const { resourceId } = useParams();
const setRecordStatusAsOpen = () => setRecordStatus({ type: RecordStatus.open });
Expand Down
13 changes: 4 additions & 9 deletions src/common/hooks/useSearchFiltersData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { StatusType } from '@common/constants/status.constants';
import { UserNotificationFactory } from '@common/services/userNotification';
import * as SearchApi from '@common/api/search.api';
import state from '@state';
import { useStoreSelector } from '@common/hooks/useStoreSelectors';

const DEFAULT_SEARCH_SOURCE_LIMIT = '50';
const DEFAULT_SEARCH_FACETS_QUERY = 'id=*';
Expand All @@ -13,7 +14,7 @@ export const useSearchFiltersData = () => {
const resetSelectedFacetsGroups = useResetRecoilState(state.search.selectedFacetsGroups);
const setFacetsData = useSetRecoilState(state.search.facetsData);
const setSourceData = useSetRecoilState(state.search.sourceData);
const setCommonStatus = useSetRecoilState(state.status.commonMessages);
const { addStatusMessages } = useStoreSelector().status;

useEffect(() => {
return resetSelectedFacetsGroups();
Expand All @@ -40,10 +41,7 @@ export const useSearchFiltersData = () => {
} catch (error) {
console.error(error);

setCommonStatus(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'));
}
};

Expand All @@ -62,10 +60,7 @@ export const useSearchFiltersData = () => {
} catch (error) {
console.error(error);

setCommonStatus(currentStatus => [
...currentStatus,
UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'),
]);
addStatusMessages(UserNotificationFactory.createMessage(StatusType.error, 'ld.errorFetching'));
}
};

Expand Down
Loading

0 comments on commit f821afa

Please sign in to comment.