Skip to content

Commit

Permalink
add avatar generator example
Browse files Browse the repository at this point in the history
  • Loading branch information
always-maap committed Aug 11, 2022
1 parent f67158c commit 31b3400
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/avatar-generator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
13 changes: 13 additions & 0 deletions examples/avatar-generator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions examples/avatar-generator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "avatar-generator",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@vitejs/plugin-react": "^2.0.0",
"vite": "^3.0.0"
}
}
1 change: 1 addition & 0 deletions examples/avatar-generator/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions examples/avatar-generator/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.wrapper {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 20px;
background-color: white;
border-radius: 5px;
box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
padding: 15px;
width: 500px
}

.title {
font-size: 1.5em;
}
38 changes: 38 additions & 0 deletions examples/avatar-generator/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useState } from "react";
import "./App.css";
import UserCard from "./components/UserCard";
import Button from "./components/Button";

function App() {
const [name, setName] = useState("");
const [avatar, setAvatar] = useState("");
const [isLoading, setIsLoading] = useState(false);

async function getAvatar(name, gender) {
const url = `https://avatars.dicebear.com/api/${gender}/${name}.svg`;
setAvatar(url);
}

async function generateRandomUser() {
setIsLoading(true);
const userRes = await fetch(
"https://randomuser.me/api/?inc=gender,name&results=1"
);
const userData = await userRes.json();
const fullName = `${userData.results[0].name.title} ${userData.results[0].name.first} ${userData.results[0].name.last}`;
const gender = userData.results[0].gender;
setIsLoading(false);
setName(fullName);
getAvatar(fullName, gender);
}

return (
<div className="wrapper">
<h1 align="center">Avatar Generator</h1>
<Button onClick={() => generateRandomUser()}>Generate User</Button>
<UserCard isLoading={isLoading} name={name} avatar={avatar} />
</div>
);
}

export default App;
1 change: 1 addition & 0 deletions examples/avatar-generator/src/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/avatar-generator/src/components/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import styles from "./Button.module.css";

export default function Button(props) {
return (
<button className={styles.btn} {...props}>
{props.children}
</button>
);
}
17 changes: 17 additions & 0 deletions examples/avatar-generator/src/components/Button.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.btn {
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px 20px;
background: none;
font-size: 1.3em;
cursor: pointer;
}

.btn:hover {
background: #ccc;
}

.btn:active {
transition: all 0.2s;
transform: translateY(2px);
}
22 changes: 22 additions & 0 deletions examples/avatar-generator/src/components/UserCard.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import styles from "./UserCard.module.css";

export default function UserCard(props) {
if (!props.name || !props.avatar) {
return (
<div className={styles.wrapper} styles={{ textAlign: "center" }}>
Please click on generate avatar
</div>
);
}

if (props.isLoading) {
return <div className={styles.wrapper}>loading...</div>;
}

return (
<div className={styles.wrapper}>
<h2 className={styles.title}>{props.name}</h2>
<img width={150} height={150} src={props.avatar} />
</div>
);
}
11 changes: 11 additions & 0 deletions examples/avatar-generator/src/components/UserCard.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.wrapper {
background: lightblue;
padding: 20px;
width: 100%;
border: 1px solid black;
border-radius: 10px;
height: 200px;
display: flex;
align-items: center;
justify-content: space-between;
}
15 changes: 15 additions & 0 deletions examples/avatar-generator/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
* {
box-sizing: border-box;
}

:root {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
color: #333;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-image: linear-gradient(to right, #439bff, #2f61fe);
height: 100vh;
}
10 changes: 10 additions & 0 deletions examples/avatar-generator/src/main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App'
import './index.css'

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
7 changes: 7 additions & 0 deletions examples/avatar-generator/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
});

1 comment on commit 31b3400

@vercel
Copy link

@vercel vercel bot commented on 31b3400 Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nit-news – ./

nit-news.vercel.app
nit-news-git-master-always-maap.vercel.app
nit-news-always-maap.vercel.app

Please sign in to comment.