-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add action buttons on tripcardheader
- Loading branch information
1 parent
2d47d47
commit c771869
Showing
7 changed files
with
241 additions
and
63 deletions.
There are no files selected for viewing
142 changes: 103 additions & 39 deletions
142
packages/app/components/card/TripCardHeader/TripCardHeader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,115 @@ | ||
import React from 'react'; | ||
import { Platform, Text } from 'react-native'; | ||
import { Platform, Text, View } from 'react-native'; | ||
import { useRouter } from 'app/hooks/router'; | ||
import useTheme from 'app/hooks/useTheme'; | ||
import { CustomCardHeader } from '../CustomCardHeader'; | ||
import { RIconButton, RStack } from '@packrat/ui'; | ||
import { AntDesign } from '@expo/vector-icons'; | ||
import RStack from '@packrat/ui/src/RStack'; | ||
import RIconButton from '@packrat/ui/src/RIconButton'; | ||
import { AntDesign, MaterialIcons } from '@expo/vector-icons'; | ||
import { CascadedDropdownComponent } from '@packrat/ui/src/CascadedDropdown'; | ||
import { useFetchSingleTrip } from 'app/hooks/singletrips/useFetchSingleTrip'; | ||
import { useTripActions } from './useTripActions'; | ||
import { useAuthUser } from 'app/modules/auth'; | ||
import { EditTripModal } from 'app/modules/trip/components/EditTripModal'; | ||
|
||
export const TripCardHeader = ({ data, title, link }) => { | ||
const { isDark, currentTheme } = useTheme(); | ||
interface TripCardHeaderProps { | ||
data: any; | ||
title: string; | ||
link?: string; | ||
} | ||
interface optionValues { | ||
label: string; | ||
value: string; | ||
} | ||
|
||
export const TripCardHeader = ({ data, title }: TripCardHeaderProps) => { | ||
const { refetch } = useFetchSingleTrip(data?.id); | ||
const user = useAuthUser(); | ||
|
||
const { currentTheme } = useTheme(); | ||
const router = useRouter(); | ||
const { | ||
handleActionsOpenChange, | ||
handleSaveTitle, | ||
isEditModalOpen, | ||
setIsEditModalOpen, | ||
} = useTripActions({ data, refetch }); | ||
|
||
const optionValues: optionValues[] = [ | ||
{ label: 'Edit', value: 'Edit' }, | ||
{ label: 'Delete', value: 'Delete' }, | ||
]; | ||
|
||
return ( | ||
<CustomCardHeader | ||
data={data} | ||
ownerId={data?.owner_id} | ||
title={ | ||
<RStack | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
flexDirection: 'row', | ||
}} | ||
> | ||
{Platform.OS === 'web' && ( | ||
<RIconButton | ||
backgroundColor="transparent" | ||
icon={ | ||
<AntDesign | ||
name="arrowleft" | ||
size={24} | ||
color={isDark ? 'white' : 'black'} | ||
/> | ||
} | ||
onPress={() => { | ||
if (Platform.OS === 'web') { | ||
window?.history?.back(); | ||
} else { | ||
router.back(); | ||
<> | ||
<CustomCardHeader | ||
data={data} | ||
ownerId={data?.owner_id} | ||
title={ | ||
<RStack | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center', | ||
flexDirection: 'row', | ||
}} | ||
> | ||
{Platform.OS === 'web' && ( | ||
<RIconButton | ||
backgroundColor="transparent" | ||
icon={ | ||
<AntDesign | ||
name="arrowleft" | ||
size={24} | ||
color={currentTheme.colors.text} | ||
/> | ||
} | ||
onPress={() => { | ||
if (Platform.OS === 'web') { | ||
window?.history?.back(); | ||
} else { | ||
router.back(); | ||
} | ||
}} | ||
/> | ||
)} | ||
<Text style={{ color: currentTheme.colors.text }}>{title}</Text> | ||
</RStack> | ||
} | ||
actionsComponent={ | ||
user?.id === data.owner_id && ( | ||
<View | ||
style={{ | ||
minWidth: 50, | ||
maxWidth: 100, | ||
}} | ||
/> | ||
)} | ||
<Text style={{ color: isDark ? 'white' : 'black' }}>{title}</Text> | ||
</RStack> | ||
} | ||
link={link} | ||
actionsComponent={undefined} | ||
/> | ||
> | ||
<CascadedDropdownComponent | ||
value={null} | ||
data={optionValues} | ||
onValueChange={(value) => handleActionsOpenChange(value)} | ||
placeholder={ | ||
<RIconButton | ||
backgroundColor="transparent" | ||
icon={<MaterialIcons name="more-horiz" size={20} />} | ||
style={{ | ||
height: 20, | ||
}} | ||
/> | ||
} | ||
native={true} | ||
/> | ||
</View> | ||
) | ||
} | ||
/> | ||
<EditTripModal | ||
currentTrip={data} | ||
isOpen={isEditModalOpen} | ||
onClose={() => { | ||
setIsEditModalOpen(false); | ||
}} | ||
refetch={refetch} | ||
/> | ||
</> | ||
); | ||
}; |
41 changes: 41 additions & 0 deletions
41
packages/app/components/card/TripCardHeader/useTripActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useDeleteTrips, useEditTrips } from 'app/hooks/trips'; | ||
import { useState } from 'react'; | ||
|
||
export const useTripActions = ({ data, refetch }) => { | ||
const [isEditModalOpen, setIsEditModalOpen] = useState(false); | ||
const [isTitleEditMode, setIsTitleEditMode] = useState(false); | ||
const { editTrips } = useEditTrips(); | ||
const { handleDeleteTrip } = useDeleteTrips(data.id); | ||
|
||
const handleActionsOpenChange = (state) => { | ||
switch (state) { | ||
case 'Edit': | ||
setIsEditModalOpen(true); | ||
break; | ||
case 'Delete': | ||
handleDeleteTrip(); | ||
break; | ||
} | ||
}; | ||
|
||
const handleSaveTitle = (title) => { | ||
const TripDetails = { | ||
id: data.id, | ||
name: title, | ||
}; | ||
editTrips(TripDetails, { | ||
onSuccess: () => { | ||
refetch?.(); | ||
}, | ||
}); | ||
setIsTitleEditMode(false); | ||
}; | ||
|
||
return { | ||
handleActionsOpenChange, | ||
isTitleEditMode, | ||
isEditModalOpen, | ||
setIsEditModalOpen, | ||
handleSaveTitle, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,16 @@ | ||
import { queryTrpc } from '../../trpc'; | ||
import { useRouter } from 'app/hooks/router'; | ||
import { queryTrpc } from 'app/trpc'; | ||
|
||
export const useDeleteTrips = () => { | ||
const utils = queryTrpc.useUtils(); | ||
const mutation = queryTrpc.deleteTrip.useMutation(); | ||
export const useDeleteTrips = (id) => { | ||
const { mutateAsync: deleteTrip } = queryTrpc.deleteTrip.useMutation(); | ||
const router = useRouter(); | ||
|
||
const deleteTrips = (tripId) => { | ||
mutation.mutate(tripId, { | ||
onSuccess: () => { | ||
utils.getTrips.invalidate(); | ||
}, | ||
}); | ||
const handleDeleteTrip = async () => { | ||
try { | ||
await deleteTrip({ tripId: id }); | ||
router.replace('/trips'); | ||
} catch {} | ||
}; | ||
|
||
return { | ||
deleteTrips, | ||
...mutation, | ||
}; | ||
return { handleDeleteTrip }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { BaseModal } from '../../../../ui/src/modal/BaseModal'; | ||
import RButton from '@packrat/ui/src/RButton'; | ||
import RInput from '@packrat/ui/src/RInput'; | ||
import RStack from '@packrat/ui/src/RStack'; | ||
import RText from '@packrat/ui/src/RText'; | ||
import { Switch } from 'tamagui'; | ||
import { useEditTrips } from 'app/hooks/trips/useEditTrips'; | ||
|
||
interface EditTripModalProps { | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
currentTrip: any; | ||
refetch?: () => void; | ||
} | ||
|
||
export const EditTripModal: React.FC<EditTripModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
currentTrip, | ||
refetch, | ||
}) => { | ||
const [tripName, setTripName] = useState(currentTrip?.name ?? ''); | ||
const { editTrips, isLoading, isError } = useEditTrips(); | ||
|
||
const handleEditTrip = async () => { | ||
try { | ||
editTrips( | ||
{ id: currentTrip.id, name: tripName }, | ||
{ | ||
onSuccess: () => { | ||
onClose?.(); | ||
refetch?.(); | ||
}, | ||
}, | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
setTripName(currentTrip?.name ?? ''); | ||
} | ||
}, [isOpen]); | ||
|
||
return ( | ||
<BaseModal | ||
showTrigger={false} | ||
title="Edit Trip" | ||
footerButtons={[ | ||
{ | ||
label: 'Cancel', | ||
color: '#B22222', | ||
onClick: (_, closeModal) => { | ||
closeModal(); | ||
onClose?.(); | ||
}, | ||
}, | ||
]} | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
> | ||
<RInput | ||
placeholder="Trip Name" | ||
value={tripName} | ||
onChangeText={(t) => setTripName(t)} | ||
style={{ width: 200 }} | ||
/> | ||
<RButton onPress={handleEditTrip} disabled={isLoading}> | ||
{isLoading ? 'Saving...' : 'Save'} | ||
</RButton> | ||
{isError && ( | ||
<RStack> | ||
<RText style={{ color: 'red' }}>Failed to save changes</RText> | ||
</RStack> | ||
)} | ||
</BaseModal> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters