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 { DashboardContent } from 'src/layouts/dashboard';
|
||||
import docService, { Doc } from 'src/services/document-service';
|
||||
import { Attribute, getAttributes } from 'src/services/attribute-service';
|
||||
import { Doc, getDocumentByAttributes, getDocuments } from 'src/services/document-service';
|
||||
|
||||
import { Iconify } from 'src/components/iconify';
|
||||
|
||||
@ -40,7 +40,7 @@ export function DocSearchView() {
|
||||
const [docs, setDocs] = useState<Doc[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
getDocuments({}).then((res) => {
|
||||
docService.getDocuments({}).then((res) => {
|
||||
setDocs(res);
|
||||
});
|
||||
}, []);
|
||||
@ -49,7 +49,7 @@ export function DocSearchView() {
|
||||
const [attributes, setAttributes] = useState<Record<string, string>>({});
|
||||
|
||||
useEffect(() => {
|
||||
getDocumentByAttributes(attributes).then(setDocs);
|
||||
docService.getDocumentByAttributes(attributes).then(setDocs);
|
||||
}, [attributes]);
|
||||
|
||||
function setAttribute(name: string, serilizedValue: string) {
|
||||
|
||||
@ -77,31 +77,35 @@ export type DocQuery = {
|
||||
attributes?: Record<string, string>
|
||||
}
|
||||
|
||||
export function getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
|
||||
let documents = _documents;
|
||||
class DocService {
|
||||
getDocuments(query: DocQuery | undefined = undefined): Promise<Doc[]> {
|
||||
let documents = _documents;
|
||||
|
||||
if (query?.id)
|
||||
documents = documents.filter(d => d.id === query.id);
|
||||
if (query?.id)
|
||||
documents = documents.filter(d => d.id === query.id);
|
||||
|
||||
if (query?.name)
|
||||
documents = documents.filter(d => d.name === query.name);
|
||||
if (query?.name)
|
||||
documents = documents.filter(d => d.name === query.name);
|
||||
|
||||
for (const name in query?.attributes) {
|
||||
const attr = query.attributes[name];
|
||||
documents = documents.filter(d => d.attributes.find(a => a.name === name)?.serilizedValue.toLowerCase().includes(attr.toLowerCase()));
|
||||
for (const name in query?.attributes) {
|
||||
const attr = query.attributes[name];
|
||||
documents = documents.filter(d => d.attributes.find(a => a.name === name)?.serilizedValue.toLowerCase().includes(attr.toLowerCase()));
|
||||
}
|
||||
|
||||
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[]> {
|
||||
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 });
|
||||
}
|
||||
export default new DocService();
|
||||
Loading…
x
Reference in New Issue
Block a user