-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathAppContentLibrary.tsx
130 lines (116 loc) · 4.46 KB
/
AppContentLibrary.tsx
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type {
CodeListData,
CodeListReference,
CodeListWithMetadata,
} from '@studio/content-library';
import { ResourceContentLibraryImpl } from '@studio/content-library';
import type { ReactElement } from 'react';
import React, { useCallback } from 'react';
import { useOptionListsQuery, useOptionListsReferencesQuery } from 'app-shared/hooks/queries';
import { useStudioEnvironmentParams } from 'app-shared/hooks/useStudioEnvironmentParams';
import { mapToCodeListDataList } from './utils/mapToCodeListDataList';
import { StudioPageError, StudioPageSpinner } from '@studio/components';
import { useTranslation } from 'react-i18next';
import type { ApiError } from 'app-shared/types/api/ApiError';
import { toast } from 'react-toastify';
import type { AxiosError } from 'axios';
import { isErrorUnknown } from 'app-shared/utils/ApiErrorUtils';
import {
useAddOptionListMutation,
useUpdateOptionListMutation,
useUpdateOptionListIdMutation,
useDeleteOptionListMutation,
} from 'app-shared/hooks/mutations';
import { mapToCodeListUsages } from './utils/mapToCodeListUsages';
import type { OptionListData } from 'app-shared/types/OptionList';
import type { OptionListReferences } from 'app-shared/types/OptionListReferences';
import { mergeQueryStatuses } from 'app-shared/utils/tanstackQueryUtils';
export function AppContentLibrary(): React.ReactElement {
const { org, app } = useStudioEnvironmentParams();
const { t } = useTranslation();
const { data: optionListDataList, status: optionListDataListStatus } = useOptionListsQuery(
org,
app,
);
const { data: optionListUsages, status: optionListUsagesStatus } = useOptionListsReferencesQuery(
org,
app,
);
const status = mergeQueryStatuses(optionListDataListStatus, optionListUsagesStatus);
switch (status) {
case 'pending':
return <StudioPageSpinner spinnerTitle={t('general.loading')} />;
case 'error':
return <StudioPageError message={t('app_content_library.fetch_error')} />;
case 'success':
return (
<AppContentLibraryWithData
optionListDataList={optionListDataList}
optionListUsages={optionListUsages}
/>
);
}
}
type AppContentLibraryWithDataProps = {
optionListDataList: OptionListData[];
optionListUsages: OptionListReferences;
};
function AppContentLibraryWithData({
optionListDataList,
optionListUsages,
}: AppContentLibraryWithDataProps): ReactElement {
const { org, app } = useStudioEnvironmentParams();
const { mutate: updateOptionList } = useUpdateOptionListMutation(org, app);
const { mutate: updateOptionListId } = useUpdateOptionListIdMutation(org, app);
const { mutate: deleteOptionList } = useDeleteOptionListMutation(org, app);
const handleUpload = useUploadOptionList(org, app);
const codeListDataList: CodeListData[] = mapToCodeListDataList(optionListDataList);
const codeListsUsages: CodeListReference[] = mapToCodeListUsages(optionListUsages);
const handleUpdateCodeListId = (optionListId: string, newOptionListId: string): void => {
updateOptionListId({ optionListId, newOptionListId });
};
const handleUpdate = ({ title, codeList }: CodeListWithMetadata): void => {
updateOptionList({ optionListId: title, optionList: codeList });
};
const { getContentResourceLibrary } = new ResourceContentLibraryImpl({
pages: {
codeList: {
props: {
codeListsData: codeListDataList,
onDeleteCodeList: deleteOptionList,
onUpdateCodeListId: handleUpdateCodeListId,
onUpdateCodeList: handleUpdate,
onUploadCodeList: handleUpload,
codeListsUsages,
},
},
images: {
props: {
images: [],
onUpdateImage: () => {},
},
},
},
});
return <div>{getContentResourceLibrary()}</div>;
}
function useUploadOptionList(org: string, app: string): (file: File) => void {
const { mutate: uploadOptionList } = useAddOptionListMutation(org, app, {
hideDefaultError: (error: AxiosError<ApiError>) => isErrorUnknown(error),
});
const { t } = useTranslation();
return useCallback(
(file: File) =>
uploadOptionList(file, {
onSuccess: () => {
toast.success(t('ux_editor.modal_properties_code_list_upload_success'));
},
onError: (error: AxiosError<ApiError>) => {
if (isErrorUnknown(error)) {
toast.error(t('ux_editor.modal_properties_code_list_upload_generic_error'));
}
},
}),
[uploadOptionList, t],
);
}