Skip to content

Commit

Permalink
YT-21: Migrate Search.js to TypeScript (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanL123 authored May 14, 2024
1 parent d551c0a commit d1866b0
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 183 deletions.
38 changes: 3 additions & 35 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { BrowserRouter, Route, Routes } from "react-router-dom";
import Help from "./components/Help";
import Results from "./components/Results";
import Search from "./components/Search";
import { SortOptions } from "./types";
import { getPlaylist, sortPlaylist } from "./util/playlistUtil";

ReactGA.initialize("G-LRVNS567ZT");
Expand All @@ -15,40 +16,7 @@ const App = () => {
const [playlistID, setPlaylistID] = useState("");
const [loading, setLoading] = useState(false);
const [playlist, setPlaylist] = useState([]);
const sortOptions = [
{
value: "vd",
label: "Views Descending",
},
{
value: "va",
label: "Views Ascending",
},
{
value: "ld",
label: "Likes Descending",
},
{
value: "la",
label: "Likes Ascending",
},
{
value: "ud",
label: "Most Recent",
},
{
value: "ua",
label: "Earliest",
},
{
value: "dd",
label: "Longest",
},
{
value: "da",
label: "Shortest",
},
];

const updateSortOrder = (event) => {
const value = event.target.value;
// Google Analytics
Expand Down Expand Up @@ -96,7 +64,7 @@ const App = () => {
element={
<Box>
<Search
sortOptions={sortOptions}
sortOptions={Object.keys(SortOptions)}
order={order}
playlistID={playlistID}
updatePlaylistID={(event) => updatePlaylistID(event)}
Expand Down
6 changes: 3 additions & 3 deletions src/components/Results.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Box } from "@mui/material";
import * as React from "react";
import { IVideoMetadata } from "../types";
import { VideoMetadata } from "../types";
import Video from "./Video";

const Results = ({ videos }: { videos: { stats: IVideoMetadata }[] }) => (
const Results = ({ videos }: { videos: VideoMetadata[] }) => (
<Box
display="flex"
justifyContent="center"
Expand All @@ -13,7 +13,7 @@ const Results = ({ videos }: { videos: { stats: IVideoMetadata }[] }) => (
pb="5vh"
>
{videos.map((video, index) => {
return <Video metadata={video.stats} key={index}></Video>;
return <Video metadata={video} key={index}></Video>;
})}
</Box>
);
Expand Down
28 changes: 13 additions & 15 deletions src/components/Search.js → src/components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { InfoOutlined, Search as SearchIcon } from "@mui/icons-material";
import { LoadingButton } from "@mui/lab";
import { Box, Button, MenuItem, TextField } from "@mui/material";
import PropTypes from "prop-types";
import React from "react";
import * as React from "react";
import { Link } from "react-router-dom";
import { SortOptions } from "../types";

const Search = (props) => (
const Search = (props: {
playlistID: string;
order: string;
loading: boolean;
updatePlaylistID: any;
updateSortOrder: any;
search: any;
sortOptions: SortOptions[];
}) => (
<Box display="flex" justifyContent="center" py="5vh" px="10vw">
<Box display="flex" width="100%" alignItems="center" flexDirection="column">
<form style={{ width: "100%" }}>
Expand All @@ -29,8 +37,8 @@ const Search = (props) => (
onChange={props.updateSortOrder}
>
{props.sortOptions.map((option) => (
<MenuItem key={option.value} value={option.value}>
{option.label}
<MenuItem key={option} value={option.valueOf()}>
{SortOptions[option]}
</MenuItem>
))}
</TextField>
Expand Down Expand Up @@ -70,14 +78,4 @@ const Search = (props) => (
</Box>
);

Search.propTypes = {
playlistID: PropTypes.string,
order: PropTypes.string,
loading: PropTypes.bool,
updatePlaylistID: PropTypes.func,
updateSortOrder: PropTypes.func,
search: PropTypes.func,
sortOptions: PropTypes.array,
};

export default Search;
4 changes: 2 additions & 2 deletions src/components/Video.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Box, Link, useMediaQuery } from "@mui/material";
import Typography from "@mui/material/Typography";
import * as React from "react";
import { IVideoMetadata } from "../types";
import { VideoMetadata } from "../types";
import { convertISOtoString } from "../util/dateUtil";

const Video = ({ metadata }: { metadata: IVideoMetadata }) => {
const Video = ({ metadata }: { metadata: VideoMetadata }) => {
const mobile = useMediaQuery("(max-width:1000px)");
return (
<Box
Expand Down
13 changes: 12 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Represents metadata of a video
*/
export interface IVideoMetadata {
export type VideoMetadata = {
link: string;
title: string;
channel: string;
Expand All @@ -10,4 +10,15 @@ export interface IVideoMetadata {
uploadDate: string;
duration: number;
thumbnail: string;
};

export enum SortOptions {
VIEWS_DESC = "Views Descending",
VIEWS_ASC = "Views Ascending",
LIKES_DESC = "Likes Descending",
LIKES_ASC = "Likes Ascending",
MOST_RECENT = "Most Recent",
LEAST_RECENT = "Least Recent",
LONGEST = "Longest",
SHORTEST = "Shortest",
}
127 changes: 0 additions & 127 deletions src/util/playlistUtil.js

This file was deleted.

Loading

0 comments on commit d1866b0

Please sign in to comment.