-
Notifications
You must be signed in to change notification settings - Fork 0
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
7a71a2d
commit cd91e37
Showing
14 changed files
with
259 additions
and
18 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
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,37 @@ | ||
import { DefaultError, UseMutationOptions, QueryClient, useMutation } from "@tanstack/react-query"; | ||
import { useRef } from "react"; | ||
import toast, { ValueOrFunction, Renderable, Toast } from "react-hot-toast"; | ||
|
||
type AppMutationOptions = { | ||
toast?: { | ||
loading: ValueOrFunction<Renderable, Toast>; | ||
success: ValueOrFunction<Renderable, Toast>; | ||
error: ValueOrFunction<Renderable, Toast>; | ||
}; | ||
} | ||
type useMutationOptions<T, TError = DefaultError, TVariables = void, TContext = unknown> = UseMutationOptions<T, TError, TVariables, TContext> & AppMutationOptions; | ||
|
||
export const useAppMutation = <T, TError = DefaultError, TVariables = void, TContext = unknown>(options: useMutationOptions<T, TError, TVariables, TContext> , queryClient: QueryClient) => { | ||
const toastIdRef = useRef<string>(); | ||
|
||
const originMutationFn = options.mutationFn!; | ||
options.mutationFn = (variables) => { | ||
if (toastIdRef.current) return new Promise((res, rej) => {rej('Another mutation is in progress')}); | ||
if (options.toast) toastIdRef.current = toast.loading(options.toast.loading); | ||
return originMutationFn(variables); | ||
} | ||
|
||
const originOnSettled = options.onSettled!; | ||
options.onSettled = (data: any, error: any, variables, context) => { | ||
error && console.error(error); | ||
if (error == 'Another mutation is in progress') return new Promise(() => {}); | ||
if (options.toast) { | ||
if (error) toast.error(options.toast.error, { id: toastIdRef.current }); | ||
else toast.success(options.toast.success, { id: toastIdRef.current }); | ||
toastIdRef.current = undefined; | ||
} | ||
return originOnSettled(data, error, variables, context); | ||
} | ||
|
||
return useMutation(options, queryClient); | ||
}; |
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
24 changes: 24 additions & 0 deletions
24
services/client/src/core/form/custom-fields/CustomFieldTemplate.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React, { FC, useEffect } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { AppFieldProps } from './Field.type'; | ||
|
||
type RegisterProps = ReturnType<ReturnType<typeof useForm>['register']>; | ||
type FieldProps = RegisterProps & AppFieldProps; | ||
|
||
export const CustomField: FC<FieldProps> = React.memo(props => { | ||
|
||
const [value, setValue] = React.useState<{ label: string; value: number } | null>(null); | ||
|
||
useEffect(() => { | ||
setValue(props.value); | ||
}, [props.value]); | ||
|
||
return ( | ||
<div className="relative "> | ||
<input className="hidden" {...props.register!(props.name)} /> | ||
<div className="h-[43px]"> | ||
{/* Put here your custom field */} | ||
</div> | ||
</div> | ||
); | ||
}); |
59 changes: 59 additions & 0 deletions
59
services/client/src/core/form/custom-fields/StatusFieldsInput.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { FC, useEffect } from 'react'; | ||
import { useForm } from 'react-hook-form'; | ||
import { AppFieldProps } from './Field.type'; | ||
import { Button, TextField } from '@mui/material'; | ||
|
||
type RegisterProps = ReturnType<ReturnType<typeof useForm>['register']>; | ||
type FieldProps = RegisterProps & AppFieldProps; | ||
|
||
export const StatusFieldsInput: FC<FieldProps> = React.memo(props => { | ||
|
||
|
||
const [newStatusKey, setNewStatusKey] = React.useState<string>(''); | ||
const addNewStatus = () => { | ||
if (newStatusKey) { | ||
props.setValue!({...props.value, [newStatusKey]: { label: '', correctState: '', helperText: '' }}); | ||
setNewStatusKey(''); | ||
} | ||
} | ||
|
||
|
||
const deleteKey = (key: string) => { | ||
const newValues = { ...props.value }; | ||
delete newValues[key]; | ||
props.setValue!(newValues); | ||
} | ||
|
||
const registerStatusKeyField = (key: string, field: 'label' | 'correctState' | 'helperText') => { | ||
return { | ||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => props.setValue!({...props.value, [key]: { ...props.value[key], [field]: e.target.value }}), | ||
value: props.value[key][field], | ||
} | ||
} | ||
|
||
return ( | ||
<div className="relative "> | ||
<input className="hidden" {...props.register!(props.name)} /> | ||
{props.value && Object.keys(props.value).length > 0 && Object.keys(props.value).map((key, index) => ( | ||
<div className='w-full mb-4 flex-wrap flex gap-[3%]'> | ||
<div className='flex gap-2 mb-4 w-full'> | ||
<TextField className='w-full' disabled value={key}/> | ||
<Button onClick={() => deleteKey(key)} color='error' variant='text' className='min-w-0' > | ||
<i className="fas fa-trash"></i> | ||
</Button> | ||
</div> | ||
<TextField helperText="שם סנסור" className='w-[31.333%] mb-4' {...registerStatusKeyField(key, 'label')}/> | ||
<TextField helperText="מצב תקין" className='w-[31.333%] mb-4' {...registerStatusKeyField(key, 'correctState')}/> | ||
<TextField helperText="טקסט עזר" className='w-[31.333%] mb-4' {...registerStatusKeyField(key, 'helperText')}/> | ||
</div> | ||
))} | ||
<div className='flex gap-4'> | ||
<TextField helperText='' className='flex-1' value={newStatusKey} onChange={e => setNewStatusKey(e.target.value)} /> | ||
<Button variant='outlined' onClick={addNewStatus}> | ||
<i className="fas fa-plus me-2"></i> | ||
הוסף שדה | ||
</Button> | ||
</div> | ||
</div> | ||
); | ||
}); |
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,10 +1,12 @@ | ||
import { Checkbox, TextField } from '@mui/material'; | ||
import { CountryField } from './CountryField'; | ||
import { StatusFieldsInput } from './StatusFieldsInput'; | ||
|
||
export const FieldMap = { | ||
text: ({ name, register, ...props }: any) => <TextField autoComplete="off" {...props} className='rounded overflow-hidden' {...register(name)} />, | ||
number: ({ name, register, ...props }: any) => <TextField {...props} {...register(name)} type="number" />, | ||
boolean: ({ name, register, ...props }: any) => <Checkbox {...props} {...register(name)} />, | ||
date: ({ name, register, ...props }: any) => <TextField {...props} {...register(name)} />, | ||
country: (props: any) => <CountryField {...props} /> | ||
text: ({ name, register, setValue, ...props }: any) => <TextField autoComplete="off" {...props} className='rounded overflow-hidden' {...register(name)} />, | ||
number: ({ name, register, setValue, ...props }: any) => <TextField {...props} {...register(name)} type="number" />, | ||
boolean: ({ name, register, setValue, ...props }: any) => <Checkbox {...props} {...register(name)} />, | ||
date: ({ name, register, setValue, ...props }: any) => <TextField {...props} {...register(name)} />, | ||
country: (props: any) => <CountryField {...props} />, | ||
status_fields: (props: any) => <StatusFieldsInput {...props} /> | ||
} as const; |
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
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
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,32 @@ | ||
import { Button } from '@mui/material'; | ||
import React, { FC } from 'react'; | ||
import { useMutation_CreateDeviceType } from '../../../core/api/api'; | ||
import { useForm } from 'react-hook-form'; | ||
import { AppForm } from '../../../core/form/AppForm'; | ||
|
||
interface TypeFormProps extends React.PropsWithChildren {} | ||
|
||
export const TypeForm: FC<TypeFormProps> = React.memo(props => { | ||
const mutation_createType = useMutation_CreateDeviceType() | ||
const form = useForm(); | ||
const onSubmit = form.handleSubmit((data) => { | ||
|
||
mutation_createType.mutate({...data, status_fields: {x: '2'}}) | ||
}); | ||
return ( | ||
<> | ||
<AppForm | ||
form={form} | ||
onSubmit={onSubmit} | ||
submitText='שמור' | ||
fields={[{ | ||
name: 'name', | ||
helperText: 'שם הסוג', | ||
type: 'text', | ||
}, { | ||
name: 'status_fields', | ||
type: 'status_fields', | ||
}]}/> | ||
</> | ||
); | ||
}); |
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,11 @@ | ||
import React from 'react'; | ||
import { FC } from 'react'; | ||
import { TypeForm } from './TypeForm'; | ||
|
||
export const TypesPage: FC = React.memo(() => { | ||
return ( | ||
<div className='p-4'> | ||
<TypeForm /> | ||
</div> | ||
); | ||
}); |
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