Skip to content

Commit

Permalink
add editPackModal
Browse files Browse the repository at this point in the history
  • Loading branch information
Isthisanmol committed Sep 9, 2024
1 parent e7174de commit d3e10e3
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 81 deletions.
42 changes: 23 additions & 19 deletions packages/app/components/card/PackCardHeader/PackCardHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import useTheme from 'app/hooks/useTheme';
import { CustomCardHeader } from '../CustomCardHeader';
import { AntDesign, MaterialIcons } from '@expo/vector-icons';
import { useAuthUser } from 'app/modules/auth';
import {
RStack,
RIconButton,
EditableText,
DropdownComponent,
} from '@packrat/ui';
import { CascadedDropdownComponent } from '@packrat/ui/src/CascadedDropdown';
import { EditableText } from '@packrat/ui/src/EditableText';
import RStack from '@packrat/ui/src/RStack';
import RIconButton from '@packrat/ui/src/RIconButton';
import { useFetchSinglePack, useDeletePack } from 'app/modules/pack';
import { usePackTitleInput } from './usePackTitleInput';
import { usePackActions } from './usePackActions';
import { useRouter } from 'app/hooks/router';
import { Platform, View } from 'react-native';
import useResponsive from 'app/hooks/useResponsive';
import { EditPackModal } from 'app/modules/pack/components/EditPackModal';

interface PackCardHeaderProps {
data: any;
Expand All @@ -31,24 +29,20 @@ export const PackCardHeader = ({ data, title }: PackCardHeaderProps) => {
const { handleDeletePack } = useDeletePack(data.id);
const {
handleActionsOpenChange,
handleEdit,
handleSaveTitle,
isEditMode,
isOpen,
setIsOpen,
} = usePackTitleInput(data);
isEditModalOpen,
setIsEditModalOpen,
isTitleEditMode,
} = usePackActions({ data, refetch });

const { isDark } = useTheme();
const router = useRouter();

const optionValues: optionValues[] = [
{ label: 'Edit', value: 'Edit' },
{ label: 'Save', value: 'Save' },
{ label: 'Delete', value: 'Delete' },
];

const { xxs, xs, xxl } = useResponsive();

return (
<>
<CustomCardHeader
Expand Down Expand Up @@ -86,7 +80,7 @@ export const PackCardHeader = ({ data, title }: PackCardHeaderProps) => {
<EditableText
isLoading={isLoading}
defaultValue={title}
isFocused={isEditMode}
isFocused={isTitleEditMode}
onSave={handleSaveTitle}
/>
</RStack>
Expand All @@ -99,15 +93,17 @@ export const PackCardHeader = ({ data, title }: PackCardHeaderProps) => {
maxWidth: 100,
}}
>
<DropdownComponent
<CascadedDropdownComponent
value={null}
data={optionValues}
onValueChange={(value) => handleActionsOpenChange(value)}
placeholder={
<RIconButton
backgroundColor="transparent"
icon={<MaterialIcons name="more-horiz" size={20} />}
style={{ paddingTop: 20 }}
style={{
height: 20,
}}
/>
}
native={true}
Expand All @@ -116,6 +112,14 @@ export const PackCardHeader = ({ data, title }: PackCardHeaderProps) => {
)
}
/>
<EditPackModal
currentPack={data}
isOpen={isEditModalOpen}
onClose={() => {
setIsEditModalOpen(false);
}}
refetch={refetch}
/>
</>
);
};
42 changes: 42 additions & 0 deletions packages/app/components/card/PackCardHeader/usePackActions.ts
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 packages/app/components/card/PackCardHeader/usePackTitleInput.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const SearchItem = () => {
searchString={searchString}
onChange={setSearchString}
resultItemComponent={<ResultItem />}
placeholder="Search Item"
placeholder="Search Global Item"
results={results}
onSelect={handleSearchResultClick}
/>
Expand Down
103 changes: 103 additions & 0 deletions packages/app/modules/pack/components/EditPackModal.tsx
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>
);
};

0 comments on commit d3e10e3

Please sign in to comment.