Skip to content

Commit 2c4c63e

Browse files
committed
chore
0 parents  commit 2c4c63e

27 files changed

+9918
-0
lines changed

.gitignore

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.DS_Store
2+
3+
# Logs
4+
logs
5+
*.log
6+
npm-debug.log*
7+
yarn-debug.log*
8+
yarn-error.log*
9+
10+
# Runtime data
11+
pids
12+
*.pid
13+
*.seed
14+
*.pid.lock
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# nyc test coverage
23+
.nyc_output
24+
25+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
26+
.grunt
27+
28+
# Bower dependency directory (https://bower.io/)
29+
bower_components
30+
31+
# node-waf configuration
32+
.lock-wscript
33+
34+
# Compiled binary addons (https://nodejs.org/api/addons.html)
35+
build/Release
36+
37+
# Dependency directories
38+
node_modules/
39+
jspm_packages/
40+
41+
# TypeScript v1 declaration files
42+
typings/
43+
44+
# Optional npm cache directory
45+
.npm
46+
47+
# Optional eslint cache
48+
.eslintcache
49+
50+
# Optional REPL history
51+
.node_repl_history
52+
53+
# Output of 'npm pack'
54+
*.tgz
55+
56+
# Yarn Integrity file
57+
.yarn-integrity
58+
59+
# dotenv environment variables file
60+
.env
61+
62+
# next.js build output
63+
.next
64+
dist
65+
coverage
66+
.cache

api/src/db/db.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"user": {
3+
"id": "jBWMVGjm50l6LGwepDoty",
4+
"username": "frontendmaster"
5+
},
6+
"pet": []
7+
}

api/src/db/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const low = require('lowdb')
2+
const FileSync = require('lowdb/adapters/FileSync')
3+
4+
const adapter = new FileSync('api/src/db/db.json')
5+
const db = low(adapter)
6+
7+
const createPetModel = require('./pet')
8+
const createUserModel = require('./user')
9+
10+
module.exports = {
11+
models: {
12+
Pet: createPetModel(db),
13+
User: createUserModel(db),
14+
},
15+
db
16+
}

api/src/db/pet.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const nanoid = require('nanoid')
2+
3+
const createPetModel = db => {
4+
return {
5+
findMany(filter) {
6+
return db.get('pet')
7+
.filter(filter)
8+
.value()
9+
},
10+
11+
findOne(filter) {
12+
return db.get('pet')
13+
.find(filter)
14+
.value()
15+
},
16+
17+
create(pet) {
18+
const newPet = {id: nanoid(), createdAt: Date.now(), ...pet}
19+
20+
db.get('pet')
21+
.push(newPet)
22+
.write()
23+
24+
return newPet
25+
}
26+
}
27+
}
28+
29+
module.exports = createPetModel

api/src/db/user.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const nanoid = require('nanoid')
2+
3+
const createUserModel = db => {
4+
return {
5+
findOne() {
6+
return db.get('user')
7+
.value()
8+
},
9+
10+
create(user) {
11+
const newUser = {id: nanoid(), createdAt: Date.now(), ...user}
12+
db.set('user', newUser)
13+
.write()
14+
15+
return newUser
16+
}
17+
}
18+
}
19+
20+
module.exports = createUserModel

api/src/resolvers.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module.exports = {
2+
Query: {
3+
pets(_, {input}, {models}) {
4+
return models.Pet.findMany(input || {})
5+
},
6+
pet(_, {id}, {models}) {
7+
return models.Pet.findOne({id})
8+
},
9+
user(_, __, {models}) {
10+
return models.User.findOne()
11+
}
12+
},
13+
Mutation: {
14+
addPet(_, {input}, {models, user}) {
15+
const pet = models.Pet.create({...input, user: user.id})
16+
return pet
17+
}
18+
},
19+
Pet: {
20+
owner(pet, _, {models}) {
21+
return models.User.findOne({id: pet.user})
22+
},
23+
img(pet) {
24+
return pet.type === 'DOG'
25+
? 'https://placedog.net/300/300'
26+
: 'http://placekitten.com/300/300'
27+
}
28+
},
29+
User: {
30+
pets(user, _, {models}) {
31+
return models.Pet.findMany({user: user.id})
32+
}
33+
}
34+
}

api/src/schema.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const { gql } = require('apollo-server')
2+
3+
const typeDefs = gql`
4+
enum PetType {
5+
CAT
6+
DOG
7+
}
8+
9+
type User {
10+
id: ID!
11+
username: String!
12+
pets: [Pet]!
13+
}
14+
15+
type Pet {
16+
id: ID!
17+
type: PetType!
18+
name: String!
19+
owner: User!
20+
img: String!
21+
createdAt: Int!
22+
}
23+
24+
input NewPetInput {
25+
name: String!
26+
type: PetType!
27+
}
28+
29+
input PetsInput {
30+
type: PetType
31+
}
32+
33+
type Query {
34+
user: User!
35+
pets(input: PetsInput): [Pet]!
36+
pet(id: ID!): Pet!
37+
}
38+
39+
type Mutation {
40+
addPet(input: NewPetInput!): Pet!
41+
}
42+
`;
43+
44+
module.exports = typeDefs

api/src/server.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const { ApolloServer } = require('apollo-server')
2+
const typeDefs = require('./schema')
3+
const resolvers = require('./resolvers')
4+
const {models, db} = require('./db')
5+
6+
const server = new ApolloServer({
7+
typeDefs,
8+
resolvers,
9+
context() {
10+
const user = db.get('user').value()
11+
return {models, db, user}
12+
}
13+
})
14+
15+
server.listen().then(({ url }) => {
16+
console.log(`🚀 Server ready at ${url}`);
17+
})

api/tests/mutations.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('mutations', () => {
2+
test('hello', () => {
3+
expect(1).toBe(1)
4+
})
5+
})

api/tests/queries.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('queries', () => {
2+
test('hello', () => {
3+
expect(1).toBe(1)
4+
})
5+
})

api/tests/resolvers.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('resolvers', () => {
2+
test('hello', () => {
3+
expect(1).toBe(1)
4+
})
5+
})

client/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["env", "react"],
3+
"plugins": ["emotion", "transform-object-rest-spread"]
4+
}

client/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>React starter app</title>
5+
</head>
6+
<body>
7+
<div id="app"></div>
8+
<script src="./src/index.js"></script>
9+
</body>
10+
</html>

client/src/client.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ApolloClient } from 'apollo-client'
2+
import { InMemoryCache } from 'apollo-cache-inmemory'
3+
import { HttpLink } from 'apollo-link-http'
4+
5+
const cache = new InMemoryCache()
6+
const link = new HttpLink({
7+
uri: 'http://localhost:4000/'
8+
})
9+
10+
const client = new ApolloClient({
11+
cache,
12+
link
13+
})
14+
15+
export default client

client/src/components/App.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Switch, Route } from 'react-router-dom'
2+
import React, {Fragment} from 'react'
3+
import Header from './Header'
4+
import Pets from '../pages/Pets'
5+
6+
const App = () => (
7+
<Fragment>
8+
<Header />
9+
<div>
10+
<Switch>
11+
<Route exact path="/" component={Pets} />
12+
</Switch>
13+
</div>
14+
</Fragment>
15+
)
16+
17+
export default App

client/src/components/Error.js

Whitespace-only changes.

client/src/components/Header.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react'
2+
import { Link } from 'react-router-dom'
3+
import { withRouter } from 'react-router'
4+
5+
const Header = () =>
6+
<header>
7+
<div className="row">
8+
<div className="col-xs">
9+
<Link to="/" >
10+
Home
11+
</Link>
12+
</div>
13+
</div>
14+
</header>
15+
16+
export default withRouter(Header)

client/src/components/Loader.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import ClipLoader from 'react-spinners/ClipLoader'
3+
4+
const Loader = () =>
5+
<div className='full-page-loader'>
6+
<ClipLoader
7+
sizeUnit={"px"}
8+
size={150}
9+
color={'#3454D1'}
10+
loading={true}
11+
/>
12+
</div>
13+
14+
export default Loader

client/src/components/NewPet.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, {useState} from 'react'
2+
import Select from 'react-select'
3+
4+
const options = [
5+
{ value: 'CAT', label: 'Cat' },
6+
{ value: 'DOG', label: 'Dog' }
7+
]
8+
9+
export default function NewPet({onSubmit, onCancel}) {
10+
const [type, setType] = useState('DOG')
11+
const [name, setName] = useState('')
12+
13+
const activeOption = options.find(o => o.value === type)
14+
15+
const submit = e => {
16+
e.preventDefault()
17+
onSubmit({name, type})
18+
}
19+
20+
const cancel = e => {
21+
e.preventDefault()
22+
onCancel()
23+
}
24+
25+
return (
26+
<div className="new-pet page">
27+
<h1>New Pet</h1>
28+
<div className="box">
29+
<form onSubmit={submit}>
30+
<Select
31+
value={activeOption}
32+
defaultValue={options[0]}
33+
onChange={e => setType(e.value)}
34+
options={options}
35+
/>
36+
37+
<input
38+
className="input"
39+
type="text"
40+
placeholder="pet name"
41+
value={name}
42+
onChange={e => setName(e.target.value)}
43+
required
44+
/>
45+
<a className="error button" onClick={cancel}>cancel</a>
46+
<button type="submit" name="submit">add pet</button>
47+
</form>
48+
</div>
49+
</div>
50+
)
51+
}

0 commit comments

Comments
 (0)