Skip to content

Commit a238863

Browse files
Campaign Fixes (#354)
* fixed issue with start date * es lint fix * change field to number field * es lint fix --------- Co-authored-by: Carina Akaia <cvo.akaia@gmail.com>
1 parent d7f90cf commit a238863

File tree

3 files changed

+35
-24
lines changed

3 files changed

+35
-24
lines changed

src/entities/campaign/components/CampaignForm.tsx

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { ChangeEvent, useEffect, useState } from "react";
22

3+
import { Temporal } from "temporal-polyfill";
4+
35
import { IPFS_NEAR_SOCIAL_URL } from "@/common/constants";
46
import { Campaign } from "@/common/contracts/core/campaigns";
57
import { yoctoNearToFloat } from "@/common/lib";
@@ -11,12 +13,6 @@ import { NearInputField } from "@/entities/_shared";
1113

1214
import { useCampaignForm } from "../hooks/forms";
1315

14-
const formatTimestampForInput = (timestamp: number) => {
15-
if (!timestamp) return "";
16-
const date = new Date(timestamp);
17-
return date.toISOString().slice(0, 16);
18-
};
19-
2016
export const CampaignForm = ({
2117
existingData,
2218
campaignId,
@@ -50,17 +46,13 @@ export const CampaignForm = ({
5046
form.setValue("max_amount", yoctoNearToFloat(existingData.max_amount));
5147
}
5248

53-
form.setValue(
54-
"start_ms",
55-
existingData?.start_ms ? formatTimestampForInput(existingData?.start_ms) : "",
56-
);
49+
form.setValue("start_ms", existingData?.start_ms);
5750

58-
form.setValue(
59-
"end_ms",
60-
existingData?.end_ms ? formatTimestampForInput(existingData?.end_ms) : "",
61-
);
51+
if (existingData?.end_ms) {
52+
form.setValue("end_ms", existingData?.end_ms);
53+
}
6254
}
63-
}, [isUpdate, existingData, form]);
55+
}, [isUpdate, existingData]);
6456

6557
const handleCoverImageChange = async (e: ChangeEvent) => {
6658
const target = e.target as HTMLInputElement;
@@ -209,11 +201,19 @@ export const CampaignForm = ({
209201
<FormField
210202
control={form.control}
211203
name="start_ms"
212-
render={({ field }) => (
204+
render={({ field: { value, ...field } }) => (
213205
<TextField
214-
label="Start Date"
215206
{...field}
216-
required
207+
required={!!watch("min_amount")}
208+
label="Start Date"
209+
value={
210+
typeof value === "number"
211+
? Temporal.Instant.fromEpochMilliseconds(value)
212+
.toZonedDateTimeISO(Temporal.Now.timeZoneId())
213+
.toPlainDateTime()
214+
.toString({ smallestUnit: "minute" })
215+
: undefined
216+
}
217217
classNames={{ root: "lg:w-90" }}
218218
type="datetime-local"
219219
/>
@@ -222,11 +222,19 @@ export const CampaignForm = ({
222222
<FormField
223223
control={form.control}
224224
name="end_ms"
225-
render={({ field }) => (
225+
render={({ field: { value, ...field } }) => (
226226
<TextField
227+
{...field}
227228
required={!!watch("min_amount")}
228229
label="End Date"
229-
{...field}
230+
value={
231+
typeof value === "number"
232+
? Temporal.Instant.fromEpochMilliseconds(value)
233+
.toZonedDateTimeISO(Temporal.Now.timeZoneId())
234+
.toPlainDateTime()
235+
.toString({ smallestUnit: "minute" })
236+
: undefined
237+
}
230238
classNames={{ root: "lg:w-90" }}
231239
type="datetime-local"
232240
/>

src/entities/campaign/hooks/forms.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useCallback, useEffect } from "react";
33
import { zodResolver } from "@hookform/resolvers/zod";
44
import { useRouter } from "next/router";
55
import { SubmitHandler, useForm, useWatch } from "react-hook-form";
6+
import { Temporal } from "temporal-polyfill";
67
import { infer as FromSchema } from "zod";
78

89
import { campaignsContractClient } from "@/common/contracts/core/campaigns";
@@ -79,7 +80,7 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
7980
});
8081
}, [values, self]);
8182

82-
const timeToMilliseconds = (time: string) => {
83+
const timeToMilliseconds = (time: number) => {
8384
return new Date(time).getTime();
8485
};
8586

@@ -152,7 +153,8 @@ export const useCampaignForm = ({ campaignId }: { campaignId?: CampaignId }) =>
152153
max_amount: floatToYoctoNear(values.max_amount) as any,
153154
}),
154155
...(values.start_ms &&
155-
timeToMilliseconds(values.start_ms) > Date.now() && {
156+
!campaignId &&
157+
timeToMilliseconds(values.start_ms) >= Date.now() && {
156158
start_ms: timeToMilliseconds(values.start_ms),
157159
}),
158160
...(values.end_ms && {

src/entities/campaign/models/schema.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from "zod";
22

33
import { NETWORK } from "@/common/_config";
44
import { near } from "@/common/blockchains/near-protocol/client";
5+
import { futureTimestamp } from "@/common/lib";
56

67
export const campaignFormSchema = z
78
.object({
@@ -14,8 +15,8 @@ export const campaignFormSchema = z
1415
min_amount: z.number().optional(),
1516
max_amount: z.number().optional(),
1617
cover_image_url: z.string().optional(),
17-
start_ms: z.string().min(1, "Start Time is Required"),
18-
end_ms: z.string()?.optional(),
18+
start_ms: futureTimestamp.describe("Campaign Start Date"),
19+
end_ms: futureTimestamp.describe("Campaign End Date"),
1920
owner: z.string()?.optional(),
2021
recipient: z.string().refine(near.isAccountValid, {
2122
message: `Account does not exist on ${NETWORK}`,

0 commit comments

Comments
 (0)