Skip to content

Commit

Permalink
modals refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
taronaleksanian committed Jan 12, 2025
1 parent 0db3c86 commit 77450f9
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 68 deletions.
1 change: 1 addition & 0 deletions packages/app/modules/item/components/AddItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export const AddItem = ({
<ItemForm
validationSchema={isEdit ? editItemSchema : addItemSchema}
handleSubmit={handleSubmit}
closeModalHandler={closeModalHandler}
defaultValues={defaultValues}
isLoading={isLoading}
isEdit={isEdit}
Expand Down
7 changes: 0 additions & 7 deletions packages/app/modules/item/components/AddItemModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ export const AddItemModal = ({
/>
}
showTrigger={showTrigger}
footerButtons={[
{
label: 'Cancel',
color: '#B22222',
onClick: (_, closeModal) => closeModal(),
},
]}
footerComponent={undefined}
isOpen={isAddItemModalOpen}
onOpen={() => setIsAddItemModalOpen(true)}
Expand Down
9 changes: 6 additions & 3 deletions packages/app/modules/item/components/ImportForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as DocumentPicker from 'expo-document-picker';
import { useImportItem, useImportFromBucket } from '../hooks';
import { useImportPackItem } from '../../pack/hooks';
import useResponsive from 'app/hooks/useResponsive';
import RPrimaryButton from 'app/components/RPrimaryButton';

interface ImportFormProps {
showSubmitButton?: boolean;
Expand Down Expand Up @@ -183,9 +184,11 @@ export const ImportForm: FC<ImportFormProps> = ({
style={{ width: '100%' }}
/>
</View>
<RButton onClick={handleItemImport} disabled={isImporting}>
<RText style={{ color: currentTheme.colors.text }}>{buttonText}</RText>
</RButton>
<RPrimaryButton
onPress={handleItemImport}
disabled={isImporting}
label={buttonText}
/>
</View>
);
};
30 changes: 22 additions & 8 deletions packages/app/modules/item/components/ItemForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
FormSelect,
FormRadioGroup,
DropdownComponent,
RButton,
XStack,
} from '@packrat/ui';
import { Platform, View } from 'react-native';

Expand All @@ -28,6 +30,7 @@ interface ItemFormProps {
isEdit?: boolean;
defaultValues: Partial<Item>;
validationSchema: any;
closeModalHandler?: () => void;
currentPack?: {
items: Array<{
category: {
Expand All @@ -39,6 +42,7 @@ interface ItemFormProps {

export const ItemForm = ({
handleSubmit,
closeModalHandler,
showSubmitButton = true,
isLoading,
isEdit,
Expand Down Expand Up @@ -123,14 +127,24 @@ export const ItemForm = ({
style={{ width: '100%' }}
/>
<FormRadioGroup name={'type' as any} options={radioOptions} />

{showSubmitButton && (
<SubmitButton onSubmit={handleSubmit}>
<RText style={{ color: currentTheme.colors.white }}>
{isLoading ? 'Loading..' : isEdit ? 'Edit item' : 'Add Item'}
</RText>
</SubmitButton>
)}
<XStack style={{ justifyContent: 'flex-end', gap: 8 }}>
{showSubmitButton && (
<SubmitButton onSubmit={handleSubmit}>
<RText style={{ color: currentTheme.colors.white }}>
{isLoading ? 'Loading..' : isEdit ? 'Edit item' : 'Add Item'}
</RText>
</SubmitButton>
)}
{closeModalHandler && (
<RButton
backgroundColor="#B22222"
onPress={closeModalHandler}
label="Cancel"
>
<RText style={{ color: '#fff' }}>Cancel</RText>
</RButton>
)}
</XStack>
</RStack>
</Form>
</View>
Expand Down
57 changes: 28 additions & 29 deletions packages/app/modules/pack/components/EditPackModal.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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 { 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';
import { Form, FormInput, FormSwitch, useFormSubmitTrigger } from '@packrat/ui';
import { editPack as editPackSchema } from '@packrat/validations';

interface EditPackModalProps {
isOpen?: boolean;
Expand All @@ -23,14 +21,16 @@ export const EditPackModal: React.FC<EditPackModalProps> = ({
}) => {
const [packName, setPackName] = useState(currentPack?.name ?? '');
const [isPublic, setIsPublic] = useState(currentPack?.is_public ?? true);
const [formRef, triggerSubmit] = useFormSubmitTrigger();
const { editPack, isLoading, isError } = useEditPack();

const handleEditPack = async () => {
const handleEditPack = async (closeModal, { name: packName, is_public }) => {
try {
editPack(
{ id: currentPack.id, name: packName, is_public: isPublic },
{ id: currentPack.id, name: packName, is_public },
{
onSuccess: () => {
closeModal();
onClose?.();
refetch?.();
},
Expand All @@ -53,6 +53,15 @@ export const EditPackModal: React.FC<EditPackModalProps> = ({
showTrigger={false}
title="Edit Pack"
footerButtons={[
{
label: 'Save',
color: '#232323',
onClick: (_, closeModal) => {
if (typeof triggerSubmit === 'function') {
triggerSubmit(closeModal);
}
},
},
{
label: 'Cancel',
color: '#B22222',
Expand All @@ -65,31 +74,21 @@ export const EditPackModal: React.FC<EditPackModalProps> = ({
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"
<RStack style={{ width: 320, gap: 8 }}>
<Form
ref={formRef}
onSubmit={handleEditPack}
validationSchema={editPackSchema}
defaultValues={{
id: currentPack?.id,
name: currentPack?.name,
is_public: currentPack?.is_public,
}}
>
<Switch.Thumb />
</RSwitch>
<FormInput label="Name" name="name" />
<FormSwitch name="is_public" labelLeft="Is public" size="$2.5" />
</Form>
</RStack>
<RButton onPress={handleEditPack} disabled={isLoading}>
{isLoading ? 'Saving...' : 'Save'}
</RButton>
{isError && (
<RStack>
<RText style={{ color: 'red' }}>Failed to save changes</RText>
Expand Down
20 changes: 8 additions & 12 deletions packages/ui/src/form/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export { useFormContext as useAppFormContext } from 'react-hook-form';
interface Props extends Omit<UseFormProps, 'resolver'> {
validationSchema?: any;
formRef?: any;
onSubmit?: FormSubmitHandler<FieldValues>;
onSubmit?: any;
children: ReactNode;
}

Expand All @@ -23,17 +23,13 @@ export const Form = forwardRef<any, Props>(
resolver: validationSchema ? zodResolver(validationSchema) : undefined,
});

useImperativeHandle(
ref,
() => {
return {
submit: (...params) =>
typeof onSubmit === 'function' &&
form.handleSubmit(onSubmit.bind(null, ...params))(),
};
},
[form, onSubmit],
);
useImperativeHandle(ref, () => {
return {
submit: (...params) =>
typeof onSubmit === 'function' &&
form.handleSubmit(onSubmit.bind(null, ...params))(),
};
}, [form, onSubmit]);

return (
<FormProvider {...form}>
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/src/form/components/FormSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import type { StyleProp, ViewStyle } from 'react-native';
export { LmSwitch as Switch } from '../lib';

interface Props
extends LmSwitchRhfProps<{ label: string | number; value: any }> {
extends Omit<
LmSwitchRhfProps<{ label: string | number; value: any }>,
'name'
> {
style?: StyleProp<ViewStyle>;
name: string;
}

// TODO change the name to "Input" after handling tamagui all components export,
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/form/components/SubmitButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export const SubmitButton = <T extends {}>({
return (
<LmSubmitButtonRhf
$theme-dark={{
...({ color: 'text', backgroundColor: '$colors.background' } as any),
...({ backgroundColor: '$colors.background' } as any),
}}
$theme-light={{
...({
color: 'text',
backgroundColor: '#232323',
fontWeight: 700,
} as any),
Expand All @@ -27,6 +26,7 @@ export const SubmitButton = <T extends {}>({
}}
disabledStyle={{ opacity: 0.6 }}
style={{
color: '#fff',
...safeStyle,
}}
{...props}
Expand Down
14 changes: 8 additions & 6 deletions packages/ui/src/modal/BaseModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ export const BaseModal = ({
{children}
</ModalProvider>

<RStack
style={{ alignSelf: 'flex-end', flexDirection: 'row' }}
gap="$4"
>
{memoFooterButtons}
</RStack>
{!!memoFooterButtons?.length && (
<RStack
style={{ alignSelf: 'flex-end', flexDirection: 'row' }}
gap="$2"
>
{memoFooterButtons}
</RStack>
)}
{footerElement}
<Dialog.Close asChild>
<Button
Expand Down

0 comments on commit 77450f9

Please sign in to comment.