Skip to content

Commit

Permalink
feat:had draft changed detection
Browse files Browse the repository at this point in the history
  • Loading branch information
cmoinier committed Dec 23, 2024
1 parent 8c98ae0 commit b891028
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
45 changes: 44 additions & 1 deletion libs/api/repository/src/lib/gn4/gn4-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
import { catchError, map, tap } from 'rxjs/operators'
import { lt } from 'semver'
import { ElasticsearchService } from './elasticsearch'
import { TranslateService } from '@ngx-translate/core'

const minPublicationApiVersion = '4.2.5'

Expand All @@ -59,7 +60,8 @@ export class Gn4Repository implements RecordsRepositoryInterface {
private gn4SearchHelper: ElasticsearchService,
private gn4Mapper: Gn4Converter,
private gn4RecordsApi: RecordsApiService,
private platformService: PlatformServiceInterface
private platformService: PlatformServiceInterface,
private translateService: TranslateService
) {}

search({
Expand Down Expand Up @@ -365,6 +367,47 @@ export class Gn4Repository implements RecordsRepositoryInterface {
return of(draftCount)
}

hasRecordChangedSinceDraft(localRecord: CatalogRecord) {
const isUnsaved = this.isRecordNotYetSaved(localRecord.uniqueIdentifier)
const hasDraft = this.recordHasDraft(localRecord.uniqueIdentifier)

if (isUnsaved || !hasDraft) {
return of([])
}

return combineLatest([
this.getAllDrafts().pipe(
map((drafts) => {
const matchingRecord = drafts.find(
(draft) => draft.uniqueIdentifier === localRecord.uniqueIdentifier
)
return matchingRecord ? matchingRecord.recordUpdated : null
})
),
this.getRecord(localRecord.uniqueIdentifier),
]).pipe(
map(([draftRecordUpdated, recentRecord]) => {
if (recentRecord.recordUpdated > draftRecordUpdated) {
return [
this.formatDate(recentRecord.recordUpdated),
recentRecord.extras?.['ownerInfo'].toString().split('|')[0],
]
}
return []
})
)
}

formatDate(date: Date): string {
return date.toLocaleDateString(this.translateService.currentLang, {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
})
}

private getRecordAsXml(uniqueIdentifier: string): Observable<string | null> {
return this.gn4RecordsApi
.getRecordAs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,7 @@ export abstract class RecordsRepositoryInterface {
abstract getAllDrafts(): Observable<CatalogRecord[]>
abstract getDraftsCount(): Observable<number>
abstract draftsChanged$: Observable<void>
abstract hasRecordChangedSinceDraft(
localRecord: CatalogRecord
): Observable<string[]>
}
10 changes: 10 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@ export const setFieldVisibility = createAction(
'[Editor] Set field visibility',
props<{ field: EditorFieldIdentification; visible: boolean }>()
)

export const hasRecordChangedSinceDraft = createAction(
'[Editor] Has Record Changed Since Draft',
props<{ record: CatalogRecord }>()
)

export const hasRecordChangedSinceDraftSuccess = createAction(
'[Editor] Has Record Changed Since Draft Success',
props<{ changes: any[] }>()
)
15 changes: 15 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,19 @@ export class EditorEffects {
map(() => EditorActions.markRecordAsChanged())
)
)

hasRecordChangedSinceDraft$ = createEffect(() =>
this.actions$.pipe(
ofType(EditorActions.hasRecordChangedSinceDraft),
switchMap(({ record }) =>
this.editorService
.hasRecordChangedSinceDraft(record)
.pipe(
map((changes) =>
EditorActions.hasRecordChangedSinceDraftSuccess({ changes })
)
)
)
)
)
}
7 changes: 7 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class EditorFacade {
draftSaveSuccess$ = this.actions$.pipe(ofType(EditorActions.draftSaveSuccess))
currentPage$ = this.store.pipe(select(EditorSelectors.selectCurrentPage))
editorConfig$ = this.store.pipe(select(EditorSelectors.selectEditorConfig))
hasRecordChanged$ = this.store.pipe(
select(EditorSelectors.selectHasRecordChanged)
)

openRecord(
record: CatalogRecord,
Expand Down Expand Up @@ -63,4 +66,8 @@ export class EditorFacade {
setFieldVisibility(field: EditorFieldIdentification, visible: boolean) {
this.store.dispatch(EditorActions.setFieldVisibility({ field, visible }))
}

hasRecordChangedSinceDraft(record: CatalogRecord) {
this.store.dispatch(EditorActions.hasRecordChangedSinceDraft({ record }))
}
}
6 changes: 6 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface EditorState {
changedSinceSave: boolean
editorConfig: EditorConfig
currentPage: number
hasRecordChanged: any[]
}

export interface EditorPartialState {
Expand All @@ -39,6 +40,7 @@ export const initialEditorState: EditorState = {
changedSinceSave: false,
editorConfig: DEFAULT_CONFIGURATION,
currentPage: 0,
hasRecordChanged: [],
}

const reducer = createReducer(
Expand Down Expand Up @@ -104,6 +106,10 @@ const reducer = createReducer(
})),
})),
},
})),
on(EditorActions.hasRecordChangedSinceDraftSuccess, (state, { changes }) => ({
...state,
hasRecordChanged: changes,
}))
)

Expand Down
5 changes: 5 additions & 0 deletions libs/feature/editor/src/lib/+state/editor.selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ export const selectRecordSections = createSelector(
})) as EditorSectionWithValues[]
}
)

export const selectHasRecordChanged = createSelector(
selectEditorState,
(state: EditorState) => state.hasRecordChanged
)
5 changes: 5 additions & 0 deletions libs/feature/editor/src/lib/services/editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export class EditorService {
record: CatalogRecord,
recordSource: string
): Observable<void> {
record.recordUpdated = new Date()
return this.recordsRepository
.saveRecordAsDraft(record, recordSource)
.pipe(map(() => undefined))
Expand All @@ -70,4 +71,8 @@ export class EditorService {
this.recordsRepository.clearRecordDraft(record.uniqueIdentifier)
return this.recordsRepository.openRecordForEdition(record.uniqueIdentifier)
}

hasRecordChangedSinceDraft(localRecord: CatalogRecord): Observable<string[]> {
return this.recordsRepository.hasRecordChangedSinceDraft(localRecord)
}
}

0 comments on commit b891028

Please sign in to comment.