Skip to content

Commit

Permalink
feat: local fridge now stores data (see #1)
Browse files Browse the repository at this point in the history
  • Loading branch information
aimed committed Aug 29, 2018
1 parent a6c4988 commit 357f1c5
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions client/src/fridge/FridgeLocal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export interface FridgeLocalProps {
}

export class FridgeLocal extends React.Component<WithApolloClient<FridgeLocalProps>, FridgeLocalState> {
public state: FridgeLocalState = {
fridge: {
ingredients: {
edges: []
public state: FridgeLocalState = (window && window.localStorage && window.localStorage.getItem('localFridge'))
? JSON.parse(window.localStorage.getItem('localFridge')!)
: {
fridge: {
ingredients: {
edges: []
}
}
}
}

public get edges(): FridgeContentViewer_viewer_fridge_ingredients_edges[] {
return this.state.fridge.ingredients.edges;
Expand Down Expand Up @@ -76,7 +78,12 @@ export class FridgeLocal extends React.Component<WithApolloClient<FridgeLocalPro
};
edges = [...this.edges, newEdge];
}
this.setState({ fridge: { ingredients: { edges } } });

const nextState = { fridge: { ingredients: { edges } } };
if (window && window.localStorage) {
window.localStorage.setItem('localFridge', JSON.stringify(nextState));
}
this.setState(nextState);
}

public render() {
Expand Down

0 comments on commit 357f1c5

Please sign in to comment.