Skip to content

Commit

Permalink
Merge pull request #313 from Chia-Network/refactor/create-token-form
Browse files Browse the repository at this point in the history
Refactor/create token form
  • Loading branch information
wwills2 authored Aug 29, 2024
2 parents f7e2e91 + f9d186a commit 11b96bc
Show file tree
Hide file tree
Showing 13 changed files with 2,338 additions and 907 deletions.
2,796 changes: 1,951 additions & 845 deletions package-lock.json

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,53 @@
"electron:package:linux": "npm run clean && npm run build && electron-builder -l -c.extraMetadata.main=build/main.js"
},
"dependencies": {
"@reduxjs/toolkit": "^2.2.3",
"@reduxjs/toolkit": "^2.2.7",
"@types/styled-components": "^5.1.34",
"@xterm/xterm": "^5.3.0",
"dayjs": "^1.11.10",
"@xterm/xterm": "^5.5.0",
"dayjs": "^1.11.13",
"express": "^4.19.2",
"flowbite": "^2.3.0",
"flowbite": "^2.5.1",
"flowbite-react": "^0.7.8",
"formik": "^2.4.5",
"formik": "^2.4.6",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-content-loader": "^7.0.0",
"react-dom": "^18.2.0",
"react": "^18.3.1",
"react-content-loader": "^7.0.2",
"react-dom": "^18.3.1",
"react-icons": "^5.3.0",
"react-intl": "^6.6.5",
"react-redux": "^9.1.1",
"react-router-dom": "^6.22.3",
"react-intl": "^6.6.8",
"react-redux": "^9.1.2",
"react-router-dom": "^6.26.1",
"redux-persist": "^6.0.0",
"simplebar": "^6.2.5",
"simplebar-react": "^3.2.4",
"styled-components": "^6.1.8",
"simplebar": "^6.2.7",
"simplebar-react": "^3.2.6",
"styled-components": "^6.1.12",
"uuid": "^9.0.1",
"yup": "^1.4.0"
},
"devDependencies": {
"@commitlint/config-conventional": "^19.2.2",
"@types/react": "^18.2.79",
"@types/react-dom": "^18.2.25",
"@commitlint/config-conventional": "^19.4.1",
"@types/react": "^18.3.4",
"@types/react-dom": "^18.3.0",
"@types/react-router-dom": "^5.3.3",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vitejs/plugin-react-swc": "^3.6.0",
"autoprefixer": "^10.4.19",
"@vitejs/plugin-react-swc": "^3.7.0",
"autoprefixer": "^10.4.20",
"concurrently": "^8.2.2",
"cross-env": "^7.0.3",
"electron": "^28.3.0",
"electron": "^28.3.3",
"electron-builder": "^24.13.3",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.11",
"husky": "^9.1.5",
"lint-staged": "^15.2.9",
"postcss": "^8.4.41",
"prettier": "^3.3.3",
"standard-version": "^9.5.0",
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5",
"vite": "^5.2.9",
"tailwindcss": "^3.4.10",
"typescript": "^5.5.4",
"vite": "^5.4.2",
"wait-on": "^7.2.0"
},
"standard-version": {
Expand Down
30 changes: 27 additions & 3 deletions src/renderer/api/cadt/v1/units.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { cadtApi, RECORDS_PER_PAGE, tokenizedUnitsTag, untokenizedUnitsTag } from './index';
import {
cadtApi,
projectsByIdsTag,
projectsTag,
RECORDS_PER_PAGE,
tokenizedUnitsTag,
untokenizedUnitsTag,
} from './index';
import { Unit } from '@/schemas/Unit.schema';

interface GetUnitsParams {
Expand All @@ -13,6 +20,8 @@ interface GetUnitParams {
warehouseUnitId: string;
}

interface TokenizeParams {}

export interface GetUnitsResponse {
page: number;
pageCount: number;
Expand Down Expand Up @@ -81,10 +90,25 @@ const unitsApi = cadtApi.injectEndpoints({
}),
keepUnusedDataFor: 0,
}),

tokenizeUnit: builder.mutation<any, TokenizeParams>({
query: (tokenizeParams: TokenizeParams) => ({
url: '/tokenize',
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: tokenizeParams,
}),
invalidatesTags: [untokenizedUnitsTag, tokenizedUnitsTag, projectsTag, projectsByIdsTag],
}),
}),
});

export const invalidateUnitsApiTag = unitsApi.util.invalidateTags;

export const { useGetTokenizedUnitsQuery, useGetUntokenizedUnitsQuery, useLazyGetUnitQuery, useGetUnitQuery } =
unitsApi;
export const {
useGetTokenizedUnitsQuery,
useGetUntokenizedUnitsQuery,
useLazyGetUnitQuery,
useGetUnitQuery,
useTokenizeUnitMutation,
} = unitsApi;
10 changes: 7 additions & 3 deletions src/renderer/components/blocks/buttons/FormButton.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Button, Spinner } from '@/components';
import { Button } from '@/components';

interface FormButtonProps {
isSubmitting: boolean;
Expand All @@ -9,8 +9,12 @@ interface FormButtonProps {

const FormButton: React.FC<FormButtonProps> = ({ isSubmitting, formikErrors, children }) => {
return (
<Button type="submit" disabled={isSubmitting || Boolean(Object.keys(formikErrors).length)}>
{isSubmitting ? <Spinner size="sm" light={true} /> : children}
<Button
type="submit"
disabled={isSubmitting || Boolean(Object.keys(formikErrors).length)}
isProcessing={isSubmitting}
>
{children}
</Button>
);
};
Expand Down
81 changes: 81 additions & 0 deletions src/renderer/components/blocks/forms/CreateTokenForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, { useCallback } from 'react';
import { noop } from 'lodash';
import { ErrorMessage, Field, Form, Formik } from 'formik';
import * as yup from 'yup';
import { FloatingLabel, FormButton, HelperText, Spacer } from '@/components';
import { FormattedMessage, IntlShape, useIntl } from 'react-intl';

interface FormProps {
onSubmit: (walletAddress: string) => Promise<void>;
onClearError?: () => void;
}

const CreateTokenForm: React.FC<FormProps> = ({ onSubmit, onClearError = noop }) => {
const intl: IntlShape = useIntl();

const validationSchema = yup.object({
walletAddress: yup
.string()
.required(intl.formatMessage({ id: 'wallet-address-is-required' }))
.matches(
/^xch[a-zA-Z0-9]{59}$/,
intl.formatMessage({ id: 'wallet-addresses-start-with-xch-and-are-62-characters-long' }),
),
});

const handleSubmit = useCallback(
async (values: { walletAddress: string }, { setSubmitting }) => {
await onSubmit(values.walletAddress);
setSubmitting(false);
},
[onSubmit],
);

const handleChange = useCallback(
(event, field) => {
onClearError();
field.onChange(event); // Call Formik's original onChange
},
[onClearError],
);

return (
<Formik initialValues={{ walletAddress: '' }} validationSchema={validationSchema} onSubmit={handleSubmit}>
{({ errors, touched, isSubmitting }) => (
<Form>
<div className="mb-4">
<HelperText className="text-gray-400 sentence-case">
<FormattedMessage id="carbon-token-recipient" />
</HelperText>
<Spacer size={5} />
<Field name="walletAddress">
{({ field }) => (
<FloatingLabel
id="walletAddress"
disabled={isSubmitting}
label={intl.formatMessage({ id: 'wallet-address' })}
color={errors.walletAddress && touched.walletAddress && 'error'}
variant="outlined"
required
type="text"
{...field}
onChange={(event) => handleChange(event, field)}
/>
)}
</Field>
{touched.walletAddress && <ErrorMessage name="walletAddress" component="div" className="text-red-600" />}
</div>
<div className="flex gap-4">
<FormButton isSubmitting={isSubmitting} formikErrors={errors}>
<p className="capitalize">
<FormattedMessage id="create-token" />
</p>
</FormButton>
</div>
</Form>
)}
</Formik>
);
};

export { CreateTokenForm };
Loading

0 comments on commit 11b96bc

Please sign in to comment.