Skip to content

Commit

Permalink
rebase with andrew_testing
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzaib-manzoor committed Jan 4, 2024
2 parents 0141b86 + 9cdda6b commit 276ef86
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 149 deletions.
2 changes: 1 addition & 1 deletion client/components/ScoreContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default function ScoreContainer({ type, data, isOwner }) {
{isAlreadyScored && (
<>
<ScoreProgressChart score={totalScore} />
{/* <GradingPieChart scores={scores} /> */}
<GradingPieChart scores={scores} />
</>
)}
</HStack>
Expand Down
22 changes: 7 additions & 15 deletions client/components/item/AddItem.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { useEffect, useState } from 'react';
import { View } from 'react-native';
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 { useAddPackItem } from '~/hooks/packs/useAddPackItem';
import { add } from 'date-fns';
import { useEditPackItem } from '~/hooks/packs/useEditPackItem';

export const AddItem = ({
Expand All @@ -24,8 +17,6 @@ export const AddItem = ({
closeModalHandler,
setIsAddItemModalOpen = () => {},
}) => {
const dispatch = useDispatch();
const isLoading = useSelector((state) => state.packs.isLoading);

// Moved the state up to the parent component
const [name, setName] = useState(initialData?.name || '');
Expand All @@ -41,11 +32,14 @@ export const AddItem = ({

const {
// mutation: addPackItemMutation
isLoading,
isError,
addPackItem,
} = useAddPackItem();

const {
// mutation: addPackItemMutation

editPackItem,
} = useEditPackItem();

Expand All @@ -62,7 +56,6 @@ export const AddItem = ({
*
* @return {type} description of return value
*/
console.log(categoryType);
const handleSubmit = () => {
const PackId = packId || initialData._id;

Expand All @@ -74,7 +67,7 @@ export const AddItem = ({
quantity,
unit,
type: categoryType,
_id: initialData._id,
// _id: initialData._id,
});
closeModalHandler();
} else {
Expand All @@ -84,8 +77,8 @@ export const AddItem = ({
quantity,
unit,
type: categoryType,
_id,
packId,
// _id,
// packId,
});
setPage(1);
closeModalHandler();
Expand All @@ -97,8 +90,7 @@ export const AddItem = ({
quantity,
type: categoryType,
unit,
_id,
packId,
packId
});
}
};
Expand Down
7 changes: 4 additions & 3 deletions client/components/item/searchItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ export const SearchItem: React.FC<Props> = ({ onSelect, placeholder }) => {
const [selectedSearch, setSelectedSearch] = useState('');

const searchResults =
useSelector((state: any) => state.search.searchResults.items) || [];

useSelector((state: any) => state.search.searchResults) || [];
const user = useSelector((state: any) => state.auth.user);
const [showSearchResults, setShowSearchResults] = useState(false);

Expand All @@ -33,7 +32,7 @@ export const SearchItem: React.FC<Props> = ({ onSelect, placeholder }) => {
const ownerId = user._id;
// @ts-expect-error
const packId = window.location.pathname.substring('/path/'.length);
const selectedItem = item._id;
const selectedItem = item?._id;
const data = {
ownerId,
packId,
Expand Down Expand Up @@ -87,6 +86,8 @@ export const SearchItem: React.FC<Props> = ({ onSelect, placeholder }) => {
}
disabled={!showSearchResults}
/>


<View style={{ position: 'relative' }}>
{showSearchResults && searchResults?.length > 0 && (
<RScrollView
Expand Down
18 changes: 17 additions & 1 deletion client/components/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { X } from '@tamagui/lucide-icons';

import { Dialog } from 'tamagui';

type PropTypes = {
id?: string;
title: string;
trigger?: string;
children: React.ReactNode;
onSave?: () => void;
onCancel?: () => void;
buttonColor?: string;
type?: string;
size?: string;
footerButtons?: any[];
isActive: boolean;
onTrigger: (value: boolean) => void;
buttonText?: string;
triggerComponent?: React.ReactNode;
};
export const CustomModal = ({
id,
title,
Expand All @@ -20,7 +36,7 @@ export const CustomModal = ({
buttonText,
triggerComponent = null,
...rest
}) => {
}:PropTypes) => {
/**
*
* Closes the modal either by calling the onCancel function or by triggering the onTrigger function with a value of false.
Expand Down
41 changes: 22 additions & 19 deletions client/components/pack/AddPack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,44 @@ import useTheme from '../../hooks/useTheme';
import useCustomStyles from '~/hooks/useCustomStyles';
import { useAddNewPack } from '~/hooks/packs';
import { useRouter } from 'expo-router';
import { Box, Button, CheckIcon, Input, Select, Text } from 'native-base';
import { packSelectOptions } from '~/constants/options';


export const AddPack = ({ isCreatingTrip = false }) => {
//Hooks
const { enableDarkMode, enableLightMode, isDark, isLight, currentTheme } =
useTheme();
const styles = useCustomStyles(loadStyles);

const dispatch = useDispatch();
const router = useRouter();

const [name, setName] = useState('');
const [isPublic, setIsPublic] = useState(false);
const {
addNewPack,
isSuccess,
isError,
response,
error,
isLoading,
name,
setIsPublic,
setName,
} = useAddNewPack();

// const { addPack } = useAddPack();
// const { user } = useAuth();
const user = useSelector((state) => state.auth.user);
const isLoading = useSelector((state) => state.packs.isLoading);
const { addNewPack, isSuccess, isError, response } = useAddNewPack();
//routing
if (isSuccess && !isCreatingTrip && response) {
router.push(`/pack/${response.createdPack._id}`);
}
/**
* Handles the addition of a pack.
*
* @param {string} name - The name of the pack.
* @param {string} owner_id - The ID of the pack's owner.
* @return {void}
*/
const handleAddPack = () => {
addNewPack({ name, owner_id: user?._id, is_public: isPublic });
setName('');
addNewPack();
};

const data = ['Yes', 'For me only'];

const handleonValueChange = (itemValue) => {
setIsPublic(itemValue == 'Yes');
if (itemValue === 'Yes') setIsPublic(true);
else setIsPublic(false);
};

return (
Expand All @@ -60,9 +62,9 @@ export const AddPack = ({ isCreatingTrip = false }) => {
/>
<RLabel>Is Public:</RLabel>
<DropdownComponent
value={isPublic}
// value={isPublic}
onValueChange={handleonValueChange}
data={data}
data={packSelectOptions}
width="300px"
accessibilityLabel="Choose Service"
placeholder={'Is Public'}
Expand Down Expand Up @@ -109,6 +111,7 @@ const loadStyles = (theme, appTheme) => {
width: '100%',
paddingHorizontal: 18,
gap: 20,
marginTop: 20,
},
desktopStyle: {
flexDirection: 'row',
Expand Down
3 changes: 0 additions & 3 deletions client/components/pack/PackContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ export default function PackContainer({ isCreatingTrip = false }) {
const [isAddItemModalOpen, setIsAddItemModalOpen] = useState(false);

const user = useSelector((state) => state.auth.user);
// const packs = useSelector(selectAllPacks);

const newTrip = useSelector((state) => state.trips.newTrip);

const [currentPackId, setCurrentPackId] = useState(null);
const [refetch, setRefetch] = useState(false);
Expand Down
6 changes: 1 addition & 5 deletions client/components/pack/PackDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@ import { useUserPacks } from '~/hooks/packs/useUserPacks';
import { useFetchSinglePack } from '../../hooks/packs';

export function PackDetails() {
const searchParams = new URLSearchParams(window.location.search);

const searchParams = new URLSearchParams(this.location.search);
const canCopy = searchParams.get('copy');

const dispatch = useDispatch();

const { packId } = useSearchParams();
const link = `${CLIENT_URL}/packs/${packId}`;
const updated = useSelector((state) => state.packs.update);
Expand Down
4 changes: 4 additions & 0 deletions client/constants/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const packSelectOptions = [
{ value: 'Yes', label: 'Yes' },
{ value: 'For me only', label: 'For me only' },
];
34 changes: 27 additions & 7 deletions client/hooks/packs/useAddNewPack.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { useState } from 'react';
import { queryTrpc } from '../../trpc';
import { useSelector } from 'react-redux';

export const useAddNewPack = () => {
const user = useSelector((state) => state.auth.user);
const [name, setName] = useState('');
const [isPublic, setIsPublic] = useState(false);
const utils = queryTrpc.useContext();
// Use mutation for adding a pack

const addNewPack = () => {
mutation.mutate({
name: name,
is_public: isPublic,
owner_id: user?._id,
});
}

const mutation = queryTrpc.addPack.useMutation({
onMutate: async (newPack) => {
onMutate: async (packData) => {
utils.getPacks.cancel({
ownerId: newPack?.owner_id,
ownerId: packData?.owner_id,
queryBy: '',
});
// Step 1: Define optimistic update
const optimisticUpdate = {
...newPack,
...packData,
id: Date.now(),
};

const oldQueryData = utils.getPacks.getData({
ownerId: newPack?.owner_id,
ownerId: packData?.owner_id,
queryBy: '',
});

Expand All @@ -28,17 +43,18 @@ export const useAddNewPack = () => {
};
utils.getPacks.setData(
{
ownerId: newPack.owner_id,
ownerId: packData.owner_id,
queryBy: '',
},
(oldQueryData) => newQueryData,
);
setName('');
setIsPublic(false);
return {
oldQueryData,
};
},
onError: (_error, _pack, context) => {
console.log('error');
console.log(context.oldQueryData);
utils.getPacks.setData(
{
Expand All @@ -54,11 +70,15 @@ export const useAddNewPack = () => {
});
return {
mutation,
addNewPack: mutation.mutate,
addNewPack,
isLoading: mutation.isLoading,
isError: mutation.isError,
isSuccess: mutation.isSuccess,
error: mutation.error,
response: mutation.data,
name,
isPublic,
setIsPublic,
setName
};
};
3 changes: 2 additions & 1 deletion client/hooks/search/useSearchInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import {
} from '../../store/searchStore';
import { setLatLng } from '../../store/weatherStore';
import { usePhotonDetail } from '~/hooks/photonDetail';
import { RootState } from 'store/store';

const useSearchInput = (onSelect) => {
const [searchString, setSearchString] = useState('');
const [showSearchResults, setShowSearchResults] = useState(false);
const [isLoadingMobile, setIsLoadingMobile] = useState(false);
const selectedSearchResult =
useSelector((state) => state.search.selectedSearchResult) || {};
useSelector((state:RootState) => state.search.selectedSearchResult) || {};

const { refetch, data } = usePhotonDetail(searchString, showSearchResults);

Expand Down
6 changes: 4 additions & 2 deletions client/store/packsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ export const duplicatePackItem = createAsyncThunk(
},
);



export const scorePack = createAsyncThunk('packs/scorePack', async (packId) => {
// const response = await axios.put(`${api}/pack/score/${packId}`);
// return response.data;
Expand Down Expand Up @@ -315,8 +317,8 @@ const packsSlice = createSlice({
})
.addCase(scorePack.fulfilled, (state, action) => {
packsAdapter.updateOne(state, {
id: action.payload.updatedPack._id,
changes: action.payload.updatedPack,
id: action.payload._id,
changes: action.payload,
});
state.isLoading = false;
state.error = null;
Expand Down
3 changes: 1 addition & 2 deletions client/store/searchStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ export const fetchItemsSearchResults = createAsyncThunk(
'search/fetchItemsSearchResults',
async (searchString) => {
const url = api + `/item/global?search=${encodeURIComponent(searchString)}`;

try {
// const response = await axios.get(url);
// return response.data;
return await trpc.getItemsGlobally.query({ searchString });
return await trpc.getItemsGlobally.query({limit:5, page:0, searchString });
} catch (error) {
console.error('error:' + error);
}
Expand Down
Loading

0 comments on commit 276ef86

Please sign in to comment.