Skip to content

Commit

Permalink
Cleanup frontend files
Browse files Browse the repository at this point in the history
  • Loading branch information
n1klaus committed May 1, 2023
1 parent c1f1962 commit 33d6d88
Show file tree
Hide file tree
Showing 35 changed files with 1,062 additions and 850 deletions.
3 changes: 2 additions & 1 deletion frontend/.eslintrc.mjs → frontend/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = {
},
ecmaVersion: 2018,
sourceType: 'module',
project: './tsconfig.json'
project: 'frontend/tsconfig.json'
},
plugins: ['jest', 'react', '@typescript-eslint'],
settings: {
Expand All @@ -44,6 +44,7 @@ module.exports = {
}
},
rules: {
quotes: [2, "single", { "avoidEscape": true }],
'max-classes-per-file': 'off',
'no-underscore-dangle': 'off',
'no-console': 'off',
Expand Down
4 changes: 2 additions & 2 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"start": "vite --port 3000",
"build": "tsc --incremental --noEmit && vite build",
"preview": "vite preview",
"lint": "./node_modules/.bin/eslint *.js *.ts --fix",
"check-lint": "./node_modules/.bin/eslint *.js *.ts",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"check-lint": "eslint . --ext .js,.jsx,.ts,.tsx ",
"fmt": "prettier --write .",
"check-fmt": "prettier --check .",
"semifix": "semistandard --fix .",
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRoutes } from "react-router-dom";
import routes from "./router";
import { useRoutes } from 'react-router-dom';
import routes from './router';

function App() {
const content = useRoutes(routes);
Expand Down
75 changes: 23 additions & 52 deletions frontend/src/components/ChannelCard.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import axios from "axios";
import React, { useEffect, useState } from "react";
import { Link } from "react-router-dom";
import styled from "styled-components";
import {format} from "timeago.js";
import { IVideo, IChannel } from "../utils/types"
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { format } from 'timeago.js';
import { IChannel } from '../utils/types';

const Container = styled.div`
width: ${(props: any) => props.type !== "sm" && "360px"};
margin-bottom: ${(props: any) => (props.type === "sm" ? "10px" : "45px")};
width: ${(props: any) => props.type !== 'sm' && '360px'};
margin-bottom: ${(props: any) => (props.type === 'sm' ? '10px' : '45px')};
cursor: pointer;
display: ${(props: any) => props.type === "sm" && "flex"};
display: ${(props: any) => props.type === 'sm' && 'flex'};
gap: 10px;
`;

const Image = styled.img`
width: 100%;
height: ${(props: any) => (props.type === "sm" ? "120px" : "202px")};
height: ${(props: any) => (props.type === 'sm' ? '120px' : '202px')};
background-color: #999;
flex: 1;
`;

const Details = styled.div`
display: flex;
margin-top: ${(props: any) => props.type !== "sm" && "16px"};
margin-top: ${(props: any) => props.type !== 'sm' && '16px'};
gap: 12px;
flex: 1;
`;
Expand All @@ -32,7 +30,7 @@ const ChannelImage = styled.img`
height: 38px;
border-radius: 50%;
background-color: #999;
display: ${(props: any) => props.type === "sm" && "none"};
display: ${(props: any) => props.type === 'sm' && 'none'};
`;

const Texts = styled.div``;
Expand All @@ -55,54 +53,27 @@ const Info = styled.div`
color: ${({ theme }) => theme.textSoft};
`;

const ChannelCard = ({ channel }: { channel:IChannel}) => {
// const [channel, setChannel] = useState<IChannel>({
// _id: "",
// name: "",
// description: "",
// imgUrl: "",
// views: 0,
// tags: [],
// likes: [],
// dislikes: [],
// videos: [],
// subscribers: 0,
// isPublic: false,
// });
const SERVER_ENDPOINT = import.meta.env.VITE_BACKEND_ENDPOINT;
// useEffect(() => {
// const fetchChannel = async () => {
// const res = await axios.get(`${SERVER_ENDPOINT}/channels/${channel.channelId}/view`);
// setChannel(res.data);
// };
// fetchChannel();
// }, [channel.userId]);

function ChannelCard({ channel }: { channel: IChannel }) {
return (
<Link to={`/channels/${channel._id}`} style={{ textDecoration: "none" }}>
<Container
// type={type}
>
<Image
// type={type}
src={channel.imgUrl}
/>
<Details
// type={type}
>
<ChannelImage
// type={type}
src={channel.imgUrl}
/>
<Link to={`/channels/${channel._id}`} style={{ textDecoration: 'none' }}>
<Container>
<Image src={channel.imgUrl} />
<Details>
<ChannelImage src={channel.imgUrl} />
<Texts>
<Title>{channel.name}</Title>
<ChannelName>{channel.name}</ChannelName>
<Info>{channel.views} views • {format(channel.createdAt)}</Info>
<Info>
{channel.views}
{' '}
views •
{format(channel.createdAt)}
</Info>
</Texts>
</Details>
</Container>
</Link>
);
};
}

export default ChannelCard;
51 changes: 22 additions & 29 deletions frontend/src/components/Comment.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import axios from "axios";
import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { IChannel, IComment } from "../utils/types";
import {format} from "timeago.js";
import useStore from "../store";
import axios from 'axios';
import { useEffect } from 'react';
import styled from 'styled-components';
import { format } from 'timeago.js';
import { IComment } from '../utils/types';
import useStore from '../store';

const Container = styled.div`
display: flex;
Expand Down Expand Up @@ -40,44 +40,37 @@ const Text = styled.span`
`;

const SERVER_ENDPOINT = import.meta.env.VITE_BACKEND_ENDPOINT;
const Comment = ({ videoId, comment }: {videoId: string, comment: IComment}) => {
function Comment({
videoId,
comment,
}: {
videoId: string;
comment: IComment;
}) {
const store = useStore();
const user = store.authUser;
// const [channel, setChannel] = useState<IChannel>({
// _id: "",
// name: "",
// description: "",
// imgUrl: "",
// views: 0,
// tags: [],
// likes: [],
// dislikes: [],
// videos: [],
// subscribers: 0,
// isPublic: false,
// createdAt: ""
// });

useEffect(() => {
const fetchComment = async () => {
const videoRes = await axios.get(`${SERVER_ENDPOINT}/videos/${videoId}`);
// const userRes = await axios.get(`${SERVER_ENDPOINT}/users/${videoRes.data.userId}`);
// setChannel(userRes.data)
};
fetchComment();
const fetchComment = async () => {
const videoRes = await axios.get(`${SERVER_ENDPOINT}/videos/${videoId}`);
};
fetchComment();
}, [videoId]);

return (
<Container>
<Avatar src={user?.avatar} />
<Details>
<Name>
{user?.username}{format(comment.createdAt)}
{user?.username}
{' '}
{format(comment.createdAt)}
</Name>
<Text>{comment.description}</Text>
</Details>
</Container>
);
};
}

export default Comment;
77 changes: 30 additions & 47 deletions frontend/src/components/Comments.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import PropTypes from "prop-types"
import PropTypes from 'prop-types';
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import { useNavigate } from 'react-router-dom';
import { toast } from 'react-toastify';
import Comment from './Comment';
import { IChannel, IUser, IVideo, IComment } from "../utils/types";
import { useNavigate } from "react-router-dom";
import useStore from "../store";
import { toast } from "react-toastify";
import { IComment } from '../utils/types';
import useStore from '../store';

const Container = styled.div``;

Expand Down Expand Up @@ -43,59 +43,44 @@ const Button = styled.button`

const SERVER_ENDPOINT = import.meta.env.VITE_BACKEND_ENDPOINT;

const Comments = ( {videoId}: {videoId: string} ) => {
function Comments({ videoId }: { videoId: string }) {
const navigate = useNavigate();
const store = useStore();
const [newcomment, setNewComment] = useState<string>();
const [comments, setComments] = useState<Array<IComment>>([]);
// const [currentUser, setUser] = useState<IUser>({
// _id: "",
// username: "",
// email: "",
// avatar: "",
// subscriptions: [],
// history: [],
// channels: [],
// fromGoogle: false,
// });
const currentUser = store.authUser;

useEffect(() => {
const fetchComments = async () => {
try {
const commentRes = await axios.get(`${SERVER_ENDPOINT}/comments/${videoId}`);
const commentRes = await axios.get(
`${SERVER_ENDPOINT}/comments/${videoId}`,
);
setComments(commentRes.data);
// const userRes = await axios.get(
// `${SERVER_ENDPOINT}/users/${commentRes.data.userId}`
// );
// setUser(userRes.data)
} catch (err) {}
};
fetchComments();
}, [videoId]);

const handleAddComment = async () => {
try {
await axios.post(`${SERVER_ENDPOINT}/comments/${videoId}`,
{description: newcomment},
{
withCredentials: true,
});
await axios.post(
`${SERVER_ENDPOINT}/comments/${videoId}`,
{ description: newcomment },
{
withCredentials: true,
},
);
} catch (error: any) {
console.log(error?.message)
const resMessage =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();

if (error?.message === "You are not logged in") {
navigate("/login");
}
console.log(error?.message);
const resMessage = (error.response
&& error.response.data
&& error.response.data.message)
|| error.message
|| error.toString();

toast.error(resMessage, {
position: "top-right",
position: 'top-right',
});
}
};
Expand All @@ -104,23 +89,21 @@ const Comments = ( {videoId}: {videoId: string} ) => {
<Container>
<NewComment>
<Avatar src={currentUser?.avatar} />
<Input
<Input
placeholder="Add a comment..."
onChange={(e) => setNewComment(e.target.value)}
/>
<Button onClick={handleAddComment}>
Add
</Button>
<Button onClick={handleAddComment}>Add</Button>
</NewComment>
{comments.map((comment) => (
<Comment key={comment._id} videoId={videoId} comment={comment}/>
<Comment key={comment._id} videoId={videoId} comment={comment} />
))}
</Container>
);
};
}

Comments.propTypes = {
videoId: PropTypes.string
}
videoId: PropTypes.string,
};

export default Comments;
13 changes: 0 additions & 13 deletions frontend/src/components/DefaultLayout.tsx

This file was deleted.

Loading

0 comments on commit 33d6d88

Please sign in to comment.