Skip to content

Commit

Permalink
refactored existing code in user context which had attempted to store…
Browse files Browse the repository at this point in the history
… data without using favourite workout token. All CRUD operations were so updated
  • Loading branch information
chrismcanulty committed Jul 6, 2023
1 parent af2af34 commit cfb7464
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
24 changes: 21 additions & 3 deletions src/component/FavouriteTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export default function FavouriteTableRow({
}
};

const selectedFavouriteToken = token;

// console.log('token', token);
// console.log('title', title);
// console.log('item', item);
Expand Down Expand Up @@ -122,14 +124,28 @@ export default function FavouriteTableRow({
)}
<DataTable.Cell style={styles.cell}>
<TouchableOpacity
onPress={() => favouriteClickComplete({row, index, workoutId})}>
onPress={() =>
favouriteClickComplete({
row,
index,
workoutId,
selectedFavouriteToken,
})
}>
<WorkoutIcon name={row.Completion} />
</TouchableOpacity>
</DataTable.Cell>
<DataTable.Cell style={styles.cell}>
<TouchableOpacity
onPress={() =>
favouriteEditSet({row, index, workoutId, reps, weight})
favouriteEditSet({
row,
index,
workoutId,
reps,
weight,
selectedFavouriteToken,
})
}>
{row.Edit ? (
<WorkoutIcon name="check" />
Expand All @@ -140,7 +156,9 @@ export default function FavouriteTableRow({
</DataTable.Cell>
<DataTable.Cell style={styles.cell}>
<TouchableOpacity
onPress={() => favouriteDeleteSet({index, workoutId})}>
onPress={() =>
favouriteDeleteSet({index, workoutId, selectedFavouriteToken})
}>
<WorkoutIcon name="trash" />
</TouchableOpacity>
</DataTable.Cell>
Expand Down
5 changes: 4 additions & 1 deletion src/component/FavouriteWorkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export default function FavouriteWorkout({

const headers = ['Set', 'Reps', 'Weight', 'Done', 'Edit', 'Delete'];

const selectedFavouriteToken = token;

// need to pass down required props - token (favouriteWorkoutData is available from user context)

const saveFavouriteWorkoutData = async () => {
Expand Down Expand Up @@ -173,7 +175,8 @@ export default function FavouriteWorkout({
token={token}
/>
</DataTable>
<InfoButton onPress={() => addFavouriteSet({workoutId})}>
<InfoButton
onPress={() => addFavouriteSet({workoutId, selectedFavouriteToken})}>
<AddText>Add set</AddText>
<AddIcon name="plus" size={14} color={'rgb(169,169,169)'} />
</InfoButton>
Expand Down
38 changes: 29 additions & 9 deletions src/context/User.Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,20 @@ const UserProvider = ({children}) => {
setWorkout(tempWorkout);
};

const favouriteClickComplete = ({row, index, workoutId}) => {
const favouriteClickComplete = ({
row,
index,
workoutId,
selectedFavouriteToken,
}) => {
const tempWorkout = {...favouriteWorkoutData};
const modifyComplete =
row.Completion === 'check-circle'
? {...row, Completion: 'circle'}
: {...row, Completion: 'check-circle'};
tempWorkout[workoutId][index] = modifyComplete;
setFavouriteWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout, selectedFavouriteToken);
};

// add logic to remove trailing zeroes
Expand All @@ -169,7 +175,14 @@ const UserProvider = ({children}) => {
setWorkout(tempWorkout);
};

const favouriteEditSet = ({row, index, workoutId, reps, weight}) => {
const favouriteEditSet = ({
row,
index,
workoutId,
reps,
weight,
selectedFavouriteToken,
}) => {
if (reps === '') {
reps = '0';
}
Expand All @@ -182,14 +195,15 @@ const UserProvider = ({children}) => {
if (weight.slice(-1) === '.') {
weight = weight.replace('.', '');
}
console.log('edit', selectedFavouriteToken);
const tempWorkout = {...favouriteWorkoutData};
const editRow =
row.Edit === true
? {...row, Edit: false, Reps: reps, Weight: weight}
: {...row, Edit: true};
tempWorkout[workoutId][index] = editRow;
setFavouriteWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout, selectedFavouriteToken);
};

const addSet = ({workoutId}) => {
Expand All @@ -205,16 +219,21 @@ const UserProvider = ({children}) => {
setWorkout(tempWorkout);
};

const storeWorkoutData = async value => {
const storeWorkoutData = async (value, selectedFavouriteToken) => {
console.log('selected', selectedFavouriteToken);
try {
const jsonValue = JSON.stringify(value);
await AsyncStorage.setItem('@workout_key', jsonValue);
await AsyncStorage.setItem(
`@workout_key-${selectedFavouriteToken}`,
jsonValue,
);
} catch (e) {
// saving error
}
};

const addFavouriteSet = ({workoutId}) => {
const addFavouriteSet = ({workoutId, selectedFavouriteToken}) => {
console.log('add', selectedFavouriteToken);
const tempWorkout = {...favouriteWorkoutData};
const setNumber = tempWorkout[workoutId].length + 1;
const newSet = {
Expand All @@ -225,7 +244,7 @@ const UserProvider = ({children}) => {
};
tempWorkout[workoutId].push(newSet);
setFavouriteWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout, selectedFavouriteToken);
};

const deleteSet = ({index, workoutId}) => {
Expand All @@ -237,14 +256,15 @@ const UserProvider = ({children}) => {
setWorkout(tempWorkout);
};

const favouriteDeleteSet = ({index, workoutId}) => {
const favouriteDeleteSet = ({index, workoutId, selectedFavouriteToken}) => {
console.log('delete', selectedFavouriteToken);
const tempWorkout = {...favouriteWorkoutData};
tempWorkout[workoutId].splice(index, 1);
tempWorkout[workoutId].map((set, index) => {
set.Set = index + 1;
});
setFavouriteWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout);
storeWorkoutData(tempWorkout, selectedFavouriteToken);
};

const clearWorkout = ({navigation}) => {
Expand Down

0 comments on commit cfb7464

Please sign in to comment.