refactor(doc): encapsulate document query functions into DocService class
This commit is contained in:
parent
c022b96a12
commit
a34270cfc3
@ -7,8 +7,8 @@ import Typography from '@mui/material/Typography';
|
|||||||
import Pagination from '@mui/material/Pagination';
|
import Pagination from '@mui/material/Pagination';
|
||||||
|
|
||||||
import { DashboardContent } from 'src/layouts/dashboard';
|
import { DashboardContent } from 'src/layouts/dashboard';
|
||||||
|
import docService, { Doc } from 'src/services/document-service';
|
||||||
import { Attribute, getAttributes } from 'src/services/attribute-service';
|
import { Attribute, getAttributes } from 'src/services/attribute-service';
|
||||||
import { Doc, getDocumentByAttributes, getDocuments } from 'src/services/document-service';
|
|
||||||
|
|
||||||
import { Iconify } from 'src/components/iconify';
|
import { Iconify } from 'src/components/iconify';
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ export function DocSearchView() {
|
|||||||
const [docs, setDocs] = useState<Doc[]>([]);
|
const [docs, setDocs] = useState<Doc[]>([]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getDocuments({}).then((res) => {
|
docService.getDocuments({}).then((res) => {
|
||||||
setDocs(res);
|
setDocs(res);
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
@ -49,7 +49,7 @@ export function DocSearchView() {
|
|||||||
const [attributes, setAttributes] = useState<Record<string, string>>({});
|
const [attributes, setAttributes] = useState<Record<string, string>>({});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
getDocumentByAttributes(attributes).then(setDocs);
|
docService.getDocumentByAttributes(attributes).then(setDocs);
|
||||||
}, [attributes]);
|
}, [attributes]);
|
||||||
|
|
||||||
function setAttribute(name: string, serilizedValue: string) {
|
function setAttribute(name: string, serilizedValue: string) {
|
||||||
|
|||||||
@ -77,7 +77,8 @@ export type DocQuery = {
|
|||||||
attributes?: Record<string, string>
|
attributes?: Record<string, string>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
|
class DocService {
|
||||||
|
getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
|
||||||
let documents = _documents;
|
let documents = _documents;
|
||||||
|
|
||||||
if (query?.id)
|
if (query?.id)
|
||||||
@ -92,16 +93,19 @@ export function getDocuments(query: DocQuery | undefined = undefined): Promise<D
|
|||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(documents);
|
return Promise.resolve(documents);
|
||||||
|
}
|
||||||
|
|
||||||
|
getDocumentById(id: number): Promise<Doc[]> {
|
||||||
|
return this.getDocuments({ id: id });
|
||||||
|
}
|
||||||
|
|
||||||
|
getDocumentByName(name: string): Promise<Doc[]> {
|
||||||
|
return this.getDocuments({ name: name });
|
||||||
|
}
|
||||||
|
|
||||||
|
getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
|
||||||
|
return this.getDocuments({ attributes: attributes });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getDocumentById(id: number): Promise<Doc[]> {
|
export default new DocService();
|
||||||
return getDocuments({ id: id });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getDocumentByName(name: string): Promise<Doc[]> {
|
|
||||||
return getDocuments({ name: name });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getDocumentByAttributes(attributes: Record<string, string>): Promise<Doc[]> {
|
|
||||||
return getDocuments({ attributes: attributes });
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user