Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering dynamic data in Homepage, MyTutorials #861

Merged
merged 15 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"react-responsive": "^8.1.0",
"react-reveal": "^1.2.2",
"react-router-dom": "^5.2.0",
"react-router-hash-link": "^2.4.3",
"react-select": "^5.7.0",
"react-social-login-buttons": "^3.4.0",
"react-syntax-highlighter": "^15.4.3",
Expand Down
93 changes: 60 additions & 33 deletions src/components/Card/CardWithPicture.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { makeStyles } from "@mui/styles";
import Card from "@mui/material/Card";
import CardHeader from "@mui/material/CardHeader";
Expand All @@ -18,6 +18,10 @@ import MoreVertOutlinedIcon from "@mui/icons-material/MoreVertOutlined";
import { ToggleButton, ToggleButtonGroup } from "@mui/material";
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";
import { Link } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
import { useFirebase, useFirestore } from "react-redux-firebase";
import { getUserProfileData } from "../../store/actions";

const useStyles = makeStyles(theme => ({
root: {
Expand Down Expand Up @@ -67,10 +71,13 @@ const useStyles = makeStyles(theme => ({
}
}));

export default function CardWithPicture(props) {
export default function CardWithPicture({ tutorial }) {
const classes = useStyles();
const [alignment, setAlignment] = React.useState("left");
const [count, setCount] = useState(1);
const dispatch = useDispatch();
const firebase = useFirebase();
const firestore = useFirestore();
const handleIncrement = () => {
setCount(count + 1);
};
Expand All @@ -83,22 +90,40 @@ export default function CardWithPicture(props) {
setAlignment(newAlignment);
};

useEffect(() => {
getUserProfileData(tutorial?.created_by)(firebase, firestore, dispatch);
}, [tutorial]);

const user = useSelector(
({
profile: {
user: { data }
}
}) => data
);

const getTime = timestamp => {
return timestamp.toDate().toDateString();
};

return (
<Card className={classes.root}>
<CardMedia
className={classes.media}
image={cardImage}
title="code"
data-testId="Image"
/>
<Link to={`/tutorial/${tutorial?.tutorial_id}`}>
<CardMedia
className={classes.media}
image={tutorial?.featured_image}
title="code"
data-testId="Image"
/>
</Link>
<CardHeader
avatar={
<Avatar
aria-label="recipe"
className={classes.avatar}
data-testId="UserAvatar"
>
S
<Avatar className={classes.avatar}>
{user?.photoURL && user?.photoURL.length > 0 ? (
<img src={user?.photoURL} />
) : (
user?.displayName[0]
)}
</Avatar>
}
title={
Expand All @@ -110,9 +135,9 @@ export default function CardWithPicture(props) {
color="textPrimary"
data-testId="UserName"
>
{props.name}
{user?.displayName}
</Typography>
{props.organizationName && (
{tutorial?.owner && (
<>
{" for "}
<Typography
Expand All @@ -122,28 +147,30 @@ export default function CardWithPicture(props) {
color="textPrimary"
data-testId="UserOrgName"
>
{props.organizationName}
{tutorial?.owner}
</Typography>
</>
)}
</React.Fragment>
}
subheader={props.date}
subheader={tutorial?.createdAt ? getTime(tutorial?.createdAt) : ""}
/>
<CardContent className={classes.contentPadding}>
<Typography variant="h5" color="text.primary" data-testId="Title">
{props.title}
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="p"
paragraph
data-testId="Description"
>
{props.contentDescription}
</Typography>
</CardContent>
<Link to={`/tutorial/${tutorial?.tutorial_id}`}>
<CardContent className={classes.contentPadding}>
<Typography variant="h5" color="text.primary" data-testId="Title">
{tutorial?.title}
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="p"
paragraph
data-testId="Description"
>
{tutorial?.summary}
</Typography>
</CardContent>
</Link>
<CardActions className={classes.settings} disableSpacing>
<Chip
label="HTML"
Expand All @@ -159,7 +186,7 @@ export default function CardWithPicture(props) {
className={classes.time}
data-testId="Time"
>
{props.time}
{"10 min"}
</Typography>
<div className={classes.grow} />
<ToggleButtonGroup
Expand Down
80 changes: 52 additions & 28 deletions src/components/Card/CardWithoutPicture.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { Link } from "react-router-dom";
import { makeStyles } from "@mui/styles";
import Card from "@mui/material/Card";
import CardHeader from "@mui/material/CardHeader";
Expand All @@ -17,7 +18,9 @@ import ToggleButton from "@mui/lab/ToggleButton";
import ToggleButtonGroup from "@mui/lab/ToggleButtonGroup";
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
import KeyboardArrowDownIcon from "@mui/icons-material/KeyboardArrowDown";

import { useDispatch, useSelector } from "react-redux";
import { useFirebase, useFirestore } from "react-redux-firebase";
import { getUserProfileData } from "../../store/actions";
const useStyles = makeStyles(theme => ({
root: {
margin: "0.5rem",
Expand Down Expand Up @@ -62,10 +65,13 @@ const useStyles = makeStyles(theme => ({
}
}));

export default function CardWithoutPicture(props) {
export default function CardWithoutPicture({ tutorial }) {
const classes = useStyles();
const [alignment, setAlignment] = React.useState("left");
const [count, setCount] = useState(1);
const dispatch = useDispatch();
const firebase = useFirebase();
const firestore = useFirestore();
const handleIncrement = () => {
setCount(count + 1);
};
Expand All @@ -78,16 +84,32 @@ export default function CardWithoutPicture(props) {
setAlignment(newAlignment);
};

useEffect(() => {
getUserProfileData(tutorial?.created_by)(firebase, firestore, dispatch);
}, [tutorial]);

const user = useSelector(
({
profile: {
user: { data }
}
}) => data
);

const getTime = timestamp => {
return timestamp.toDate().toDateString();
};

return (
<Card className={classes.root}>
<CardHeader
avatar={
<Avatar
aria-label="recipe"
className={classes.avatar}
data-testId="UserAvatar"
>
S
<Avatar className={classes.avatar}>
{user?.photoURL && user?.photoURL.length > 0 ? (
<img src={user?.photoURL} />
) : (
user?.displayName[0]
)}
</Avatar>
}
title={
Expand All @@ -99,9 +121,9 @@ export default function CardWithoutPicture(props) {
color="textPrimary"
data-testId="UserName"
>
{props.name}
{user?.displayName}
</Typography>
{props.organizationName && (
{tutorial?.owner && (
<>
{" for "}
<Typography
Expand All @@ -111,28 +133,30 @@ export default function CardWithoutPicture(props) {
color="textPrimary"
data-testId="UserOrgName"
>
{props.organizationName}
{tutorial?.owner}
</Typography>
</>
)}
</React.Fragment>
}
subheader={props.date}
subheader={tutorial?.createdAt ? getTime(tutorial?.createdAt) : ""}
/>
<CardContent className={classes.contentPadding}>
<Typography variant="h5" color="text.primary" data-testId="Title">
{props.title}
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="p"
paragraph
data-testId="Description"
>
{props.contentDescription}
</Typography>
</CardContent>
<Link to={`/tutorial/${tutorial?.tutorial_id}`}>
<CardContent className={classes.contentPadding}>
<Typography variant="h5" color="text.primary" data-testId="Title">
{tutorial?.title}
</Typography>
<Typography
variant="body2"
color="textSecondary"
component="p"
paragraph
data-testId="Description"
>
{tutorial?.summary}
</Typography>
</CardContent>
</Link>
<CardActions className={classes.settings} disableSpacing>
<Chip
label="HTML"
Expand All @@ -148,7 +172,7 @@ export default function CardWithoutPicture(props) {
className={classes.time}
data-testId="Time"
>
{props.time}
{"10 min"}
</Typography>
<div className={classes.grow} />
<ToggleButtonGroup
Expand Down
Loading
Loading