Skip to content

Commit

Permalink
Created actions for creating/updating a tip
Browse files Browse the repository at this point in the history
  • Loading branch information
biagioo committed Sep 2, 2020
1 parent dfe6c47 commit 4382c83
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions actions/tipAction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import BASEURL from '../helpers/BaseUrl';
import { Alert } from 'react-native';

export const EditTip = (tip, jwt, type = '') => {
return (dispatch) => {
Expand All @@ -14,3 +15,46 @@ export const EditTip = (tip, jwt, type = '') => {
fetch(BASEURL + `tips/${tip.id}`, configurationObject);
};
};

export const CreateOrUpdateTip = (props, tipObject) => {
return (dispatch) => {
const { route, navigation, jwt } = props;
const { tip } = route.params;

const configObject = {
method: tip ? 'PATCH' : 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${jwt}`,
},
body: JSON.stringify({
tip: tipObject,
}),
};
const url = BASEURL + (tip ? `tips/${tip.id}` : 'tips');
fetch(url, configObject)
.then((data) => data.json())
.then((json) => {
if (json.error) {
Alert.alert(json.message);
} else {
if (tip) {
props.UpdateUserTips(json.data);
navigation.navigate('Auth');
} else {
props.newTip(json.data);

navigation.navigate('Tips', { data: json.data, ...route.params });
}
}
});
};
};

export const UpdateUserTips = (payload) => {
return {
type: 'UPDATE_TIP',
payload,
};
};

0 comments on commit 4382c83

Please sign in to comment.