Skip to content

Commit

Permalink
chore(repository): make draft-related methods synchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Jun 10, 2024
1 parent 477860c commit 6e10265
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 27 deletions.
25 changes: 11 additions & 14 deletions libs/api/repository/src/lib/gn4/gn4-repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,35 +404,32 @@ describe('Gn4Repository', () => {
})
})
describe('#clearRecordDraft', () => {
beforeEach(async () => {
await lastValueFrom(
repository.clearRecordDraft(DATASET_RECORD_SIMPLE.uniqueIdentifier)
)
beforeEach(() => {
repository.clearRecordDraft(DATASET_RECORD_SIMPLE.uniqueIdentifier)
})
it('removes the record draft', async () => {
const record = await lastValueFrom(
const [record] = await lastValueFrom(
repository.openRecordForEdition(
DATASET_RECORD_SIMPLE.uniqueIdentifier
)
)
expect(record?.title).not.toBe('The title has been modified')
const hasDraft = await lastValueFrom(
repository.recordHasDraft(DATASET_RECORD_SIMPLE.uniqueIdentifier)
const hasDraft = repository.recordHasDraft(
DATASET_RECORD_SIMPLE.uniqueIdentifier
)

expect(hasDraft).toBe(false)
})
})
describe('#recordHasDraft', () => {
it('returns true when there is a draft', async () => {
const hasDraft = await lastValueFrom(
repository.recordHasDraft(DATASET_RECORD_SIMPLE.uniqueIdentifier)
it('returns true when there is a draft', () => {
const hasDraft = repository.recordHasDraft(
DATASET_RECORD_SIMPLE.uniqueIdentifier
)
expect(hasDraft).toBe(true)
})
it('returns false otherwise', async () => {
const hasDraft = await lastValueFrom(
repository.recordHasDraft('blargz')
)
it('returns false otherwise', () => {
const hasDraft = repository.recordHasDraft('blargz')
expect(hasDraft).toBe(false)
})
})
Expand Down
7 changes: 3 additions & 4 deletions libs/api/repository/src/lib/gn4/gn4-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,14 @@ export class Gn4Repository implements RecordsRepositoryInterface {
)
}

clearRecordDraft(uniqueIdentifier: string): Observable<void> {
clearRecordDraft(uniqueIdentifier: string): void {
window.localStorage.removeItem(
this.getLocalStorageKeyForRecord(uniqueIdentifier)
)
return of(undefined)
}

recordHasDraft(uniqueIdentifier: string): Observable<boolean> {
return of(
recordHasDraft(uniqueIdentifier: string): boolean {
return (
window.localStorage.getItem(
this.getLocalStorageKeyForRecord(uniqueIdentifier)
) !== null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ export abstract class RecordsRepositoryInterface {
referenceRecordSource?: string
): Observable<string>

abstract clearRecordDraft(uniqueIdentifier: string): Observable<void>
abstract recordHasDraft(uniqueIdentifier: string): Observable<boolean>
abstract clearRecordDraft(uniqueIdentifier: string): void
abstract recordHasDraft(uniqueIdentifier: string): boolean
}
4 changes: 2 additions & 2 deletions libs/feature/editor/src/lib/+state/editor.effects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class EditorServiceMock {
saveRecordAsDraft = jest.fn(() => of('<xml>blabla</xml>'))
}
class RecordsRepositoryMock {
recordHasDraft = jest.fn(() => of(true))
recordHasDraft = jest.fn(() => true)
}

describe('EditorEffects', () => {
Expand Down Expand Up @@ -157,7 +157,7 @@ describe('EditorEffects', () => {
beforeEach(() => {
;(
TestBed.inject(RecordsRepositoryInterface).recordHasDraft as jest.Mock
).mockImplementationOnce(() => of(false))
).mockImplementationOnce(() => false)
})
it('dispatches nothing', () => {
actions = hot('-a-|', {
Expand Down
2 changes: 1 addition & 1 deletion libs/feature/editor/src/lib/+state/editor.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class EditorEffects {
checkHasChangesOnOpen$ = createEffect(() =>
this.actions$.pipe(
ofType(EditorActions.openRecord),
switchMap(({ record }) =>
map(({ record }) =>
this.recordsRepository.recordHasDraft(record.uniqueIdentifier)
),
filter((hasDraft) => hasDraft),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div class="flex flex-row items-center gap-2 max-w-full">
<span class="overflow-hidden text-ellipsis">{{ item.title }}</span>
<gn-ui-badge
*ngIf="hasDraft(item) | async"
*ngIf="hasDraft(item)"
[style.--gn-ui-badge-padding]="'0.4em 0.6em'"
[style.--gn-ui-badge-text-color]="'#3d2006'"
[style.--gn-ui-badge-background-color]="'#ffbc7b'"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DATASET_RECORDS } from '@geonetwork-ui/common/fixtures'
import { ResultsTableComponent } from './results-table.component'
import { CatalogRecord } from '@geonetwork-ui/common/domain/model/record'
import { By } from '@angular/platform-browser'
import { BehaviorSubject, firstValueFrom, of } from 'rxjs'
import { BehaviorSubject, firstValueFrom } from 'rxjs'
import { SearchFacade } from '../state/search.facade'
import { SearchService } from '../utils/service/search.service'
import { SelectionService } from '@geonetwork-ui/api/repository'
Expand All @@ -28,7 +28,7 @@ class SelectionServiceMock {
selectedRecordsIdentifiers$ = new BehaviorSubject([])
}
class RecordsRepositoryMock {
recordHasDraft = jest.fn(() => of(false))
recordHasDraft = jest.fn(() => false)
}

describe('ResultsTableComponent', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class ResultsTableComponent {
)
}

hasDraft(record: CatalogRecord): Observable<boolean> {
hasDraft(record: CatalogRecord): boolean {
return this.recordsRepository.recordHasDraft(record.uniqueIdentifier)
}
}

0 comments on commit 6e10265

Please sign in to comment.