Skip to content

Commit

Permalink
getDateForNewPage thunk -> api slice
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 25, 2024
1 parent c06a51c commit a0fdcf5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ type PagesToolbarProps = PropsWithChildren<unknown>;

const PagesToolbar: FC<PagesToolbarProps> = ({ children }) => {
const classes = useToolbarStyles();
const [filter, showFilter] = usePopover();

const selectedPageIds = useAppSelector(state => state.pages.selectedPageIds);
const operationStatus = useAppSelector(state => state.pages.operationStatus);

const [isInputDialogOpened, setIsInputDialogOpened] = useState(false);
const [isDeleteDialogOpened, setIsDeleteDialogOpened] = useState(false);

const [filter, showFilter] = usePopover();
const dateForNewPage = useDateForNewPage(isInputDialogOpened);

const dateForNewPage = useDateForNewPage();
const [createPage, createPageRequest] = pagesApi.useCreatePageMutation();
const [deletePages, deletePagesRequest] = pagesApi.useDeletePagesMutation();

Expand Down
21 changes: 6 additions & 15 deletions src/frontend/src/features/pages/hooks/useDateForNewPage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { useEffect, useMemo } from 'react';
import { useAppDispatch, useAppSelector } from 'src/hooks';
import { getDateForNewPage } from '../thunks';
import { useMemo } from 'react';
import { pagesApi } from '../api';

export const useDateForNewPage = (isInitialized: boolean): Date => {
const dateForNewPage = useAppSelector(state => state.pages.dateForNewPage);
const status = useAppSelector(state => state.pages.dateForNewPageLoading);
const dispatch = useAppDispatch();

useEffect(() => {
if (isInitialized) {
void dispatch(getDateForNewPage());
}
}, [dispatch, isInitialized]);
export const useDateForNewPage = (): Date => {
const query = pagesApi.useGetDateForNewPageQuery();

return useMemo(
() => (status === 'succeeded' && dateForNewPage ? new Date(dateForNewPage) : new Date()),
[dateForNewPage, status],
() => (query.isSuccess && query.data ? new Date(query.data) : new Date()),
[query.data, query.isSuccess],
);
};

0 comments on commit a0fdcf5

Please sign in to comment.