Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More trpc query with packs #415

Open
wants to merge 2 commits into
base: feat/experimenting-with-trpc-query
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
766 changes: 283 additions & 483 deletions client/.tamagui/tamagui.config.json

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions client/app/(app)/items/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { getItemsGlobal } from '../../../store/globalItemsStore';
import { Stack } from 'expo-router';
import Head from 'expo-router/head';
import { useFetchGlobalItems } from '~/hooks/globalItems';
import useGetGlobalItems from '~/hooks/globalItems/useGetGlobalItems';

export default function Items() {
const [isAddItemModalOpen, setIsAddItemModalOpen] = useState(false);
Expand All @@ -27,10 +27,7 @@ export default function Items() {
const { enableDarkMode, enableLightMode, isDark, isLight, currentTheme } =
UseTheme();

const { data, isLoading, isError, refetch } = useFetchGlobalItems(
limit,
page,
);
const { data, isLoading, isError, refetch } = useGetGlobalItems(limit, page);
console.log('🚀 ~ file: index.js:32 ~ Items ~ data:', data);
// const data = useSelector((state) => state.globalItems);

Expand Down
11 changes: 10 additions & 1 deletion client/components/EditableText/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { theme } from '../../theme';
import { useDispatch } from 'react-redux';
import { updatePack } from '../../store/packsStore';
import { editTrip } from '../../store/tripsStore';
import { PACKQUERYS, PACKREDUCERS } from '~/hooks/packs';
import { useMutation } from '~/hooks/useMutation';

export const EditableInput = ({
data,
Expand All @@ -14,6 +16,10 @@ export const EditableInput = ({
}) => {
const [headerTitle, setHeaderTitle] = useState(title || '');
const dispatch = useDispatch();
const { mutation, onSuccesMutation } = useMutation(
PACKQUERYS.editItem,
PACKREDUCERS.updatePack,
);
return (
<TextInput
style={{
Expand All @@ -37,7 +43,10 @@ export const EditableInput = ({
setEditTitle(false);
titleRef.current.style =
'font-size:20px !important;font-weight:bold;color: #22c67c;';

mutation.mutate(packDetails, {
onSuccess: (data) =>
onSuccesMutation({ ...data, itemId, packId: pack._id }),
});
dispatch(updatePack(packDetails));
} else {
const tripDetails = {
Expand Down
14 changes: 12 additions & 2 deletions client/components/ScoreContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Box, Button, VStack, Text, HStack, View } from 'native-base';
import { theme } from '../theme';
import useTheme from '../hooks/useTheme';
import { useDispatch } from 'react-redux';
import { scorePack } from '../store/packsStore';
import { Svg, Circle, Path, G, Text as SvgText } from 'react-native-svg';
import useCustomStyles from '~/hooks/useCustomStyles';
import { useMutation } from '~/hooks/useMutation';
import { PACKQUERYS, PACKREDUCERS } from '~/hooks/packs';

const ScoreProgressChart = ({ score, size = 150, strokeWidth = 10 }) => {
if (!score) return null;
Expand Down Expand Up @@ -157,6 +158,10 @@ const GradingPieChart = ({ scores, size = 150, strokeWidth = 10 }) => {

export default function ScoreContainer({ type, data, isOwner }) {
const dispatch = useDispatch();
const { mutation, onSuccesMutation } = useMutation(
PACKQUERYS.scorePack,
PACKREDUCERS.scorePack,
);
const { enableDarkMode, enableLightMode, isDark, isLight, currentTheme } =
useTheme();
const styles = useCustomStyles(loadStyles);
Expand Down Expand Up @@ -188,7 +193,12 @@ export default function ScoreContainer({ type, data, isOwner }) {

const handleScoreClick = () => {
if (type === 'pack') {
dispatch(scorePack(id));
mutation.mutate(
{ packId: id },
{
onSuccess: (data) => onSuccesMutation(data),
},
);
} else if (type === 'trip') {
dispatch(scoreTrip(id));
}
Expand Down
12 changes: 11 additions & 1 deletion client/components/Water.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import { useDispatch } from 'react-redux';
import { addPackItem } from '../store/packsStore';
import useCustomStyles from '~/hooks/useCustomStyles';

import { useMutation } from '~/hooks/useMutation';
import { PACKQUERYS, PACKREDUCERS } from '~/hooks/packs';

export default function Water({ currentPack, setWaterItem }) {
const [waterWeight, setWaterWeight] = useState(0);
const dispatch = useDispatch();
const styles = useCustomStyles(loadStyles);

const { mutation, onSuccesMutation } = useMutation(
PACKQUERYS.addItem,
PACKREDUCERS.addItem,
);

/**
* Update the water weight.
*
Expand All @@ -36,7 +44,9 @@ export default function Water({ currentPack, setWaterItem }) {
type: ItemCategoryEnum.WATER,
};

dispatch(addPackItem(data));
mutation.mutate(data, {
onSuccess: (data) => onSuccesMutation(data),
});
};

return (
Expand Down
58 changes: 43 additions & 15 deletions client/components/item/AddItem.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { useEffect, useState } from 'react';
import { Box, Input, Button, Text } from 'native-base';
import { useDispatch, useSelector } from 'react-redux';
import {
addPackItem,
editPackItem,
editItemsGlobalAsDuplicate,
} from '../../store/packsStore';
import { ItemForm } from './ItemForm'; // assuming you moved the form related code to a separate component
import { ItemCategoryEnum } from '../../constants/itemCategory';
import { PACKQUERYS, PACKREDUCERS } from '~/hooks/packs';

export const AddItem = ({
_id,
Expand All @@ -24,6 +20,24 @@ export const AddItem = ({
setRefetch = () => {},
}) => {
const dispatch = useDispatch();
const {
mutation: editItemsGlobalAsDuplicateMutation,
onSuccesMutation: editItemsGlobalAsDuplicateOnSuccessMutation,
} = useMutation(
PACKQUERYS.editGlobalItemAsDuplicate,
PACKREDUCERS.editGlobalItemAsDuplicate,
);

const {
mutation: editItemsMutation,
onSuccesMutation: editItemsOnSuccessMutation,
} = useMutation(PACKQUERYS.editItem, PACKREDUCERS.editItem);

const {
mutation: addPackItemsMutation,
onSuccesMutation: addPackItemsOnSuccessMutation,
} = useMutation(PACKQUERYS.addItem, PACKREDUCERS.addItem);

const isLoading = useSelector((state) => state.packs.isLoading);

// Moved the state up to the parent component
Expand Down Expand Up @@ -56,45 +70,59 @@ export const AddItem = ({
if (isEdit) {
if (packId && initialData.global) {
console.log('editing', packId);

dispatch(
editItemsGlobalAsDuplicate({
editItemsGlobalAsDuplicateMutation.mutate(
{
itemId: _id,
packId,
name,
weight,
quantity,
unit,
type: categoryType,
}),
},
{
onSuccess: (data) =>
editItemsGlobalAsDuplicateOnSuccessMutation({
...data,
itemId: _id,
packId: pack._id,
}),
},
);
closeModalHandler();
} else {
dispatch(
editPackItem({
editItemsMutation.mutate(
{
name,
weight,
quantity,
unit,
type: categoryType,
_id: initialData._id,
}),
},
{
onSuccess: (data) => editItemsOnSuccessMutation(data),
},
);

setPage(1);
closeModalHandler();
setRefetch(refetch !== true);
}
} else {
dispatch(
addPackItem({
addPackItemsMutation.mutate(
{
name,
weight,
quantity,
type: categoryType,
unit,
_id,
packId,
}),
},
{
onSuccess: (data) => addPackItemsOnSuccessMutation(data),
},
);
setIsAddItemModalOpen(false);
setRefetch(refetch !== true);
Expand Down
18 changes: 9 additions & 9 deletions client/components/item/AddItemGlobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useDispatch, useSelector } from 'react-redux';
import { addItemsGlobal, addItemOffline } from '../../store/globalItemsStore';
import { addOfflineRequest } from '../../store/offlineQueue';
import { ItemForm } from './ItemForm'; // assuming you moved the form related code to a separate component
import useAddGlobalItems from '~/hooks/globalItems/useAddGlobalItems';

export const AddItemGlobal = ({
setIsAddItemModalOpen,
Expand Down Expand Up @@ -46,15 +47,14 @@ export const AddItemGlobal = ({
dispatch(addItemOffline({ ...item, weight: Number(item.weight) }));
dispatch(addOfflineRequest({ method: 'addGlobalItem', data: item }));
} else {
dispatch(
addItemsGlobal({
name,
weight,
quantity,
type: categoryType,
unit,
}),
);
useAddGlobalItems({
name,
weight,
quantity,
type: categoryType,
unit,
});

setRefetch(refetch !== true);
}

Expand Down
15 changes: 12 additions & 3 deletions client/components/pack/AddPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import { useSelector, useDispatch } from 'react-redux';
import { CustomModal } from '../modal';
import useTheme from '../../hooks/useTheme';
import useCustomStyles from '~/hooks/useCustomStyles';
import { useAddNewPack } from '~/hooks/packs';
import { PACKQUERYS, PACKREDUCERS } from '~/hooks/packs';
import { useMutation } from '@tanstack/react-query';

export const AddPack = () => {
const { enableDarkMode, enableLightMode, isDark, isLight, currentTheme } =
Expand All @@ -30,7 +31,10 @@ export const AddPack = () => {
const error = useSelector((state) => state.packs.error);

const isError = error !== null;
const { mutation } = useAddNewPack();
const {
mutation: addPackMutation,
onSuccesMutation: addPackOnSuccesMutation,
} = useMutation(PACKQUERYS.addPack, PACKREDUCERS.addPack);
/**
* Handles the addition of a pack.
*
Expand All @@ -39,7 +43,12 @@ export const AddPack = () => {
* @return {void}
*/
const handleAddPack = () => {
dispatch(addPack({ name, owner_id: user?._id, is_public: isPublic }));
addPackMutation.mutate(
{ name, owner_id: user?._id, is_public: isPublic },
{
onSuccess: (data) => addPackOnSuccesMutation(data),
},
);
setName('');
};

Expand Down
1 change: 1 addition & 0 deletions client/components/pack/PackDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function PackDetails() {

const { data: userPacks, isLoading: isUserPacksLoading } =
useUserPacks(userId);

const {
data: currentPack,
isLoading,
Expand Down
29 changes: 27 additions & 2 deletions client/components/pack_table/DeletePackItemModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
deleteItemOffline,
} from '../../store/globalItemsStore';
import { addOfflineRequest } from '../../store/offlineQueue';
import useDeleteGlobalItems from '~/hooks/globalItems/useDeleteGlobalItems';
import { PACKQUERYS, PACKREDUCERS, useDeletePackItem } from '~/hooks/packs';
import { useMutation } from '~/hooks/useMutation';

export const DeletePackItemModal = ({
itemId,
Expand All @@ -22,6 +25,16 @@ export const DeletePackItemModal = ({
setIsModalOpen(false);
};

const { mutation: deletePackItemMutation, onSuccesMutation } = useMutation(
PACKQUERYS.deleteItem,
PACKREDUCERS.deleteItem,
);

const {
mutation: deleteGlobalItemsMutation,
onSuccesMutation: deleteGlobalItemsonSuccesMutation,
} = useDeleteGlobalItems();

/**
* Sets the value of `isModalOpen` to `true`.
*
Expand All @@ -40,10 +53,22 @@ export const DeletePackItemModal = ({
*/
const deleteItemHandler = () => {
if (pack) {
dispatch(deletePackItem({ itemId, currentPackId: pack._id }));
deletePackItemMutation.mutate(
{ itemId, packId: pack._id },
{
onSuccess: (data) =>
onSuccesMutation({ ...data, itemId, packId: pack._id }),
},
);
} else {
if (isConnected) {
dispatch(deleteGlobalItem(itemId));
deleteGlobalItemsMutation.mutate(
{ itemId: item },
{
onSuccess: (data) => deleteGlobalItemsonSuccesMutation(data),
},
);

setRefetch(refetch !== true);
} else {
dispatch(deleteItemOffline(itemId));
Expand Down
5 changes: 4 additions & 1 deletion client/components/pack_table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { PackOptions } from '../PackOptions';
import CustomButton from '../custombutton';
import ItemPicker from '../Picker';
import useCustomStyles from '~/hooks/useCustomStyles';
import { useMutation } from '~/hooks/useMutation';
import { PACKQUERYS } from '~/hooks/packs';

const WeightUnitDropdown = ({ value, onChange }) => {
return (
Expand Down Expand Up @@ -225,6 +227,7 @@ export const TableContainer = ({
}) => {
const user = useSelector((state) => state.auth.user);
const dispatch = useDispatch();
const { mutation } = useMutation(PACKQUERYS.duplicatePublicPack);
const styles = useCustomStyles(loadStyles);
let ids = [];
if (currentPack?.items) {
Expand All @@ -238,7 +241,7 @@ export const TableContainer = ({
ownerId: user._id,
items: checkedItems,
};
dispatch(duplicatePackItem(data));
mutation.mutate(data);
};

const [weightUnit, setWeightUnit] = useState('g');
Expand Down
Loading