Skip to content

Commit 5ddff23

Browse files
committed
feat: localstorage minting form on a per blueprint basis
If minting a hypercert from a blueprint, this will make sure the value are being cached for that specific blueprint. This prevents: a) the blueprint showing up in the hypercert create form when navigating to /hypercerts/new after opening the minting form with a blueprint, possibly causing confusion as the only difference will be the URI b) enable caching for modifications made for blueprints, and ensure they are for that blueprint only
1 parent a1b0ded commit 5ddff23

File tree

2 files changed

+34
-37
lines changed

2 files changed

+34
-37
lines changed

app/hypercerts/new/page.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ export default async function NewHypercertPage({
2121
return <div>Blueprint not found</div>;
2222
}
2323

24-
formValues = fetchedBlueprint.form_values as HypercertFormValues;
24+
formValues = {
25+
...fetchedBlueprint.form_values,
26+
allowlistEntries: fetchedBlueprint.form_values.allowlistEntries?.map(
27+
(entry) => ({
28+
...entry,
29+
units: BigInt(entry.units),
30+
}),
31+
),
32+
projectDates: {
33+
from: new Date(fetchedBlueprint.form_values.projectDates.from),
34+
to: new Date(fetchedBlueprint.form_values.projectDates.to),
35+
},
36+
} as HypercertFormValues;
2537
blueprintChainId = fetchedBlueprint.admins[0].chain_id
2638
? parseInt(fetchedBlueprint.admins[0].chain_id)
2739
: undefined;

components/hypercert/hypercert-minting-form/index.tsx

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useEffect, useRef, useState } from "react";
44
import useIsWriteable from "@/hooks/useIsWriteable";
55
import { useMintHypercert } from "@/hypercerts/hooks/useMintHypercert";
66
import { useLocalStorage } from "react-use";
7-
import { type FieldErrors, useForm, useWatch } from "react-hook-form";
7+
import { type FieldErrors, useForm } from "react-hook-form";
88
import { zodResolver } from "@hookform/resolvers/zod";
99
import { toast } from "@/components/ui/use-toast";
1010
import {
@@ -178,17 +178,16 @@ export function HypercertMintingForm({
178178
}) {
179179
const [currentStep, setCurrentStep] = useState(1);
180180
const [language, setLanguage] = useState("en-US");
181-
const {
182-
writeable,
183-
errors: writeableErrors,
184-
resetErrors: resetWriteableErrors,
185-
} = useIsWriteable();
181+
const { errors: writeableErrors } = useIsWriteable();
186182

187183
const { mutateAsync: mintHypercert } = useMintHypercert();
188184
const { mutateAsync: createBlueprint } = useCreateBlueprint();
189-
const [value, setValue] = useLocalStorage<HypercertFormValues>(
190-
"user-hypercert-create-form-data",
191-
formDefaultValues,
185+
const localStorageKey = blueprintId
186+
? `user-hypercert-blueprint-${blueprintId}-form-data`
187+
: "user-hypercert-create-form-data";
188+
const [values, setValues] = useLocalStorage<HypercertFormValues>(
189+
localStorageKey,
190+
presetValues || formDefaultValues,
192191
{
193192
raw: false,
194193
serializer: JSON.stringify,
@@ -202,37 +201,24 @@ export function HypercertMintingForm({
202201
blueprint_minter_address: true,
203202
});
204203

205-
const defaultValues = presetValues
206-
? {
207-
...presetValues,
208-
allowlistEntries: presetValues.allowlistEntries?.map((entry) => ({
209-
...entry,
210-
units: BigInt(entry.units),
211-
})),
212-
projectDates: {
213-
to: new Date(presetValues.projectDates.to),
214-
from: new Date(presetValues.projectDates.from),
215-
},
216-
}
217-
: value;
218-
219204
const form = useForm<HypercertFormValues>({
220205
resolver: zodResolver(formSchemaUsed),
221-
defaultValues,
206+
defaultValues: values,
222207
mode: "onBlur",
223208
});
224209

225-
const watchedValues = useWatch({
226-
control: form.control,
227-
name: ["title", "banner", "logo", "tags", "projectDates"],
228-
});
210+
const title = form.watch("title");
211+
const banner = form.watch("banner");
212+
const logo = form.watch("logo");
213+
const tags = form.watch("tags");
214+
const projectDates = form.watch("projectDates");
229215

230216
const cardPreviewData = {
231-
title: watchedValues[0] ?? formDefaultValues.title,
232-
banner: watchedValues[1] ?? formDefaultValues.banner,
233-
logo: watchedValues[2] ?? formDefaultValues.logo,
234-
tags: watchedValues[3] ?? formDefaultValues.tags,
235-
projectDates: watchedValues[4] ?? formDefaultValues.projectDates,
217+
title: title ?? formDefaultValues.title,
218+
banner: banner ?? formDefaultValues.banner,
219+
logo: logo ?? formDefaultValues.logo,
220+
tags: tags ?? formDefaultValues.tags,
221+
projectDates: projectDates ?? formDefaultValues.projectDates,
236222
};
237223
const cardRef = useRef<HTMLDivElement>(null);
238224

@@ -241,12 +227,11 @@ export function HypercertMintingForm({
241227
}, []);
242228

243229
const onBlur = () => {
244-
console.log("blur");
245-
setValue(form.getValues());
230+
setValues(form.getValues());
246231
};
247232

248233
const onReset = () => {
249-
setValue(formDefaultValues);
234+
setValues(formDefaultValues);
250235
form.reset(formDefaultValues);
251236
setCurrentStep(1);
252237
};

0 commit comments

Comments
 (0)