-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
175 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,41 @@ | ||
import { API_HOST } from '../config/constants'; | ||
import { DEFAULT_GET, DEFAULT_POST } from './utils'; | ||
import { DEFAULT_DELETE, DEFAULT_GET, DEFAULT_POST } from './utils'; | ||
|
||
// payload = {email} | ||
export const getOwnItems = async () => { | ||
const req = await fetch(`${API_HOST}/own`, { | ||
const req = await fetch(`${API_HOST}/items/own`, { | ||
...DEFAULT_GET, | ||
headers: { 'Content-Type': 'application/json' }, | ||
}); | ||
// eslint-disable-next-line no-console | ||
console.log(req); | ||
return req.json(); | ||
}; | ||
|
||
// payload = {email} | ||
export const createItem = async () => { | ||
const req = await fetch(`${API_HOST}/items`, { | ||
// payload = {name, type, description, extra} | ||
// querystring = {parentId} | ||
export const createItem = async ({ | ||
name, | ||
type, | ||
description, | ||
extra, | ||
parentId, | ||
} = {}) => { | ||
let url = `${API_HOST}/items`; | ||
if (parentId) { | ||
url += `?parentId=${parentId}`; | ||
} | ||
const req = await fetch(url, { | ||
...DEFAULT_POST, | ||
body: JSON.stringify({ name: 'myitem' }), | ||
body: JSON.stringify({ name, type, description, extra }), | ||
}); | ||
// eslint-disable-next-line no-console | ||
console.log(await req.json()); | ||
return req.json(); | ||
}; | ||
|
||
// payload = {name, type, description, extra} | ||
// querystring = {parentId} | ||
export const deleteItem = async (id) => { | ||
const req = await fetch(`${API_HOST}/items/${id}`, DEFAULT_DELETE); | ||
return req.json(); | ||
}; | ||
|
||
// we need this function for navigation purposes: when you click on an item, you want to see its 'immediate' children | ||
// eslint-disable-next-line import/prefer-default-export | ||
export const fetchItemImmediateChildren = () => {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { getOwnItems, deleteItem } from '../../api/item'; | ||
import sampleItems from '../../data/sample'; | ||
|
||
const ItemContext = React.createContext(); | ||
|
||
class ItemProvider extends Component { | ||
static propTypes = { | ||
children: PropTypes.oneOfType([ | ||
PropTypes.arrayOf(PropTypes.node), | ||
PropTypes.node, | ||
]).isRequired, | ||
}; | ||
|
||
state = { | ||
items: [], | ||
}; | ||
|
||
async componentDidMount() { | ||
const ownedItems = (await getOwnItems()) || sampleItems; | ||
this.setState({ items: ownedItems }); | ||
} | ||
|
||
addItem = (item) => { | ||
const { items } = this.state; | ||
this.setState({ items: [...items, item] }); | ||
}; | ||
|
||
setItems = (items) => { | ||
this.setState({ items }); | ||
}; | ||
|
||
deleteItem = async (id) => { | ||
const { items } = this.state; | ||
const deletedItem = await deleteItem(id); | ||
if (deletedItem.status === 404) { | ||
return console.error(`Couldn't delete item ${id}`); | ||
} | ||
return this.setState({ | ||
items: items.filter(({ id: thisId }) => thisId !== id), | ||
}); | ||
}; | ||
|
||
buildValue = () => { | ||
const { items } = this.state; | ||
return { | ||
items, | ||
addItem: this.addItem, | ||
deleteItem: this.deleteItem, | ||
}; | ||
}; | ||
|
||
render() { | ||
const { children } = this.props; | ||
return ( | ||
<ItemContext.Provider value={this.buildValue()}> | ||
{children} | ||
</ItemContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
export { ItemProvider, ItemContext }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,29 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import React, { Component } from 'react'; | ||
import Grid from '@material-ui/core/Grid'; | ||
import ItemsHeader from './ItemsHeader'; | ||
import CreateNewItemButton from './CreateNewItemButton'; | ||
import Item from './Item'; | ||
import { ItemContext } from '../context/item'; | ||
|
||
const Items = ({ items }) => { | ||
return ( | ||
<div> | ||
<ItemsHeader /> | ||
<CreateNewItemButton /> | ||
<Grid container spacing={1}> | ||
{items.map((item) => ( | ||
<Grid key={item.id} item xs={12} sm={6} md={4} lg={3} xl={2}> | ||
<Item item={item} /> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</div> | ||
); | ||
}; | ||
class Items extends Component { | ||
static contextType = ItemContext; | ||
|
||
Items.propTypes = { | ||
items: PropTypes.arrayOf(PropTypes.shape({})).isRequired, | ||
}; | ||
render() { | ||
const { items } = this.context; | ||
return ( | ||
<div> | ||
<ItemsHeader /> | ||
<CreateNewItemButton /> | ||
<Grid container spacing={1}> | ||
{items.reverse().map((item) => ( | ||
<Grid key={item.id} item xs={12} sm={6} md={4} lg={3} xl={2}> | ||
<Item item={item} /> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Items; |