Skip to content

Commit ef07145

Browse files
committed
Add Enums for document status and file type
1 parent fd0e37b commit ef07145

File tree

4 files changed

+120
-59
lines changed

4 files changed

+120
-59
lines changed

src/views/ContentManagement/ContentActions/index.tsx

+7-9
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ const RETRIGGER_CONTENT = gql`
5757
`;
5858

5959
interface ContentActionsProps {
60-
contentId: number;
60+
id: number;
6161
}
6262

63-
function ContentActions({ contentId }: ContentActionsProps) {
63+
function ContentActions({ id }: ContentActionsProps) {
6464
const alert = useAlert();
6565
const [showEditContentModal, {
6666
setTrue: setShowEditContentModalTrue,
@@ -121,26 +121,24 @@ function ContentActions({ contentId }: ContentActionsProps) {
121121
},
122122
},
123123
);
124-
125124
const handleArchiveContent = useCallback(() => {
126125
triggerArchiveContent({
127126
variables: {
128127
input: {
129-
content: contentId,
128+
content: id,
130129
} as unknown as ArchiveContentInput,
131130
},
132131
});
133-
}, [triggerArchiveContent, contentId]);
134-
132+
}, [triggerArchiveContent, id]);
135133
const handleRetriggerContent = useCallback(() => {
136134
retriggerContent({
137135
variables: {
138136
input: {
139-
content: contentId,
137+
content: id,
140138
} as unknown as RetriggerContentInput,
141139
},
142140
});
143-
}, [retriggerContent, contentId]);
141+
}, [retriggerContent, id]);
144142

145143
return (
146144
<div className={styles.contentActions}>
@@ -171,7 +169,7 @@ function ContentActions({ contentId }: ContentActionsProps) {
171169
{showEditContentModal && (
172170
<EditContentModal
173171
onClose={setShowEditContentModalFalse}
174-
contentId={contentId}
172+
id={id}
175173
/>
176174
)}
177175
</div>

src/views/ContentManagement/EditContent/index.tsx

+27-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import {
66
import {
77
createSubmitHandler,
88
getErrorObject,
9+
nonFieldError,
910
ObjectSchema,
11+
removeNull,
1012
requiredStringCondition,
1113
useForm,
1214
} from '@togglecorp/toggle-form';
@@ -22,6 +24,7 @@ import {
2224
UpdateContentTitleMutationVariables,
2325
} from '#generated/types/graphql';
2426
import useAlert from '#hooks/useAlert';
27+
import { transformToFormError } from '#utils/errorTransform';
2528

2629
import styles from './styles.module.css';
2730

@@ -53,14 +56,14 @@ const defaultFormValues: PartialFormType = {};
5356

5457
interface Props {
5558
onClose: () => void;
56-
contentId: number;
59+
id: number;
5760
}
5861

5962
function EditContentModal(props: Props) {
6063
const alert = useAlert();
6164
const {
6265
onClose,
63-
contentId,
66+
id,
6467
} = props;
6568

6669
const {
@@ -78,27 +81,31 @@ function EditContentModal(props: Props) {
7881
] = useMutation<UpdateContentTitleMutation, UpdateContentTitleMutationVariables>(
7982
UPDATE_CONTENT,
8083
{
81-
onCompleted: (projectResponse) => {
82-
const response = projectResponse?.private?.updateContentTitle;
83-
if (!response) {
84-
return;
85-
}
86-
if (response.ok) {
84+
onCompleted: (response) => {
85+
const { private: privateRes } = response;
86+
if (!privateRes) return;
87+
const { updateContentTitle: updateContentRes } = privateRes;
88+
if (!updateContentRes) return;
89+
const { errors, ok } = updateContentRes;
90+
91+
if (errors) {
92+
const formErrors = transformToFormError(removeNull(errors));
93+
setError(formErrors);
94+
const errorMessages = errors
95+
?.map((message: { messages: string; }) => message.messages)
96+
.filter((msg: string) => msg)
97+
.join(', ');
98+
alert.show(errorMessages);
99+
} else if (ok) {
100+
onClose();
87101
alert.show(
88102
'Updated Successfully',
89-
{
90-
variant: 'success',
91-
},
103+
{ variant: 'success' },
92104
);
93-
} else {
94-
const errorMessages = response?.errors
95-
?.map((error: { messages: string; }) => error.messages)
96-
.filter((message: string) => message)
97-
.join(', ');
98-
alert.show(errorMessages, { variant: 'danger' });
99105
}
100106
},
101-
onError: () => {
107+
onError: (error) => {
108+
setError({ [nonFieldError]: error.message });
102109
alert.show(
103110
'Failed to Update',
104111
{ variant: 'danger' },
@@ -112,11 +119,11 @@ function EditContentModal(props: Props) {
112119
variables: {
113120
input: {
114121
...finalValue,
115-
content: contentId,
122+
content: id,
116123
} as unknown as UpdateContentTitleInput,
117124
},
118125
});
119-
}, [updateContentTrigger, contentId]);
126+
}, [updateContentTrigger, id]);
120127

121128
const handleSubmit = useCallback(() => {
122129
createSubmitHandler(validate, setError, handleUpdateContentSubmit)();

src/views/ContentManagement/index.tsx

+81-30
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import {
2-
useMemo,
3-
useState,
4-
} from 'react';
1+
import { useMemo } from 'react';
52
import {
63
gql,
74
useQuery,
85
} from '@apollo/client';
96
import {
107
Button,
8+
Chip,
119
createDateColumn,
1210
createStringColumn,
1311
Pager,
@@ -17,9 +15,12 @@ import {
1715
import Container from '#components/Container';
1816
import { createElementColumn } from '#components/CreateElementColumn';
1917
import {
18+
ContentEnumsQuery,
19+
ContentEnumsQueryVariables,
2020
ContentListQuery,
2121
ContentListQueryVariables,
2222
} from '#generated/types/graphql';
23+
import useFilterState from '#hooks/useFilterState';
2324

2425
import ContentActions from './ContentActions';
2526

@@ -29,14 +30,14 @@ type ContentListTable = NonNullable<NonNullable<NonNullable<ContentListQuery['pr
2930

3031
const contentKeySelector = (option: ContentListTable) => option.id;
3132

32-
const PAGE_SIZE = 5;
33+
const PAGE_SIZE = 2;
3334

3435
const CREATE_CONTENT_QUERY = gql`
3536
query ContentList(
36-
$input: OffsetPaginationInput
37+
$pagination: OffsetPaginationInput,
3738
) {
3839
private {
39-
contents(pagination: $input) {
40+
contents(pagination: $pagination) {
4041
count
4142
items {
4243
createdAt
@@ -53,31 +54,61 @@ const CREATE_CONTENT_QUERY = gql`
5354
}
5455
`;
5556

57+
const CONTENT_ENUMS = gql`
58+
query ContentEnums {
59+
enums {
60+
ContentDocumentStatus {
61+
key
62+
label
63+
}
64+
ContentDocumentType {
65+
key
66+
label
67+
}
68+
}
69+
}
70+
`;
71+
72+
const statusVariant: Record<string, string> = {
73+
Pending: 'default',
74+
'Text extracted': 'default',
75+
'Added to vector': 'success',
76+
'Deleted from vector': 'warning',
77+
Failure: 'danger',
78+
};
5679
/** @knipignore */
5780
// eslint-disable-next-line import/prefer-default-export
5881
export function Component() {
59-
const [page, setPage] = useState<number>(1);
82+
const {
83+
page,
84+
setPage,
85+
} = useFilterState<{
86+
createdAtGte?: string;
87+
createdAtLte?: string;
88+
documentType?: string;
89+
documentStatus?: string ;
90+
}>({
91+
pageSize: PAGE_SIZE,
92+
filter: {},
93+
});
94+
6095
const {
6196
data: contentResult,
6297
} = useQuery<ContentListQuery, ContentListQueryVariables>(
6398
CREATE_CONTENT_QUERY,
64-
{
65-
variables: {
66-
input: {
67-
limit: PAGE_SIZE,
68-
offset: page,
69-
},
70-
},
71-
},
7299
);
73100

74-
const columns = useMemo(() => ([
75-
createStringColumn<ContentListTable, string>(
76-
'sn',
77-
'S.N',
78-
(item) => (item.id),
79-
),
101+
const {
102+
data: contentEnumsResponse,
103+
} = useQuery<ContentEnumsQuery, ContentEnumsQueryVariables>(
104+
CONTENT_ENUMS,
105+
);
106+
107+
const documentType = contentEnumsResponse?.enums.ContentDocumentType;
108+
109+
const documentStatus = contentEnumsResponse?.enums.ContentDocumentStatus;
80110

111+
const columns = useMemo(() => ([
81112
createStringColumn<ContentListTable, string>(
82113
'title',
83114
'Title',
@@ -93,30 +124,50 @@ export function Component() {
93124
createStringColumn<ContentListTable, string>(
94125
'documentTypeDisplay',
95126
'File Type',
96-
(item) => item.documentType,
127+
(item) => documentType?.find(
128+
(type) => type.key === item.documentType,
129+
)?.label,
97130
),
98131
createStringColumn<ContentListTable, string>(
99132
'tag',
100133
'Tag',
101134
(item) => item.tag.map((tag: { name: string; }) => tag.name).join(','),
102135
{ columnClassName: styles.actions },
103136
),
104-
createStringColumn<ContentListTable, string>(
137+
createElementColumn<ContentListTable, string,
138+
{ status: string | undefined; variant: string }>(
105139
'documentStatusDisplay',
106140
'Status',
107-
(item) => item.documentStatus,
141+
({ status }) => (
142+
<Chip
143+
className={styles.status}
144+
label={status}
145+
/>
146+
),
147+
(_key, item) => {
148+
const statusLabel = documentStatus?.find(
149+
(status) => status.key === item.documentStatus,
150+
)?.label;
151+
const variant = statusLabel ? statusVariant[statusLabel] : '';
152+
return {
153+
status: statusLabel,
154+
variant,
155+
};
156+
},
108157
{ columnClassName: styles.actions },
109158
),
110-
createElementColumn<ContentListTable, string, { contentId: number }>(
159+
createElementColumn<ContentListTable, string, { id: number }>(
111160
'actions',
112161
'Actions',
113162
ContentActions,
114163
(_key, datum) => ({
115-
contentId: Number(datum.id),
164+
id: Number(datum.id),
116165
}),
117166
{ columnClassName: styles.actions },
118167
),
119-
]), []);
168+
]), [documentType, documentStatus]);
169+
170+
const data = contentResult?.private.contents;
120171

121172
return (
122173
<Container
@@ -138,7 +189,7 @@ export function Component() {
138189
infoHidden
139190
itemsPerPageControlHidden
140191
activePage={page}
141-
itemsCount={contentResult?.private.contents.count ?? 0}
192+
itemsCount={data?.count ?? 0}
142193
maxItemsPerPage={PAGE_SIZE}
143194
onActivePageChange={setPage}
144195
/>
@@ -148,7 +199,7 @@ export function Component() {
148199
className={styles.table}
149200
headerCellClassName={styles.headerCell}
150201
headerRowClassName={styles.headerRow}
151-
data={contentResult?.private.contents.items}
202+
data={data?.items}
152203
columns={columns}
153204
keySelector={contentKeySelector}
154205
/>

src/views/ContentManagement/styles.module.css

+5
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@
1515
.actions{
1616
min-width:14rem;
1717
}
18+
.status{
19+
background-color: var(--cms-ui-color-background);
20+
color: var(--cms-ui-primary-text-color);
21+
font-weight: var(--cms-ui-font-weight-medium);
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)