-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathuseUpdateUserLayoutPreferences.ts
More file actions
87 lines (77 loc) · 2.73 KB
/
Copy pathuseUpdateUserLayoutPreferences.ts
File metadata and controls
87 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import { useMutation } from '@tanstack/react-query';
import fetch from 'logic/rest/FetchProvider';
import { qualifyUrl } from 'util/URLUtils';
import type {
SlicingPreferences,
SlicingPreferencesJSON,
TableLayoutPreferences,
TableLayoutPreferencesJSON,
} from 'components/common/EntityDataTable/types';
import UserNotification from 'util/UserNotification';
import useUserLayoutPreferences from 'components/common/EntityDataTable/hooks/useUserLayoutPreferences';
const slicingToJSON = (slicing?: SlicingPreferences | null): SlicingPreferencesJSON | null | undefined => {
if (slicing === null) {
return null;
}
if (!slicing) {
return undefined;
}
return {
slice_column: slicing.sliceColumn,
sort_by: slicing.sortBy,
order: slicing.order,
};
};
const preferencesToJSON = <T>({
attributes,
sort,
perPage,
slicing,
customPreferences,
order,
}: TableLayoutPreferences<T>): TableLayoutPreferencesJSON<T> => ({
attributes,
sort: sort ? { order: sort.direction, field: sort.attributeId } : undefined,
per_page: perPage,
slicing: slicingToJSON(slicing),
custom_preferences: customPreferences,
order,
});
const preferencesUrl = (entityTableId: string, layoutVariant?: string) => {
const params = layoutVariant ? `?layout_variant=${encodeURIComponent(layoutVariant)}` : '';
return qualifyUrl(`/entitylists/preferences/${entityTableId}${params}`);
};
const useUpdateUserLayoutPreferences = <T>(entityTableId: string, layoutVariant?: string) => {
const { data: userLayoutPreferences = {}, refetch } = useUserLayoutPreferences(entityTableId, layoutVariant);
const mutationFn = (newPreferences: TableLayoutPreferences<T>) =>
fetch(
'POST',
preferencesUrl(entityTableId, layoutVariant),
preferencesToJSON({ ...userLayoutPreferences, ...newPreferences }),
);
const { mutateAsync } = useMutation({
mutationFn,
onError: (error) => {
UserNotification.error(`Updating table layout preferences failed with error: ${error}`);
},
onSuccess: () => refetch(),
});
return { mutateAsync };
};
export default useUpdateUserLayoutPreferences;