Skip to content

Commit 6e18178

Browse files
committed
feat(navigation): allow updating views
Signed-off-by: skjnldsv <skjnldsv@protonmail.com>
1 parent bc04277 commit 6e18178

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

__tests__/view.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,45 @@ describe('View creation', () => {
201201
await expect(view.loadChildViews?.({} as unknown as View)).resolves.toBe(undefined)
202202
})
203203
})
204+
205+
describe('View update', () => {
206+
test('Update a View', () => {
207+
const view = new View({
208+
id: 'test',
209+
name: 'Test',
210+
caption: 'Test caption',
211+
emptyTitle: 'Test empty title',
212+
emptyCaption: 'Test empty caption',
213+
getContents: () => Promise.reject(new Error()),
214+
hidden: true,
215+
icon: '<svg></svg>',
216+
order: 1,
217+
params: {},
218+
columns: [],
219+
emptyView: () => {},
220+
parent: 'parent',
221+
sticky: false,
222+
expanded: false,
223+
defaultSortKey: 'key',
224+
loadChildViews: async () => {},
225+
})
226+
227+
view.update({
228+
name: 'Updated Test',
229+
order: 2,
230+
icon: '<svg>updated</svg>',
231+
caption: 'Updated caption',
232+
emptyTitle: 'Updated empty title',
233+
emptyCaption: 'Updated empty caption',
234+
})
235+
236+
expect(view.name).toBe('Updated Test')
237+
expect(view.order).toBe(2)
238+
expect(view.icon).toBe('<svg>updated</svg>')
239+
expect(view.caption).toBe('Updated caption')
240+
expect(view.emptyTitle).toBe('Updated empty title')
241+
expect(view.emptyCaption).toBe('Updated empty caption')
242+
243+
expect(() => view.update({ id: 'new-id' })).toThrowError('Cannot change the view id after creation')
244+
})
245+
})

lib/navigation/view.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ export class View implements ViewData {
181181
return this._view.loadChildViews
182182
}
183183

184+
/**
185+
* Allows to update the view data.
186+
* This will throw an error if the view is not valid.
187+
*/
188+
update(view: Partial<ViewData>) {
189+
if (view.id && view.id !== this._view.id) {
190+
throw new Error('Cannot change the view id after creation')
191+
}
192+
193+
isValidView({ ...this._view, ...view })
194+
this._view = { ...this._view, ...view }
195+
}
196+
184197
}
185198

186199
/**

0 commit comments

Comments
 (0)