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

Finished project with hooks #42

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.DS_STORE
.env
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
# Lyrical-GraphQL
Starter project from a GraphQL course on Udemy.com

### How to run
1. Install dependencies
```
npm install
```
2. Create your database at MongoDB Atlas and copy/paste its `MONGO URI` into a `.env` file in the root folder:
```
MONGO_URI = "..."
```
3. Run the project
```
npm run dev
```
7 changes: 7 additions & 0 deletions client/components/Layout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from "react";

const Layout = ({ children }) => {
return <div className="container">{children}</div>;
};

export default Layout;
48 changes: 48 additions & 0 deletions client/components/LyricCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { useState } from "react";
import { gql, useMutation } from "@apollo/client";

const mutation = gql`
mutation AddLyric($id: ID!, $content: String!) {
addLyricToSong(songId: $id, content: $content) {
id
lyrics {
id
content
likes
}
}
}
`;

const LyricCreate = ({ songId }) => {
const [content, setContent] = useState("");
const [mutateFunction] = useMutation(mutation);

const handleSubmit = (event) => {
event.preventDefault();

mutateFunction({
variables: {
id: songId,
content,
},
});

setContent("");
};

return (
<form onSubmit={handleSubmit}>
<label>Add a lyric:</label>
<input
type="text"
value={content}
onChange={(event) => {
setContent(event.target.value);
}}
></input>
</form>
);
};

export default LyricCreate;
53 changes: 53 additions & 0 deletions client/components/LyricList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";
import { gql, useMutation } from "@apollo/client";

const mutation = gql`
mutation AddLike($id: ID!) {
likeLyric(id: $id) {
id
likes
}
}
`;

const LyricList = ({ lyrics }) => {
const [mutateFunction] = useMutation(mutation);

const onLike = (id, currentLikes) => {
mutateFunction({
variables: {
id,
},
optimisticResponse: {
__typename: "Mutation",
likeLyric: {
id,
__typename: "LyricType",
likes: currentLikes + 1,
},
},
});
};

const renderLyrics = () => {
return (lyrics || []).map(({ id, content, likes }) => {
return (
<li className="collection-item " key={id}>
{`${content} (${likes || 0})`}
<i
className="material-icons"
onClick={() => {
onLike(id, likes);
}}
>
thumb_up
</i>
</li>
);
});
};

return <ul className="collection">{renderLyrics()}</ul>;
};

export default LyricList;
55 changes: 55 additions & 0 deletions client/components/SongCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState } from "react";
import { gql, useMutation } from "@apollo/client";
import { Link, useHistory } from "react-router-dom";
import songs from "../queries/songs";

const mutation = gql`
mutation AddSong($title: String) {
addSong(title: $title) {
id
title
}
}
`;

const SongCreate = () => {
const [mutateFunction] = useMutation(mutation);
const [title, setTitle] = useState("");
const history = useHistory();

const onSubmit = (event) => {
event.preventDefault();

mutateFunction({
variables: {
title,
},
refetchQueries: [
{
query: songs,
},
],
}).then(() => {
history.push("/");
});
};

return (
<div>
<Link to="/">Back </Link>
<h3>Create a new song</h3>
<form onSubmit={onSubmit}>
<label>Song title:</label>
<input
type="text"
value={title}
onChange={(event) => {
setTitle(event.target.value);
}}
></input>
</form>
</div>
);
};

export default SongCreate;
24 changes: 24 additions & 0 deletions client/components/SongDetail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { Link, useParams } from "react-router-dom";
import LyricCreate from "./LyricCreate";
import LyricList from "./LyricList";
import songQuery from "../queries/song";
import { useQuery } from "@apollo/client";

const SongDetail = () => {
const { id } = useParams();
const { data, loading } = useQuery(songQuery, {
variables: { id },
});

return (
<div>
<Link to="/">Back </Link>
<h3>{loading ? "Loading..." : data.song.title}</h3>
{!loading && <LyricList lyrics={data.song.lyrics} />}
<LyricCreate songId={id} />
</div>
);
};

export default SongDetail;
60 changes: 60 additions & 0 deletions client/components/SongList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import React from "react";
import { useQuery, gql, useMutation } from "@apollo/client";
import { Link } from "react-router-dom";
import songs from "../queries/songs";

const mutation = gql`
mutation DeleteSong($id: ID) {
deleteSong(id: $id) {
id
}
}
`;

const SongList = () => {
const { loading, data, refetch } = useQuery(songs);
const [mutateFunction] = useMutation(mutation);

const onSongDelete = (id) => {
mutateFunction({
variables: {
id,
},
}).then(() => {
refetch();
});
};

const renderSongs = () => {
return (data.songs || []).map(({ id, title }) => {
return (
<li className="collection-item " key={id}>
<Link to={`/song/${id}`}>{title}</Link>
<i
className="material-icons"
onClick={() => {
onSongDelete(id);
}}
>
delete
</i>
</li>
);
});
};

if (loading) {
return <div>Loading...</div>;
}

return (
<div>
<ul className="collection">{renderSongs()}</ul>
<Link to="/song/new" className="btn-floating btn-large red right">
<i className="material-icons">add</i>
</Link>
</div>
);
};

export default SongList;
49 changes: 41 additions & 8 deletions client/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
import React from 'react';
import ReactDOM from 'react-dom';
import React from "react";
import ReactDOM from "react-dom/client";
import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import { Route, Switch, HashRouter } from "react-router-dom";

const Root = () => {
return <div>Lyrical</div>
import Layout from "./components/Layout";
import SongList from "./components/SongList";
import SongCreate from "./components/SongCreate";
import SongDetail from "./components/SongDetail";

import "./style/style.css";

const apolloClient = new ApolloClient({
uri: "http://localhost:4000/graphql",
dataIdFromObject: (o) => o.id,
cache: new InMemoryCache(),
});

const App = () => {
return (
<ApolloProvider client={apolloClient}>
<Layout>
<HashRouter>
<Switch>
<Route path="/song/new">
<SongCreate />
</Route>
<Route path="/song/:id">
<SongDetail />
</Route>
<Route path="/">
<SongList />
</Route>
</Switch>
</HashRouter>
</Layout>
</ApolloProvider>
);
};

ReactDOM.render(
<Root />,
document.querySelector('#root')
);
const rootEl = document.querySelector("#root");
const root = ReactDOM.createRoot(rootEl);

root.render(<App />);
15 changes: 15 additions & 0 deletions client/queries/song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { gql } from "@apollo/client";

export default gql`
query GetLyricsOfSong($id: ID!) {
song(id: $id) {
id
title
lyrics {
id
content
likes
}
}
}
`;
10 changes: 10 additions & 0 deletions client/queries/songs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { gql } from "@apollo/client";

export default gql`
query GetSongs {
songs {
id
title
}
}
`;
8 changes: 8 additions & 0 deletions client/style/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.collection-item {
display: flex;
justify-content: space-between;
}

.material-icons {
cursor: pointer;
}
Loading