Skip to content

Commit

Permalink
feat(react): add function for forms
Browse files Browse the repository at this point in the history
  • Loading branch information
jrea committed Sep 27, 2022
1 parent 007bda5 commit 29d4f90
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
18 changes: 16 additions & 2 deletions packages/react/src/components/EntityForm/EntityForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SimpleForm, { Attribute, AttributeType } from '../../lib/SimpleForm';
type AllowedAny = any;

type Props = {
beforeMutate?: (data: AllowedAny) => AllowedAny;
onSuccess: (data: AllowedAny) => void;
onError?: (error: Error) => void;
cancelLink?: string;
Expand Down Expand Up @@ -60,15 +61,28 @@ export const processFormData = (data: AllowedAny, fields: Attribute[]) => {

export default function EntityForm(props: Props) {
const nile = useNile();
const { fields, org, entityType, cancelLink, onSuccess, onError } = props;
const {
fields,
org,
entityType,
cancelLink,
onSuccess,
onError,
beforeMutate,
} = props;

const handleMutate =
typeof beforeMutate === 'function'
? beforeMutate
: (data: AllowedAny): AllowedAny => data;

const mutation = useMutation(
(data: AllowedAny) => {
const body = processFormData(data, fields);
return nile.entities.createInstance({
org,
type: entityType,
body,
body: handleMutate(body),
});
},
{
Expand Down
14 changes: 10 additions & 4 deletions packages/react/src/components/LoginForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import React from 'react';
import { useMutation } from '@tanstack/react-query';
import { LoginInfo } from '@theniledev/js';

import { Attribute } from '../../lib/SimpleForm/types';
import { useNile } from '../../context';
import SimpleForm, { AttributeType } from '../../lib/SimpleForm';

import { Props } from './types';
import { Props, AllowedAny } from './types';

export default function LoginForm(props: Props) {
const nile = useNile();

const { attributes, onSuccess, onError } = props;
const { attributes, onSuccess, onError, beforeMutate } = props;

const handleMutate =
typeof beforeMutate === 'function'
? beforeMutate
: (data: AllowedAny): AllowedAny => data;

const mutation = useMutation(
(data: { email: string; password: string }) => {
const { email, password } = data;
return nile.users.loginUser({ loginInfo: { email, password } });
const _data = handleMutate(data);
return nile.users.loginUser({ loginInfo: _data as LoginInfo });
},
{
onSuccess: (token, data) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/LoginForm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Attribute } from '../../lib/SimpleForm/types';

type LoginSuccess = (LoginInfo: { email: string; password: string }) => void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AllowedAny = any;

export interface Props {
beforeMutate?: (data: AllowedAny) => AllowedAny;
onSuccess: LoginSuccess;
onError?: (error: Error) => void;
attributes?: Attribute[];
Expand Down
10 changes: 7 additions & 3 deletions packages/react/src/components/OrganizationForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useNile } from '../../context';
import { useMutation } from '../../lib/queries';
import SimpleForm, { AttributeType } from '../../lib/SimpleForm';

import { Props } from './types';
import { AllowedAny, Props } from './types';

const attributes = [
{
Expand All @@ -18,13 +18,17 @@ const attributes = [
];

export default function AddOrgForm(props: Props) {
const { onSuccess, onError, cancelLink } = props;
const { onSuccess, onError, cancelLink, beforeMutate } = props;
const nile = useNile();
const handleMutate =
typeof beforeMutate === 'function'
? beforeMutate
: (data: AllowedAny): AllowedAny => data;

const mutation = useMutation(
(data: CreateOrganizationRequest) =>
nile.organizations.createOrganization({
createOrganizationRequest: data,
createOrganizationRequest: handleMutate(data),
}),
{
onSuccess: (data: Organization) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/OrganizationForm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Organization } from '@theniledev/js';

type OrgCreateSuccess = (org: Organization) => void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AllowedAny = any;

export interface Props {
beforeMutate?: (data: AllowedAny) => AllowedAny;
onSuccess: OrgCreateSuccess;
onError?: (error: Error) => void;
cancelLink?: string;
Expand Down

0 comments on commit 29d4f90

Please sign in to comment.