-
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.
- Loading branch information
1 parent
e7174de
commit d3e10e3
Showing
5 changed files
with
169 additions
and
81 deletions.
There are no files selected for viewing
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
42 changes: 42 additions & 0 deletions
42
packages/app/components/card/PackCardHeader/usePackActions.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,42 @@ | ||
import { useEditPack, useDeletePack } from 'app/modules/pack'; | ||
import { useState } from 'react'; | ||
|
||
export const usePackActions = ({ data, refetch }) => { | ||
const [isEditModalOpen, setIsEditModalOpen] = useState(false); | ||
const [isTitleEditMode, setIsTitleEditMode] = useState(false); | ||
const { editPack } = useEditPack(); | ||
const { handleDeletePack } = useDeletePack(data.id); | ||
|
||
const handleActionsOpenChange = (state) => { | ||
switch (state) { | ||
case 'Edit': | ||
setIsEditModalOpen(true); | ||
break; | ||
case 'Delete': | ||
handleDeletePack(); | ||
break; | ||
} | ||
}; | ||
|
||
const handleSaveTitle = (title) => { | ||
const packDetails = { | ||
id: data.id, | ||
name: title, | ||
is_public: data.is_public, | ||
}; | ||
editPack(packDetails, { | ||
onSuccess: () => { | ||
refetch?.(); | ||
}, | ||
}); | ||
setIsTitleEditMode(false); | ||
}; | ||
|
||
return { | ||
handleActionsOpenChange, | ||
isTitleEditMode, | ||
isEditModalOpen, | ||
setIsEditModalOpen, | ||
handleSaveTitle, | ||
}; | ||
}; |
61 changes: 0 additions & 61 deletions
61
packages/app/components/card/PackCardHeader/usePackTitleInput.ts
This file was deleted.
Oops, something went wrong.
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,103 @@ | ||
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 { View, Text } from 'react-native'; | ||
import { useEditPack } from '../hooks/useEditPack'; | ||
import RStack from '@packrat/ui/src/RStack'; | ||
import RText from '@packrat/ui/src/RText'; | ||
import { Switch } from 'tamagui'; | ||
import RSwitch from '@packrat/ui/src/RSwitch'; | ||
|
||
interface EditPackModalProps { | ||
isOpen?: boolean; | ||
onClose?: () => void; | ||
currentPack: any; | ||
refetch?: () => void; | ||
} | ||
|
||
export const EditPackModal: React.FC<EditPackModalProps> = ({ | ||
isOpen, | ||
onClose, | ||
currentPack, | ||
refetch, | ||
}) => { | ||
console.log({ currentPack }); | ||
const [packName, setPackName] = useState(currentPack?.name ?? ''); | ||
const [isPublic, setIsPublic] = useState(currentPack?.is_public ?? true); | ||
const { editPack, isLoading, isError } = useEditPack(); | ||
|
||
const handleEditPack = async () => { | ||
try { | ||
editPack( | ||
{ id: currentPack.id, name: packName, is_public: isPublic }, | ||
{ | ||
onSuccess: () => { | ||
onClose?.(); | ||
refetch?.(); | ||
}, | ||
}, | ||
); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (isOpen) { | ||
setPackName(currentPack?.name ?? ''); | ||
setIsPublic(currentPack?.is_public ?? true); | ||
} | ||
}, [isOpen]); | ||
|
||
return ( | ||
<BaseModal | ||
showTrigger={false} | ||
title="Edit Pack" | ||
footerButtons={[ | ||
{ | ||
label: 'Cancel', | ||
color: '#B22222', | ||
onClick: (_, closeModal) => { | ||
closeModal(); | ||
onClose?.(); | ||
}, | ||
}, | ||
]} | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
> | ||
<RInput | ||
placeholder="Pack Name" | ||
value={packName} | ||
onChangeText={(t) => setPackName(t)} | ||
style={{ width: 200 }} | ||
/> | ||
<RStack | ||
style={{ | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
justifyContent: 'space-between', | ||
}} | ||
> | ||
<RText>Public </RText> | ||
<RSwitch | ||
checked={isPublic} | ||
onCheckedChange={() => setIsPublic((prev) => !prev)} | ||
size="$1.5" | ||
> | ||
<Switch.Thumb /> | ||
</RSwitch> | ||
</RStack> | ||
<RButton onPress={handleEditPack} disabled={isLoading}> | ||
{isLoading ? 'Saving...' : 'Save'} | ||
</RButton> | ||
{isError && ( | ||
<RStack> | ||
<RText style={{ color: 'red' }}>Failed to save changes</RText> | ||
</RStack> | ||
)} | ||
</BaseModal> | ||
); | ||
}; |