Skip to content

Commit

Permalink
feat:added blog tags
Browse files Browse the repository at this point in the history
  • Loading branch information
SySagar committed Sep 24, 2023
1 parent 6878117 commit d56556d
Show file tree
Hide file tree
Showing 11 changed files with 366 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ src:local('Inconsolata'), url('./fonts/Inconsolata.ttf');
font-weight: 700;
}

*{
margin: 0;
padding: 0;
}

.hero-blog:hover{
position: relative;
}
Expand Down
6 changes: 6 additions & 0 deletions src/app/blog/CreateBlog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IconButton, Stack, TextField, Typography } from "@mui/material";
import Tiptap from "./components/Tiptap";
import AddPhotoAlternateIcon from "@mui/icons-material/AddPhotoAlternate";
import useEditorContent from "../../lib/store/useEditorContent";
import TagInput from "./components/TagInput";

interface BlogImageTypes {
coverImage: File | null | Blob | MediaSource;
Expand Down Expand Up @@ -78,12 +79,17 @@ const onTitleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
<AddPhotoAlternateIcon />
Add a cover image
</IconButton>



</Stack>
{blog.coverImage && (
<img src={URL.createObjectURL(blog.coverImage)} width={'350px'} />
)}
</Stack>
<Stack>
<TagInput/>
</Stack>
<Stack className="editor">
<Tiptap/>
</Stack>
Expand Down
112 changes: 112 additions & 0 deletions src/app/blog/components/TagInput.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// TagInput.tsx
import React, { useEffect, useState } from "react";
import Autocomplete from "@mui/material/Autocomplete";
import TextField from "@mui/material/TextField";
import TAGS from "../../../lib/constants/TAGS";
import { Button, Chip, Stack, Typography } from "@mui/material";
import useEditorContent from "../../../lib/store/useEditorContent";

interface BlogTagTypes {
tags: string[];
}

const TagInput: React.FC = () => {
const [tags, setTags] = useState<string[]>([]);
const [tagInput, setTagInput] = useState<string>("");

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
const [blog, setBlog] = useEditorContent((state: any) => [ state.blog as BlogTagTypes,state.setBlog,]);
// const onTagsChnage = (e: React.ChangeEvent<HTMLInputElement>) => {

// }

const handleTagInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setTagInput(event.target.value);
};

console.log("tags", tags);
const handleAddTag = () => {
if (tagInput.trim() !== "" && !tags.includes(tagInput)) {
setTags([...tags, tagInput]);
setTagInput("");
}
};

const handleAutocompleteChange = (
event: React.SyntheticEvent,
value: string | null
) => {
console.log("value", event);
if (value) {
setTags([...tags, value]);
}
};

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
setBlog({ ...blog, tags: tags });
}, [tags]);

const handleDeleteTag = (tagToDelete: string) => {
setTags(tags.filter((tag) => tag !== tagToDelete));
};

return (
<Stack gap={3} justifyContent={"center"} alignItems={"center"}>
<Typography>Select category/categories</Typography>
<Stack direction={"row"} gap={1}>
<Autocomplete
freeSolo
sx={{
width: "400px",
}}
options={[...tags, ...TAGS]}
renderInput={(params) => (
<TextField
{...params}
variant="outlined"
label="Tags"
placeholder="Enter tags"
value={tagInput}
onChange={handleTagInputChange}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
handleAddTag();
}
}}
/>
)}
onChange={handleAutocompleteChange}
/>
<Button
variant="contained"
sx={{ color: "white", background: "#474747" }}
onClick={handleAddTag}
>
Add Tag
</Button>
</Stack>
<Stack direction={"row"} width={"500px"} flexWrap={"wrap"} gap={"4px"}>
{tags.map((tag, index) => (
<Chip
key={index}
label={tag}
onDelete={() => handleDeleteTag(tag)}
sx={{
background: "#474747",
color: "white",
borderRadius: "14px",
padding: "5px 10px",
"& .MuiChip-deleteIcon": {
color: "white", // Change this to the desired color for the delete icon
},
}}
/>
))}
</Stack>
</Stack>
);
};

export default TagInput;
76 changes: 73 additions & 3 deletions src/app/home/HeroPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useEffect, useState } from "react";
import APIMethods from '../../lib/axios/api'
import { blogTypes } from "../blog/types/blogTypes";
import SentimentDissatisfiedIcon from '@mui/icons-material/SentimentDissatisfied';
import useSearchStore from "../../lib/store/useSearchStore";
import SearchedBlogs from "./components/SearchedBlogs";

// const blogs = [
// {
Expand Down Expand Up @@ -73,6 +75,10 @@ import SentimentDissatisfiedIcon from '@mui/icons-material/SentimentDissatisfied

export default function HeroPage() {
const [loading,setLoading] = useState(true);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const [searchItem ] = useSearchStore((
state: { searchItem: string; }
) => [state.searchItem]);

const [blogs,setBlogs] = useState([] as unknown as blogTypes[]);
const fetchBLogs = async () => {
Expand Down Expand Up @@ -102,7 +108,7 @@ export default function HeroPage() {
className="main"
minHeight={"100vh"}
direction={"row"}
justifyContent={"space-evenly"}
justifyContent={( (searchItem.length==0))?"space-evenly":"space-between"}
position={"relative"}
alignItems={"flex-start"}
>
Expand All @@ -113,15 +119,27 @@ export default function HeroPage() {
alignItems={"center"}
gap={1}
borderColor={"#A3A0B2"}
width={( (searchItem.length==0))?"80vw":"100%"}
>
{
(searchItem.length!=0) ? <Stack position={'relative'} left={20} justifyContent={'start'} alignItems={'start'} width={'100%'}>
<Typography variant="h6">Results for <span style={{color:'black'}}>{searchItem}</span></Typography>
</Stack> :
<Stack justifyContent={'center'} alignItems={'center'}>
<Typography variant="h2">The best from the week</Typography>
<Typography fontSize={'16px'} fontWeight={600}>The latest industry news, interviews, technologies, and resources.</Typography>

<Stack position={"relative"} marginTop={6}>
</Stack>
}

{
(searchItem.length!=0) ? <div></div> :
<Stack position={"relative"} marginTop={6}>
<HeroBlog />
</Stack>
}

{
{/* {
loading ?
<Stack width={'100%'} justifyContent={'center'} alignItems={'center'} height={'20vh'}>
<CircularProgress color="secondary" />
Expand Down Expand Up @@ -157,6 +175,58 @@ export default function HeroPage() {
</Stack>
}
</Stack>
} */}

{
loading ?
<Stack width={'100%'} justifyContent={'center'} alignItems={'center'} height={'20vh'}>
<CircularProgress color="secondary" />
</Stack>:(
(searchItem.length!=0) ?
<Stack
className="daily-blogs"
justifyContent={"center"}
alignItems={"center"}
direction={"row"}
flexGrow={1}
gap={4}
flexWrap={"wrap"}
width={'100%'}
>
<SearchedBlogs />
</Stack>
:
<Stack
className="daily-blogs"
padding={2}
maxWidth={"80vw"}
gap={4}
flexWrap={"wrap"}
justifyContent={"center"}
alignItems={"center"}
direction={"row"}
>
{blogs.length!=0 && blogs.map((blog, index) => (
<Blog
key={index}
blogId={blog._id}
author={blog.authorName}
date={blog.date}
title={blog.title}
tags={blog.tags}
image={blog.coverImageURL}
likes={blog.likes}
/>
))}
{
(blogs.length==0) &&
<Stack direction={'row'} gap={2} paddingY={5} alignItems={'center'}>
<Typography variant="h4">No Blogs to show</Typography>
<SentimentDissatisfiedIcon sx={{fontSize:'3rem',color:'#474747'}} />
</Stack>
}
</Stack>
)
}
</Stack>
<Stack direction={'row'} gap={2} position={"sticky"}
Expand Down
9 changes: 6 additions & 3 deletions src/app/home/components/Blog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,25 @@ export default function Blog({author, date, title, tags=["india","hello world"],
justifyContent={"center"}
alignItems={"center"}
position={"relative"}
maxWidth={"300px"}
maxHeight={"300px"}
onClick={redirectToBlog}
sx={{

background:"#474554",
borderRadius:"5px",
}}
>
<Stack overflow={'hidden'}
sx={{
maxWidth: "300px",
maxHeight: "200px",
objectFit: "cover",
}}>

<img
style={{
objectFit: "cover",
width: "100%",
height: "100%",
borderRadius: "4px",
}}
src={image}
Expand Down
81 changes: 81 additions & 0 deletions src/app/home/components/SearchedBlogs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { CircularProgress, Stack, Typography } from '@mui/material'
import { useEffect } from 'react'
import APIMethods from '../../../lib/axios/api'
import useLoadingStore from '../../../lib/store/useLoading'
import { blogTypes } from '../../blog/types/blogTypes'
import useSearchStore from '../../../lib/store/useSearchStore'
import { useState } from 'react'
import Blog from './Blog'

export default function SearchedBlogs() {

const { isLoading, setIsLoading } = useLoadingStore();
const [blogs,setBlogs] = useState<blogTypes[]>([]);
const {searchItem} = useSearchStore();

const fetchSearchedBlogs = async () => {
try {
setIsLoading(true);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await APIMethods.blog.getBlogsByTag(searchItem).then((res:{data:{blogs:any}}) => {
const blogsRes = res.data.blogs as blogTypes[];
setBlogs(blogsRes);
console.log("searched blogs",blogsRes);
});

} catch (error) {
console.log(error);
}
}


useEffect(() => {
fetchSearchedBlogs().then(() => {
console.log("fetching done");
}).catch((err) => {
console.log(err);
}).finally(() => {
console.log("finally",blogs);
setIsLoading(false);
}
);
}, [searchItem])

useEffect(() => {
if(!blogs || blogs.length === 0) {
return;
}
else
{
setIsLoading(false);
}
}, [blogs])

return (
<Stack paddingTop={5} width={'100%'} padding={5}>
{
isLoading ? <Stack justifyContent={'center'} alignItems={'center'} width={'100%'} height={'100%'} border={2} flexGrow={1}>
<CircularProgress />
</Stack>
:
<Stack>
{blogs.length!=0 && blogs.map((blog, index) => (
<Blog
key={index}
blogId={blog._id}
author={blog.authorName}
date={blog.date}
title={blog.title}
tags={blog.tags}
image={blog.coverImageURL}
likes={blog.likes}
/>
))}
</Stack>
}
{
!isLoading && blogs.length === 0 && <Typography variant='h5'>No blogs found</Typography>
}
</Stack>
)
}
Loading

0 comments on commit d56556d

Please sign in to comment.