Skip to content

Commit fa335d6

Browse files
committed
feat: add create as copy on viewing a statement
- fetches once when new/statement/statement_rid is navigated to - autofills form with fetched data, filtering out a list of restricted fields KBDEV-1344
1 parent df3a823 commit fa335d6

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

src/components/StatementForm/index.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import {
55
Button, CircularProgress,
66
Paper, Typography,
77
} from '@material-ui/core';
8+
import FileCopyIcon from '@material-ui/icons/FileCopy';
89
import LocalLibraryIcon from '@material-ui/icons/LocalLibrary';
910
import { Alert } from '@material-ui/lab';
11+
import isempty from 'lodash.isempty';
1012
import { useSnackbar } from 'notistack';
1113
import React, {
1214
useCallback, useEffect, useState,
1315
} from 'react';
1416
import { useMutation, useQuery } from 'react-query';
17+
import { Link, useNavigate, useParams } from 'react-router-dom';
1518

1619
import ActionButton from '@/components/ActionButton';
1720
import { useAuth } from '@/components/Auth';
@@ -54,6 +57,9 @@ const StatementForm = ({
5457
variant,
5558
...rest
5659
}: StatementFormProps) => {
60+
const params = useParams();
61+
const navigate = useNavigate();
62+
5763
const { data: diagnosticData } = useQuery(
5864
tuple(
5965
'/query',
@@ -102,6 +108,30 @@ const StatementForm = ({
102108
async ({ queryKey: [, body] }) => api.query(body),
103109
);
104110

111+
// Fetch and populate the fields for quick copy of record
112+
useQuery(
113+
`/statements/${params.rid}?neighbors=1`,
114+
async ({ queryKey: [route] }) => api.get(route),
115+
{
116+
enabled: (isempty(initialValue) && Boolean(params.rid)),
117+
onSuccess: (data) => {
118+
navigate('/new/statement', { replace: true });
119+
const keysToIgnore = [
120+
'@rid', '@class',
121+
'createdAt', 'createdBy',
122+
'updatedAt', 'updatedBy',
123+
'deletedAt', 'deletedBy',
124+
'uuid', 'reviews',
125+
];
126+
Object.entries(data).forEach(([key, value]) => {
127+
if (!keysToIgnore.includes(key)) {
128+
updateField(key, value);
129+
}
130+
});
131+
},
132+
},
133+
);
134+
105135
const snackbar = useSnackbar();
106136
const auth = useAuth();
107137
const model = schemaDefn.get('Statement');
@@ -324,6 +354,17 @@ const StatementForm = ({
324354
Add Review
325355
</Button>
326356
)}
357+
{variant === FORM_VARIANT.VIEW && (
358+
<Button
359+
className="header__review-action"
360+
component={Link}
361+
target="_blank"
362+
to={!isempty(initialValue) ? `/new/${initialValue['@class'].toLowerCase()}/${initialValue['@rid'].replace(/^#/, '')}/` : ''}
363+
variant="outlined"
364+
>
365+
<FileCopyIcon classes={{ root: 'review-icon' }} /> Create As Copy
366+
</Button>
367+
)}
327368
{civicEvidenceId && <CivicEvidenceLink evidenceId={civicEvidenceId} />}
328369
{onToggleState && (variant === FORM_VARIANT.VIEW || variant === FORM_VARIANT.EDIT) && (
329370
<RecordFormStateToggle

src/views/MainView/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const Main = () => {
109109
component: NewRecordSelectView,
110110
admin: false,
111111
})}
112+
<Route element={<AuthenticatedRoute component={NewRecordView} />} path="/new/:modelName/:rid" />
112113
<Route element={<AuthenticatedRoute component={NewRecordView} />} path="/new/:modelName" />
113114
<Route element={<AuthenticatedRoute component={DataView} />} path="/data/table" />
114115
<Route element={<AuthenticatedRoute component={GraphView} />} path="/data/graph" />

0 commit comments

Comments
 (0)