Skip to content

Commit d4ae1b6

Browse files
Conrad Chanmergify[bot]
andauthored
feat(init): Seed the initial active annotation id (#461)
* feat(init): Seed the initial active annotation id * chore: add unit tests * chore: adding some ?? * chore: remove default value for fileOptions * chore: pr comments * chore: remove unused type Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent ffbadaf commit d4ae1b6

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/common/BaseAnnotator.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import './BaseAnnotator.scss';
99

1010
export type Container = string | HTMLElement;
1111

12+
export type FileOptions = {
13+
[key: string]: {
14+
annotations?: {
15+
activeId?: string;
16+
};
17+
};
18+
};
19+
1220
export type Options = {
1321
apiHost: string;
1422
container: Container;
@@ -19,6 +27,7 @@ export type Options = {
1927
};
2028
permissions: Permissions;
2129
};
30+
fileOptions?: FileOptions;
2231
hasTouch?: boolean;
2332
intl: IntlOptions;
2433
locale?: string;
@@ -36,8 +45,12 @@ export default class BaseAnnotator {
3645

3746
store: store.AppStore;
3847

39-
constructor({ apiHost, container, file, intl, token }: Options) {
48+
constructor({ apiHost, container, file, fileOptions, intl, token }: Options) {
49+
const activeId = fileOptions?.[file.id]?.annotations?.activeId ?? null;
4050
const initialState = {
51+
annotations: {
52+
activeId,
53+
},
4154
options: {
4255
fileId: file.id,
4356
fileVersionId: file.file_version.id,

src/common/__tests__/BaseAnnotator-test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BaseAnnotator from '../BaseAnnotator';
44
import eventManager from '../EventManager';
55
import { ANNOTATOR_EVENT } from '../../constants';
66
import { Mode } from '../../store/common';
7+
import APIFactory from '../../api';
78

89
jest.mock('../EventManager');
910
jest.mock('../../api');
@@ -47,6 +48,32 @@ describe('BaseAnnotator', () => {
4748
}
4849
});
4950

51+
describe('constructor()', () => {
52+
test.each`
53+
optionsFileId | expectedActiveId
54+
${'12345'} | ${'123'}
55+
${'987'} | ${null}
56+
`(
57+
'should parse fileOptions for initial activeId and set it to $expectedActiveId',
58+
({ optionsFileId, expectedActiveId }) => {
59+
const fileOptions = {
60+
[optionsFileId]: {
61+
annotations: { activeId: '123' },
62+
},
63+
};
64+
annotator = getAnnotator({ fileOptions });
65+
66+
expect(store.createStore).toHaveBeenLastCalledWith(
67+
{
68+
annotations: { activeId: expectedActiveId },
69+
options: { fileId: '12345', fileVersionId: '98765' },
70+
},
71+
{ api: expect.any(APIFactory) },
72+
);
73+
},
74+
);
75+
});
76+
5077
describe('destroy()', () => {
5178
test('should remove the base class name from the root element', () => {
5279
const rootEl = document.createElement('div');

src/store/createStore.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ export type Options = {
88
api?: API;
99
};
1010

11-
export default function createStore(preloadedState?: Partial<AppState>, { api }: Options = {}): AppStore {
11+
type RecursivePartial<T> = {
12+
[P in keyof T]?: RecursivePartial<T[P]>;
13+
};
14+
15+
export default function createStore(preloadedState?: RecursivePartial<AppState>, { api }: Options = {}): AppStore {
1216
const thunkOptions = {
1317
extraArgument: { api },
1418
};

0 commit comments

Comments
 (0)