Skip to content

Commit f6fafce

Browse files
committed
feat(): Adding preload feature to update the articles
1 parent 85e1ad7 commit f6fafce

9 files changed

Lines changed: 139 additions & 23 deletions

File tree

src/screens/articleEditor/ArticleEditor.tsx

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@ import { Box, Button, HStack, Input, Text, VStack } from '@chakra-ui/react';
22
import { MDXEditor } from '@mdxeditor/editor';
33
import { ALL_PLUGINS } from './AllPlugins';
44
import '@mdxeditor/editor/style.css';
5-
import { ArticleCard, MdPreview, useCursor } from '@components';
5+
import { ArticleCard, useCursor } from '@components';
66
import { useCallback, useLayoutEffect, useState } from 'react';
77
import { ARTICLE_DEFAULT } from './constants';
88
import { ArticleWithContent } from './types';
99
import { getRequestObject, isValidArticle } from './utils';
10-
import { useAddArticle } from '@services';
10+
import { useAddArticle, useGetEditorArticleData } from '@services';
11+
import ArticlePreviewDrawer from './ArticlePreviewDrawar';
1112

1213
const ArticleEditor = () => {
1314
const { setCursorType } = useCursor();
15+
const [articleKey, setArticleKey] = useState<string>('');
16+
const [queryArticleKey, setQueryArticleKey] = useState<string>('');
1417
const [state, setState] = useState<ArticleWithContent>(ARTICLE_DEFAULT);
18+
const { data, isPending } = useGetEditorArticleData(queryArticleKey);
1519
const { mutate } = useAddArticle();
1620

1721
useLayoutEffect(() => {
@@ -23,8 +27,23 @@ const ArticleEditor = () => {
2327
console.log('Submitting article', state);
2428
}, [mutate, state]);
2529

30+
useLayoutEffect(() => {
31+
if (data) {
32+
console.log('Data', (data as any)?.data.rows[0]);
33+
console.log('state', state);
34+
setState((data as any)?.data.rows[0]);
35+
}
36+
}, [data, state]);
37+
2638
return (
27-
<VStack px={10} pt={20} width={'100vw'} rowGap={4} bg={'black'}>
39+
<VStack
40+
px={10}
41+
pt={20}
42+
width={'100vw'}
43+
rowGap={4}
44+
bg={'black'}
45+
color={'gray.100'}
46+
>
2847
<HStack width={'100%'} justifyContent={'space-between'} align={'start'}>
2948
<VStack w={'80%'} rowGap={4}>
3049
<HStack w={'100%'} justifyContent={'space-between'}>
@@ -37,7 +56,10 @@ const ArticleEditor = () => {
3756
Article editor
3857
</Text>
3958
<Button
40-
onClick={() => setState(ARTICLE_DEFAULT)}
59+
onClick={() => {
60+
setState(ARTICLE_DEFAULT);
61+
setQueryArticleKey('');
62+
}}
4163
colorScheme={'red'}
4264
>
4365
Clear Article
@@ -125,13 +147,27 @@ const ArticleEditor = () => {
125147
Submit Article
126148
</Button>
127149
</VStack>
128-
<ArticleCard
129-
views={0}
130-
likes={0}
131-
last_updated={new Date().toISOString()}
132-
{...state}
133-
/>
150+
<VStack borderLeft={'1px solid gray'} p={1}>
151+
<HStack>
152+
<Input
153+
placeholder="Article Key"
154+
value={articleKey}
155+
onChange={(e) => setArticleKey(e.target.value)}
156+
/>
157+
<Button
158+
isDisabled={isPending}
159+
isLoading={isPending}
160+
onClick={() => setQueryArticleKey(articleKey)}
161+
>
162+
Preload
163+
</Button>
164+
</HStack>
165+
<ArticleCard {...state} />
166+
</VStack>
134167
</HStack>
168+
<Box w={'100%'} h={'100%'} borderRadius={'md'}>
169+
<ArticlePreviewDrawer mdString={state.md_data} />
170+
</Box>
135171
<HStack
136172
width={'100%'}
137173
border={'1px solid white'}
@@ -153,17 +189,6 @@ const ArticleEditor = () => {
153189
}));
154190
}}
155191
/>
156-
<Box w={'100%'} h={'100%'} bg={'gray.900'} borderRadius={'md'}>
157-
<Text
158-
textAlign={'center'}
159-
color={'white'}
160-
fontSize={'2xl'}
161-
fontWeight={'bold'}
162-
>
163-
Preview
164-
</Text>
165-
<MdPreview mdString={state.md_data} />
166-
</Box>
167192
</HStack>
168193
</VStack>
169194
);
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {
2+
Button,
3+
Drawer,
4+
DrawerOverlay,
5+
DrawerContent,
6+
DrawerCloseButton,
7+
DrawerHeader,
8+
DrawerBody,
9+
useDisclosure,
10+
} from '@chakra-ui/react';
11+
import { MdPreview, Noise } from '@components';
12+
import { useRef } from 'react';
13+
14+
const ArticlePreviewDrawer = ({ mdString }: { mdString: string }) => {
15+
const { isOpen, onOpen, onClose } = useDisclosure();
16+
const btnRef = useRef<HTMLButtonElement>(null);
17+
18+
return (
19+
<>
20+
<Button ref={btnRef} onClick={onOpen}>
21+
Preview Article
22+
</Button>
23+
<Drawer
24+
size={'xl'}
25+
isOpen={isOpen}
26+
placement="right"
27+
onClose={onClose}
28+
finalFocusRef={btnRef}
29+
>
30+
<DrawerOverlay />
31+
<DrawerContent
32+
bg={'black'}
33+
color={'white'}
34+
border={'1px solid gray'}
35+
borderRadius={10}
36+
>
37+
<Noise />
38+
<DrawerCloseButton />
39+
<DrawerHeader>Article Preview</DrawerHeader>
40+
<DrawerBody p={0}>
41+
<MdPreview mdString={mdString} />
42+
</DrawerBody>
43+
</DrawerContent>
44+
</Drawer>
45+
</>
46+
);
47+
};
48+
49+
export default ArticlePreviewDrawer;

src/screens/articleEditor/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@ export const ARTICLE_DEFAULT: ArticleWithContent = {
99
author: '',
1010
group_id: '',
1111
group_name: '',
12+
views: 0,
13+
likes: 0,
14+
last_updated: '',
1215
};

src/screens/articleEditor/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@ export type ArticleWithContent = {
77
author: string;
88
group_id: string;
99
group_name: string;
10+
views: number;
11+
likes: number;
12+
last_updated: string;
1013
};

src/services/backend/articles/constants.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const LEARNING_BACKEND_URL = 'https://amitraikwar-services.onrender.com';
2-
// export const LEARNING_BACKEND_URL = 'http://localhost:3000';
1+
// export const LEARNING_BACKEND_URL = 'https://amitraikwar-services.onrender.com';
2+
export const LEARNING_BACKEND_URL = 'http://localhost:3000';
33

44
// Query
55
export const ALL_ARTICLES_URL = `/all_articles`;
@@ -12,3 +12,6 @@ export const ADD_ARTICLE_URL = `/add_article`;
1212
export const DELETE_ARTICLE_URL = `/delete_article`;
1313
export const UPDATE_ARTICLE_URL = `/update_article`;
1414
export const ADD_COMMENT_URL = `/add_comment`;
15+
16+
// Editor
17+
export const EDITOR_ARTICLES_URL = `/get_editor_article`;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { GetRequest } from '../client/client';
2+
import { EDITOR_ARTICLES_URL } from './constants';
3+
import { StandardResponse } from './types';
4+
5+
const getEditorArticleData = async (
6+
articleKey: string,
7+
): Promise<StandardResponse> => {
8+
return await GetRequest(EDITOR_ARTICLES_URL + '?articleKey=' + articleKey);
9+
};
10+
11+
export default getEditorArticleData;

src/services/backend/articles/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ export { default as likePageArticleData } from './likePageArticleData';
55
export { default as addArticle } from './addArticle';
66
export { default as addComment } from './addComment';
77
export { default as getComments } from './getComments';
8+
9+
export { default as getEditorArticleData } from './getEditorArticleData';

src/services/hooks/articles/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { default as useGetArticlesData } from './useGetAllArticlesData';
22
export { default as useGetArticleData } from './useGetArticleData';
3+
export { default as useGetEditorArticleData } from './useGetEditorArticleData';
34
export { default as useLikeArticleData } from './useLikeArticleData';
45
export { default as useAddArticle } from './useAddArticle';
56
export { default as useAddComment } from './useAddComment';
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { isEmpty } from 'lodash';
2+
import { getEditorArticleData } from '../../backend';
3+
import { useCallQuery } from '../common';
4+
5+
const useGetArticlesData = (articleKey: string) => {
6+
return useCallQuery({
7+
method: () =>
8+
!isEmpty(articleKey)
9+
? getEditorArticleData(articleKey)
10+
: Promise.resolve(undefined),
11+
queryOptions: {
12+
queryKey: ['editor_article', articleKey],
13+
staleTime: 0,
14+
gcTime: 0,
15+
},
16+
});
17+
};
18+
19+
export default useGetArticlesData;

0 commit comments

Comments
 (0)