Skip to content

Commit

Permalink
feat: add fileUpload, editor, and tag/category input component and cr…
Browse files Browse the repository at this point in the history
…eate post page

create custom react context and reducer

also refactor code and folder structure, and editorjs plugin dependencies
  • Loading branch information
ahmadxgani committed Mar 3, 2023
1 parent f6ec209 commit a76567d
Show file tree
Hide file tree
Showing 16 changed files with 502 additions and 5 deletions.
1 change: 0 additions & 1 deletion lib/GraphQL/Queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export const SHOW_ALL_TAGS = gql`
query ShowAllTags {
showAllTag {
name
id
}
}
`;
6 changes: 6 additions & 0 deletions lib/constants/category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const KEY_CODES = {
comma: 188,
enter: 13,
};

export const DELIMITER = [KEY_CODES.comma, KEY_CODES.enter];
23 changes: 23 additions & 0 deletions lib/constants/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useTagSource } from "src/context/tag.context";

export type TagState = {
suggestions: string[];
tags: string[];
input: string;
};

export type TagProviderType = ReturnType<typeof useTagSource>;

export type TagAction =
| {
type: "insert";
payload: {
name: string;
};
}
| {
type: "delete";
payload: {
index: number;
};
};
9 changes: 9 additions & 0 deletions src/utility.ts → lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ export const setToStorage = (key: string, value: string) => {
return window.localStorage.setItem(key, value);
}
};

export const escapedRegex = (text: string) => {
return text.trim().replace(/[-\\^$*+?.()|[\]{}]/g, "\\$&");
};

export const stopNextEvent = (event: React.KeyboardEvent<HTMLInputElement>) => {
event.stopPropagation();
event.preventDefault();
};
116 changes: 116 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"@apollo/link-error": "^2.0.0-beta.3",
"@apollo/react-hooks": "^4.0.0",
"@chakra-ui/react": "^2.4.6",
"@editorjs/editorjs": "^2.26.5",
"@editorjs/header": "^2.7.0",
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"@next/font": "13.0.7",
Expand All @@ -31,6 +33,7 @@
"next": "13.0.7",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.43.3",
"react-icons": "^4.7.1",
"typescript": "4.9.4"
},
Expand Down
2 changes: 1 addition & 1 deletion pages/[username]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { GetServerSidePropsContext } from "next";
import { Heading } from "@chakra-ui/react";
import MainLayout from "components/layout/main.layout";
import { Query } from "generated-types";
import { addApolloState, initializeApollo } from "lib/GraphQL/apollo";
import { addApolloState, initializeApollo } from "src/hooks/apollo";
import { CURRENT_USER } from "lib/GraphQL/Queries";
import { useRouter } from "next/router";
import { withWrapper } from "src/hooks/withWrapper";
Expand Down
2 changes: 1 addition & 1 deletion pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AppProps } from "next/app";
import { ApolloProvider } from "@apollo/client";
import { ChakraProvider } from "@chakra-ui/react";

import { useApollo } from "lib/GraphQL/apollo";
import { useApollo } from "src/hooks/apollo";
import theme from "theme";

export default function App({ Component, pageProps }: AppProps) {
Expand Down
18 changes: 16 additions & 2 deletions pages/new.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { Heading } from "@chakra-ui/react";
import { Heading, Input, VStack } from "@chakra-ui/react";
import UploadImage from "components/fileUpload";
import MainLayout from "components/layout/main.layout";
import dynamic from "next/dynamic";
import TagInput from "src/context/tag.context";

const Editor = dynamic(() => import("components/editor"), {
loading: () => <>loading...</>,
ssr: false,
});

const NewPost = () => {
return (
<MainLayout title={"New Post"}>
<Heading>Create Post Page</Heading>
<Heading>Create Post</Heading>
<VStack as="form" maxW="50rem" w="full" alignItems={"stretch"} spacing={4} bg="cyan.100" p={10} rounded="lg">
<UploadImage />
<Input type={"text"} fontSize="5xl" size="lg" border="none" background="transparent" _focus={{ boxShadow: "none" }} placeholder="New post title here..." _placeholder={{ fontWeight: 700 }} />
<TagInput />
<Editor />
</VStack>
</MainLayout>
);
};
Expand Down
21 changes: 21 additions & 0 deletions src/components/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Box } from "@chakra-ui/react";
import EditorJS from "@editorjs/editorjs";
import Header from "@editorjs/header";

const Editor = () => {
const editor = new EditorJS({
holder: "editorjs",
tools: {
headers: Header,
},
autofocus: false,
});

return (
<Box w="full" h="full">
<div id="editorjs"></div>
</Box>
);
};

export default Editor;
Loading

0 comments on commit a76567d

Please sign in to comment.