Skip to content

Commit

Permalink
bugfix e miglioramenti alla gestione dell'utente ospite
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe16180 committed Jan 6, 2020
1 parent f93b432 commit 6a1aa68
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 79 deletions.
34 changes: 25 additions & 9 deletions src/Components/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,26 @@ class Cart extends React.Component {
this.provider
.doRemoveCartEntry(id, 1)
.then(alert("Rimosso"))
.catch("Riprova più tardi");
.catch(error => {
if (error == "NoResults") {
alert("Impossibile rimuovere");
} else if (error == "FailedToFetch") {
alert("Impossibile contattere il server");
}
});
};

handleRemoveQuantity = (id, quantity) => {
this.provider
.doRemoveCartEntry(id, quantity)
.then(alert("Rimosso tutto"))
.catch("Riprova più tardi");
.catch(error => {
if (error == "NoResults") {
alert("Impossibile rimuovere");
} else if (error == "FailedToFetch") {
alert("Impossibile contattere il server");
}
});
};

render() {
Expand All @@ -75,10 +87,7 @@ class Cart extends React.Component {
/>
)}
{!this.provider.isGuest() && (
<Button
text={"Logout"}
onPress={this.provider.doLogout}
/>
<Button text={"Logout"} onPress={this.provider.doLogout} />
)}
</View>
</HeaderCard>
Expand All @@ -88,10 +97,17 @@ class Cart extends React.Component {
<View style={styles.searchRow}>
<View style={styles.suggestionsRow}>
<SubTitle text={"Procedi all'ordine!"} />
<Label text={"Totale " + (this.state.total != null ? this.state.total : 0) + "€"} />
<Label
text={
"Totale " +
(this.state.total != null ? this.state.total : 0) +
"€"
}
/>
</View>
<View style={{opacity: (this.state.total != null)? 1 : 0.4}}
pointerEvents={(this.state.total != null)? "auto" : "none"}
<View
style={{ opacity: this.state.total != null ? 1 : 0.4 }}
pointerEvents={this.state.total != null ? "auto" : "none"}
>
<Button
text={"check out"}
Expand Down
99 changes: 55 additions & 44 deletions src/Components/CustomerHome.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,39 @@ class CustomerHome extends React.Component {
this.provider = new DataProvider();
console.log("customer token", this.provider.token);
this.state = {
specialOffers: []
specialOffers: [],
isReady: false
};
}

componentDidMount() {
console.debug("CustomerHome", "componentDidMount");
this.provider
.doGetSpecialOffers()
.then(response => {
console.log(response);
this.setState({ specialOffers: response });
})
.catch(err => {
if (err == "FailedToFetch")
alert("Impossibile contattare il server! Riprova più tardi");
});
if (!this.provider.isGuest()) {
this.provider
.doGetSpecialOffers()
.then(response => {
console.log(response);
this.setState({ specialOffers: response, isReady: true });
})
.catch(err => {
if (err === "NoResults") {
alert("");
} else if (err === "FailedToFetch") {
alert("Impossibile contattare il server! Riprova più tardi");
}
});
}
}

handleAddToCart = id => {
this.provider
.doAddToCart(id)
.then(alert("Aggiunto al carrelllo!"))
.catch("Impossibile aggiungere al carrelllo");
.then(alert("Aggiunto al carrello!"))
.catch(error => {
if (error == "FailedToFetch") {
alert("Impossibile contattere il server");
}
});
};

render() {
Expand All @@ -65,10 +75,7 @@ class CustomerHome extends React.Component {
onPress={this.provider.navigateOrders}
/>
<Button text={"il tuo profilo 👤"} />
<Button
text={"Logout"}
onPress={this.provider.doLogout}
/>
<Button text={"Logout"} onPress={this.provider.doLogout} />
</View>
)}
</HeaderCard>
Expand All @@ -89,34 +96,38 @@ class CustomerHome extends React.Component {
</View>
</FlatCard>
<Separator />
<FlatCard>
<View style={styles.suggestionsRow}>
<SubTitle text={"Ecco i nostri suggerimenti"} />
<Label
text={
"Perché non dai uno sguardo alle offerte speciali a te riservate?"
}
/>
</View>
</FlatCard>
{this.state.isReady && (
<FlatCard>
<View style={styles.suggestionsRow}>
<SubTitle text={"Ecco i nostri suggerimenti"} />
<Label
text={
"Perché non dai uno sguardo alle offerte speciali a te riservate?"
}
/>
</View>
</FlatCard>
)}

<FlatList
data={this.state.specialOffers}
renderItem={({ item }) => (
<MenuEntry
image={item.info.immagine}
name={item.info.nome}
price={item.ristorante.nome + " - " + item.info.prezzo}
description={
item.info.descrizione +
"\n\nValido fino al: " +
new Date(item.scadenza).toLocaleDateString()
}
onAddToCart={() => this.handleAddToCart(item.info.id)} //TODO non so se funziona così
/>
)}
keyExtractor={item => item.id}
/>
{this.state.isReady && (
<FlatList
data={this.state.specialOffers}
renderItem={({ item }) => (
<MenuEntry
image={item.info.immagine}
name={item.info.nome}
price={item.ristorante.nome + " - " + item.info.prezzo}
description={
item.info.descrizione +
"\n\nValido fino al: " +
new Date(item.scadenza).toLocaleDateString()
}
onAddToCart={() => this.handleAddToCart(item.info.id)} //TODO non so se funziona così
/>
)}
keyExtractor={item => item.id}
/>
)}
</Card>
<Separator times={4} />
</View>
Expand Down
7 changes: 2 additions & 5 deletions src/Components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Search extends React.Component {
console.log("customer token", this.provider.token);
this.state = { query: null, results: null };
this.provider
.getAllRestaurants()
.doGetAllRestaurants()
.then(response => this.setState({ results: response }));
this.handleSearch = this.handleSearch.bind(this); // Era questo il problema https://stackoverflow.com/questions/39176248/react-js-cant-read-property-of-undefined
}
Expand Down Expand Up @@ -72,10 +72,7 @@ class Search extends React.Component {
/>
)}
{!this.provider.isGuest() && (
<Button
text={"Logout"}
onPress={this.provider.doLogout}
/>
<Button text={"Logout"} onPress={this.provider.doLogout} />
)}
</View>
</HeaderCard>
Expand Down
32 changes: 11 additions & 21 deletions src/DataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ class DataProvider {
});
};

doLogout = () =>
{
doLogout = () => {
console.debug("DataProvider", "doLogout");
const data = {
value: this.token
Expand All @@ -137,21 +136,17 @@ class DataProvider {
return new Promise((resolve, reject) => {
this.doPost("/logout", data)
.then(response => {
if (response != null)
{
if (response != null) {
sessionStorage.clear();
this.navigateLogin();
}
else {
} else {
reject("NoResults");
}
})
.catch(error => {
console.error(error);
if (error === "NoResults")
reject("Riprova più tardi.");
else
reject("FailedToFetch");
if (error === "NoResults") reject("Riprova più tardi.");
else reject("FailedToFetch");
});
});
};
Expand Down Expand Up @@ -189,8 +184,7 @@ class DataProvider {
});
}

getAllRestaurants() {
//todo mettere il do
doGetAllRestaurants() {
console.debug("DataProvider", "getAllRestaurants");

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -239,7 +233,7 @@ class DataProvider {
}

doAddToCart(menuEntryId) {
console.debug("doAddToCart", menuEntryId);
console.debug("DataProvider", "doAddToCart");
const data = {
token: this.token,
piatto: {
Expand Down Expand Up @@ -325,24 +319,20 @@ class DataProvider {
}
}

doAddInfoToNotSignedUser(data)
{
doAddInfoToNotSignedUser(data) {
data.token = this.token;

return new Promise((resolve, reject) => {
this.doPost("cliente/not-signed-user/addInfo", data)
.then(response => {
if (response != null)
resolve(response);
else
reject("NoResults");
if (response != null) resolve(response);
else reject("NoResults");
})
.catch(error => {
console.error(error);
reject("FailedToFetch");
});

})
});
}

doGetCartEntries() {
Expand Down

0 comments on commit 6a1aa68

Please sign in to comment.