-
Notifications
You must be signed in to change notification settings - Fork 92
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
e293f80
commit b57d82f
Showing
10 changed files
with
480 additions
and
537 deletions.
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
packages/react/src/components/about/placeholder/FormFields.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,189 @@ | ||
import { useMemo, useState } from "react"; | ||
import { useFormContext, Controller } from "react-hook-form"; | ||
import { | ||
FormItem, | ||
FormLabel, | ||
FormControl, | ||
FormDescription, | ||
FormMessage, | ||
FormField, | ||
} from "@/shadcn/ui/form"; | ||
import { Input } from "@/shadcn/ui/input"; | ||
import { RadioGroup, RadioGroupItem } from "@/shadcn/ui/radio-group"; | ||
import { DatePicker } from "@/components/common/DatePicker"; | ||
import { ChannelPicker } from "@/components/channel/ChannelPicker"; | ||
import { useTranslation } from "react-i18next"; | ||
import { | ||
Select, | ||
SelectContent, | ||
SelectGroup, | ||
SelectItem, | ||
SelectTrigger, | ||
SelectValue, | ||
} from "@/shadcn/ui/select"; | ||
import { allTimezones } from "../../settings/timezones"; | ||
import { localeAtom } from "@/store/i18n"; | ||
import { useAtomValue } from "jotai"; | ||
|
||
export const FormInput = ({ | ||
name, | ||
label, | ||
description, | ||
...props | ||
}: { | ||
name: string; | ||
label: string; | ||
description?: string; | ||
} & Omit<Parameters<typeof Input>[0], "id">) => { | ||
const { control } = useFormContext(); | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={name} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{label}</FormLabel> | ||
<FormControl> | ||
<Input {...field} {...props} /> | ||
</FormControl> | ||
{description && <FormDescription>{description}</FormDescription>} | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export const FormRadioGroup = ({ | ||
name, | ||
label, | ||
options, | ||
...props | ||
}: { | ||
name: string; | ||
label: string; | ||
options: { value: string; label: string }[]; | ||
} & Omit< | ||
Parameters<typeof RadioGroup>[0], | ||
"onValueChange" | "defaultValue" | ||
>) => { | ||
const { control } = useFormContext(); | ||
return ( | ||
<FormField | ||
control={control} | ||
name={name} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{label}</FormLabel> | ||
<FormControl> | ||
<RadioGroup | ||
onValueChange={field.onChange} | ||
defaultValue={field.value} | ||
{...props} | ||
> | ||
{options.map(({ value, label }) => ( | ||
<FormItem | ||
key={value} | ||
className="flex items-center space-x-3 space-y-0" | ||
> | ||
<FormControl> | ||
<RadioGroupItem value={value} /> | ||
</FormControl> | ||
<FormLabel className="font-normal">{label}</FormLabel> | ||
</FormItem> | ||
))} | ||
</RadioGroup> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
); | ||
}; | ||
|
||
export const FormDatePicker = ({ | ||
name, | ||
label, | ||
...props | ||
}: Parameters<typeof DatePicker>[0] & { name: string; label: string }) => { | ||
const { t } = useTranslation(); | ||
const { control } = useFormContext(); | ||
const { dayjs } = useAtomValue(localeAtom); | ||
const [timezone, setTimezone] = useState("Asia/Tokyo"); | ||
const timezoneOptions = useMemo(() => { | ||
return Object.keys(allTimezones).map((tz) => { | ||
const now = dayjs().tz(tz); | ||
const offset = now.format("Z"); | ||
return { | ||
value: tz, | ||
label: tz, | ||
offset: offset, | ||
}; | ||
}); | ||
}, [dayjs]); | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={name} | ||
render={({ field }) => ( | ||
<FormItem className="flex flex-col"> | ||
<FormLabel>{label}</FormLabel> | ||
<FormControl> | ||
<div className="flex items-center gap-2"> | ||
<Select value={timezone} onValueChange={setTimezone}> | ||
<SelectTrigger className="h-9 w-auto shrink"> | ||
<SelectValue placeholder="Select timezone" /> | ||
</SelectTrigger> | ||
<SelectContent> | ||
<SelectGroup> | ||
{timezoneOptions.map(({ label, value, offset }) => ( | ||
<SelectItem key={value} value={value}> | ||
{label} ({offset}) | ||
</SelectItem> | ||
))} | ||
</SelectGroup> | ||
</SelectContent> | ||
</Select> | ||
<DatePicker | ||
selected={field.value ? new Date(field.value) : undefined} | ||
timezone={timezone} | ||
{...props} | ||
onSelect={(date: Date) => field.onChange(date.toISOString())} | ||
/> | ||
</div> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
); | ||
}; | ||
export const FormChannelPicker = ({ | ||
name, | ||
label, | ||
...props | ||
}: Parameters<typeof ChannelPicker>[0] & { name: string; label: string }) => { | ||
const { control } = useFormContext(); | ||
|
||
return ( | ||
<FormField | ||
control={control} | ||
name={name} | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>{label}</FormLabel> | ||
<FormControl> | ||
<ChannelPicker | ||
{...props} | ||
value={field.value} | ||
onSelect={({ id }) => field.onChange(id)} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
); | ||
}; |
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,28 @@ | ||
import { z } from "zod"; | ||
|
||
export const placeholderSchema = z.object({ | ||
id: z.string().optional(), | ||
channel_id: z.string().min(1, "Channel ID is required"), | ||
duration: z.number().min(1, "Duration must be at least 1 minute"), | ||
liveTime: z.string().min(1, "Live time is required"), | ||
title: z.object({ | ||
credits: z.object({ | ||
editor: z.object({ | ||
user: z.string().optional(), | ||
name: z.string().min(1, "Editor name is required"), | ||
}), | ||
}), | ||
name: z.string().min(1, "Title is required"), | ||
jp_name: z.string().optional(), | ||
link: z.string().url("Invalid URL"), | ||
thumbnail: z.string().url("Invalid URL"), | ||
placeholderType: z.enum([ | ||
"event", | ||
"external-stream", | ||
"scheduled-yt-stream", | ||
]), | ||
certainty: z.enum(["certain", "likely"]), | ||
}), | ||
}); | ||
|
||
export type PlaceholderFormData = z.infer<typeof placeholderSchema>; |
36 changes: 36 additions & 0 deletions
36
packages/react/src/components/about/placeholder/usePlaceholderSubmit.ts
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,36 @@ | ||
import { usePlaceholderMutation } from "@/services/video.service"; | ||
import { useToast } from "@/shadcn/ui/use-toast"; | ||
import { useTranslation } from "react-i18next"; | ||
import { PlaceholderFormData } from "./schema"; | ||
|
||
export const usePlaceholderSubmit = (token: string | null) => { | ||
const { t } = useTranslation(); | ||
const { toast } = useToast(); | ||
const { mutate } = usePlaceholderMutation(); | ||
|
||
const onSubmit = (data: PlaceholderFormData) => { | ||
mutate( | ||
{ | ||
body: { ...data, duration: data.duration * 60 }, | ||
token, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
toast({ | ||
title: t("component.addPlaceholder.success"), | ||
}); | ||
}, | ||
onError: (error) => { | ||
console.error(error); | ||
toast({ | ||
title: t("component.addPlaceholder.error"), | ||
description: error.message, | ||
variant: "error", | ||
}); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
return onSubmit; | ||
}; |
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
Oops, something went wrong.