feat: replaced select source modal with hook to set default doc

This commit is contained in:
Siddhant Rai
2024-08-05 22:28:58 +05:30
parent 22b7445ac4
commit 51553c565f
3 changed files with 32 additions and 124 deletions

View File

@@ -0,0 +1,30 @@
import React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Doc, getDocs } from '../preferences/preferenceApi';
import {
selectSelectedDocs,
setSelectedDocs,
setSourceDocs,
} from '../preferences/preferenceSlice';
export default function useDefaultDocument() {
const dispatch = useDispatch();
const selectedDoc = useSelector(selectSelectedDocs);
const fetchDocs = () => {
getDocs().then((data) => {
dispatch(setSourceDocs(data));
if (!selectedDoc)
data?.forEach((doc: Doc) => {
if (doc.model && doc.name === 'default') {
dispatch(setSelectedDocs(doc));
}
});
});
};
React.useEffect(() => {
fetchDocs();
}, []);
}