Skip to content

Commit

Permalink
✨ Now tried the separated play header for counter app
Browse files Browse the repository at this point in the history
  • Loading branch information
atapas committed Mar 16, 2022
1 parent 79549da commit 0cd01a6
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 60 deletions.
2 changes: 1 addition & 1 deletion plop-templates/play.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
path: '/plays/{{folder}}',
level: '{{level}}',
tags: '{{tags}}',
github: 'https://github.com/{{github}}',
github: '{{github}}',
blog: '{{blog}}',
video: '{{video}}'
}, //replace new play item here
29 changes: 29 additions & 0 deletions src/common/hooks/useGitHub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


import { useState, useEffect } from 'react';

const useGitHub = (github) => {
const [data, setData] = useState();
const [error, setError] = useState();
const [isLoading, setIsLoading] = useState(true);

useEffect(() => {
const fetchData = async () => {
setIsLoading(true);
try {
const response = await fetch(`https://api.github.com/users/${github}`);
const responseData = await response.json();
setData(responseData);
setIsLoading(false);
} catch (error) {
setError(error);
setIsLoading(false);
}
};
fetchData();
}, [github]);

return { data, error, isLoading };
};

export default useGitHub;
110 changes: 78 additions & 32 deletions src/common/playlists/PlayHeaderInfo.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,91 @@
import { Link } from "react-router-dom";
import { useState, useEffect } from "react";
import { RiMedal2Fill } from "react-icons/ri";
import { IoMdTrophy } from "react-icons/io";
import { IoRibbon } from "react-icons/io5";

import useGitHub from 'common/hooks/useGitHub';

const LevelBadge = ({ level }) => {
const [playLevel, setPlayLevel] = useState();

useEffect(() => {
switch (level) {
case "Beginner":
setPlayLevel(
<span className="play-level play-level-1">
<IoRibbon size="16px" /> {level}
</span>);
break;
case "Intermediate":
setPlayLevel(
<span className="play-level play-level-2">
<RiMedal2Fill size="16px" /> {level}
</span>);
break;
case "Advanced":
setPlayLevel(
<span className="play-level play-level-3">
<IoMdTrophy size="16px" /> {level}
</span>);
break;
default:
setPlayLevel(
<span className="play-level play-level-1">
<IoRibbon size="16px" /> {level}
</span>);
}
}, [level]);
return (
<>{playLevel}</>
);
};

const Author = ({github}) => {
const { data, error, isLoading } = useGitHub(github);
return (
<>
{isLoading && <span>Loading...</span>}
{error && <span>Error: {error.message}</span>}
{data && (
<div className="header-author">
by <a
href={`https://github.com/${github}`}
target="_blank"
rel="noopener noreferrer">
<strong>{data.name}</strong>
</a>
</div>
)}
</>
);
};

const Tags = ({tags}) => {
return (
<ul className="list-tags">
{tags.map((tag, index) => (
<li key={index}>
<span className="play-tag">
{tag}
</span>
</li>
))}
</ul>
);
};

const PlayHeaderInfo = ({ play }) => {

return (
<div>
<div className="header-wrapper">
<h3 className="header-title">Header Title Goes here</h3>
<Link to="google.com" className="play-level play-level-1">
<IoRibbon size="16px" />
Beginner
</Link>
<Link to="google.com" className="play-level play-level-2">
<RiMedal2Fill size="16px" /> Internemdiate
</Link>
<Link to="google.com" className="play-level play-level-3">
<IoMdTrophy size="16px" /> Advanced
</Link>
<h3 className="header-title">{play.name}</h3>
<LevelBadge level={play.level} />
</div>
<div>{play.description}</div>
<div className="header-secondary">
<div className="header-author">
by <strong>GreenRoots Media</strong>
</div>
<ul className="list-tags">
<li>
<Link className="play-tag" to="google.com">
Schedule
</Link>
</li>
<li>
<Link className="play-tag" to="google.com">
Hook
</Link>
</li>
<li>
<Link className="play-tag" to="google.com">
Tree
</Link>
</li>
</ul>
{ play.github && <Author github={play.github} /> }
{ play.tags && <Tags tags={play.tags.split(', ')} /> }
</div>
</div>
);
Expand Down
14 changes: 7 additions & 7 deletions src/meta/play-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const plays = [
index: true,
level: 'Beginner',
tags: 'JSX',
github: 'https://github.com/atapas'
github: 'atapas'
},
{
id: 'pl-0002',
Expand All @@ -28,7 +28,7 @@ const plays = [
path: '/plays/clock',
level: 'Beginner',
tags: 'JSX, Schedule',
github: 'https://github.com/atapas',
github: 'atapas',
blog: 'https://blog.greenroots.info',
video: 'https://youtube.com/tapasadhikary'
},
Expand All @@ -40,7 +40,7 @@ const plays = [
path: '/plays/countdown',
level: 'Intermediate',
tags: 'Schedule, Component Structure, Hooks, Custom Hooks',
github: 'https://github.com/atapas'
github: 'atapas'
},
{
id: 'pl-0004',
Expand All @@ -50,7 +50,7 @@ const plays = [
path: '/plays/movies',
level: 'Intermediate',
tags: 'Fetch Data, Hooks',
github: 'https://github.com/atapas'
github: 'atapas'
},
{
id: 'pl-0005',
Expand All @@ -60,16 +60,16 @@ const plays = [
path: '/plays/basic-family-tree',
level: 'Intermediate',
tags: 'Recursion, Tree',
github: 'https://github.com/green-roots'
github: 'green-roots'
}, {
id: 'pl-counter',
name: 'Counter',
description: 'A simple counter which increments the value upto a certain limit!',
component: () => {return <CounterApp />},
path: '/plays/Counter',
path: '/plays/counter',
level: 'Beginner',
tags: 'JSX, State, Props',
github: 'https://github.com/murtuzaalisurti'
github: 'murtuzaalisurti'
}, //replace new play item here
];

Expand Down
7 changes: 2 additions & 5 deletions src/plays/clock/CurrentTimer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

import PlayHeader from 'common/playlists/PlayHeader';
import { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom';
import { getPlayById } from 'meta/play-meta';

import PlayHeader from 'common/playlists/PlayHeader';



const CurrentTimer = () => {
// The following code is to fetch the current play from the URL
const location = useLocation();
Expand Down Expand Up @@ -33,7 +31,6 @@ const CurrentTimer = () => {
</div>
</div>
</>

);
}

Expand Down
55 changes: 40 additions & 15 deletions src/plays/counter/CounterApp.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
import {useState} from 'react';
import Counter from './Counter';
import './counter.css';
import PlayHeader from "common/playlists/PlayHeader";
import { useState } from 'react';
import { useLocation } from 'react-router-dom';
import { getPlayById } from 'meta/play-meta';
import Counter from "./Counter";
import "./counter.css";

function CounterApp() {
// The following code is to fetch the current play from the URL
const location = useLocation();
const { id } = location.state;
const play = getPlayById(id);

const [input, setInput] = useState("");
const [times, setTimes] = useState(33);
function register(){
function register() {
let no_times = Number(input);
console.log(no_times)
console.log(no_times);
setTimes(no_times);
setInput("");
}
return (
<div className="counter-container">
<h1>Counter</h1>
<p>A simple counter</p>
<p className="counter-desc">Specify the limit and click the circle below to increment the value until the limit has been reached. After that it will reset to zero.</p>
<div className="input_field">
<input value={input} onChange={e => setInput(e.target.value)} className="no_of_times" type="text" />
<button onClick={register}>Submit</button>
<>
<div className="play-details">
<PlayHeader play={play} />
<div className="play-details-body">
<div className="counter-container">
<h1>Counter</h1>
<p>A simple counter</p>
<p className="counter-desc">
Specify the limit and click the circle below to increment the
value until the limit has been reached. After that it will reset
to zero.
</p>
<div className="input_field">
<input
value={input}
onChange={(e) => setInput(e.target.value)}
className="no_of_times"
type="text"
/>
<button onClick={register}>Submit</button>
</div>
<Counter times={times} />
</div>
</div>
</div>
<Counter times={times}/>
</div>
</>
);
}

export default CounterApp;
export default CounterApp;

0 comments on commit 0cd01a6

Please sign in to comment.