-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from hypercerts-org/feature/update-hyperboard-…
…rendering-to-new-data-structure Feature/update hyperboard rendering to new data structure
- Loading branch information
Showing
15 changed files
with
497 additions
and
177 deletions.
There are no files selected for viewing
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,33 @@ | ||
import { Button, useDisclosure } from "@chakra-ui/react"; | ||
import { CreateOrUpdateHyperboardRegistryModal } from "@/components/admin/create-or-update-hyperboard-registry-modal"; | ||
|
||
export const AddHyperboardRegistryButton = ({ | ||
hyperboardId, | ||
registryId, | ||
size, | ||
}: { | ||
hyperboardId: string; | ||
registryId?: string; | ||
size?: string; | ||
}) => { | ||
const { onClose, onOpen, isOpen } = useDisclosure(); | ||
|
||
return ( | ||
<> | ||
<Button | ||
aria-label="Edit hyperboard registry" | ||
colorScheme="blue" | ||
onClick={onOpen} | ||
size={size} | ||
> | ||
Add registry to hyperboard | ||
</Button> | ||
<CreateOrUpdateHyperboardRegistryModal | ||
hyperboardId={hyperboardId} | ||
registryId={registryId} | ||
onClose={onClose} | ||
isOpen={isOpen} | ||
/> | ||
</> | ||
); | ||
}; |
22 changes: 22 additions & 0 deletions
22
components/admin/create-or-update-hyperboard-registry-modal.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,22 @@ | ||
import { ModalProps } from "@chakra-ui/modal"; | ||
import { GenericModal } from "@/components/GenericModal"; | ||
import { CreateOrUpdateHyperboardRegistryForm } from "@/components/forms/create-or-update-hyperboard-registry-form"; | ||
|
||
export const CreateOrUpdateHyperboardRegistryModal = ({ | ||
hyperboardId, | ||
registryId, | ||
...modalProps | ||
}: { | ||
registryId?: string; | ||
hyperboardId: string; | ||
} & Omit<ModalProps, "children">) => { | ||
return ( | ||
<GenericModal title="Edit hyperboard<>registry connection" {...modalProps}> | ||
<CreateOrUpdateHyperboardRegistryForm | ||
hyperboardId={hyperboardId} | ||
onComplete={modalProps.onClose} | ||
registryId={registryId} | ||
/> | ||
</GenericModal> | ||
); | ||
}; |
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,33 @@ | ||
import { IconButton, useDisclosure } from "@chakra-ui/react"; | ||
import { AiFillEdit } from "react-icons/ai"; | ||
import { CreateOrUpdateHyperboardRegistryModal } from "@/components/admin/create-or-update-hyperboard-registry-modal"; | ||
|
||
export const EditHyperboardRegistryButton = ({ | ||
hyperboardId, | ||
registryId, | ||
size, | ||
}: { | ||
hyperboardId: string; | ||
registryId?: string; | ||
size?: string; | ||
}) => { | ||
const { onClose, onOpen, isOpen } = useDisclosure(); | ||
|
||
return ( | ||
<> | ||
<IconButton | ||
aria-label="Edit hyperboard registry" | ||
icon={<AiFillEdit />} | ||
colorScheme="blue" | ||
onClick={onOpen} | ||
size={size} | ||
/> | ||
<CreateOrUpdateHyperboardRegistryModal | ||
hyperboardId={hyperboardId} | ||
registryId={registryId} | ||
onClose={onClose} | ||
isOpen={isOpen} | ||
/> | ||
</> | ||
); | ||
}; |
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
167 changes: 167 additions & 0 deletions
167
components/forms/create-or-update-hyperboard-registry-form.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,167 @@ | ||
import { Controller, useForm } from "react-hook-form"; | ||
import { | ||
Button, | ||
FormControl, | ||
FormErrorMessage, | ||
FormLabel, | ||
Input, | ||
useToast, | ||
VStack, | ||
} from "@chakra-ui/react"; | ||
import { useHypercertClient } from "@/components/providers"; | ||
import { SingleRegistrySelector } from "@/components/admin/registry-selector"; | ||
import { useFetchRegistryById } from "@/hooks/useFetchRegistryById"; | ||
import { useEffect } from "react"; | ||
import { useFetchHyperboardRegistryById } from "@/hooks/useFetchHyperboardRegistryById"; | ||
import { useGetAuthenticatedClient } from "@/hooks/useGetAuthenticatedClient"; | ||
import { useMyHyperboards } from "@/hooks/useMyHyperboards"; | ||
|
||
export interface CreateOrUpdateHyperboardRegistryFormValues { | ||
label: string; | ||
registry: { | ||
value: string; | ||
label: string; | ||
}; | ||
} | ||
|
||
export const CreateOrUpdateHyperboardRegistryForm = ({ | ||
hyperboardId, | ||
registryId, | ||
initialValues = {}, | ||
onComplete, | ||
}: { | ||
hyperboardId: string; | ||
registryId?: string; | ||
initialValues?: Partial<CreateOrUpdateHyperboardRegistryFormValues>; | ||
onComplete?: () => void; | ||
}) => { | ||
const { refetch } = useMyHyperboards(); | ||
const { data: registryData } = useFetchRegistryById(registryId); | ||
const { data: hyperboardRegistryData } = useFetchHyperboardRegistryById( | ||
hyperboardId, | ||
registryId, | ||
); | ||
|
||
const { | ||
control, | ||
register, | ||
setValue, | ||
formState: { errors, isSubmitting }, | ||
handleSubmit, | ||
} = useForm<CreateOrUpdateHyperboardRegistryFormValues>({ | ||
reValidateMode: "onBlur", | ||
defaultValues: initialValues, | ||
}); | ||
|
||
useEffect(() => { | ||
if (registryData?.data) { | ||
setValue("registry", { | ||
value: registryData.data.id, | ||
label: registryData.data.name, | ||
}); | ||
} | ||
}, [registryData?.data, setValue]); | ||
|
||
useEffect(() => { | ||
if (hyperboardRegistryData?.data?.label) { | ||
setValue("label", hyperboardRegistryData.data.label); | ||
} | ||
}, [hyperboardRegistryData?.data, setValue]); | ||
|
||
const toast = useToast(); | ||
const client = useHypercertClient(); | ||
|
||
const getClient = useGetAuthenticatedClient(); | ||
const isEdit = !!registryId; | ||
|
||
const onSubmit = async ( | ||
values: CreateOrUpdateHyperboardRegistryFormValues, | ||
) => { | ||
const supabase = await getClient(); | ||
|
||
if (!client) { | ||
toast({ | ||
title: "Client is not initialized", | ||
status: "error", | ||
}); | ||
return; | ||
} | ||
|
||
if (!supabase) { | ||
toast({ | ||
title: "Supabase is not initialized", | ||
status: "error", | ||
}); | ||
return; | ||
} | ||
|
||
if (!hyperboardId) { | ||
toast({ | ||
title: "Hyperboard ID is required", | ||
status: "error", | ||
}); | ||
return; | ||
} | ||
|
||
const upsertValue = { | ||
label: values.label, | ||
registry_id: values.registry.value, | ||
hyperboard_id: hyperboardId, | ||
}; | ||
|
||
try { | ||
await supabase.from("hyperboard_registries").upsert(upsertValue); | ||
} catch (e) { | ||
console.error(e); | ||
toast({ | ||
title: "Error", | ||
description: "Could not create or update hyperboard registry", | ||
status: "error", | ||
duration: 9000, | ||
isClosable: true, | ||
}); | ||
return; | ||
} | ||
|
||
await refetch(); | ||
onComplete?.(); | ||
}; | ||
|
||
return ( | ||
<form onSubmit={handleSubmit(onSubmit)}> | ||
<VStack> | ||
<FormControl isInvalid={!!errors.registry}> | ||
<FormLabel>Registry ID</FormLabel> | ||
<Controller | ||
control={control} | ||
render={(props) => ( | ||
<SingleRegistrySelector | ||
isDisabled={isEdit || isSubmitting} | ||
{...props.field} | ||
/> | ||
)} | ||
name={"registry"} | ||
/> | ||
<FormErrorMessage>{errors.registry?.message}</FormErrorMessage> | ||
</FormControl> | ||
<FormControl isInvalid={!!errors.label}> | ||
<FormLabel>Label</FormLabel> | ||
<Input | ||
disabled={isSubmitting} | ||
{...register("label", { | ||
required: "This is required", | ||
minLength: { | ||
value: 4, | ||
message: "Minimum length should be 4", | ||
}, | ||
})} | ||
/> | ||
<FormErrorMessage>{errors.label?.message}</FormErrorMessage> | ||
</FormControl> | ||
<Button mt={4} colorScheme="teal" type="submit"> | ||
Submit | ||
</Button> | ||
</VStack> | ||
</form> | ||
); | ||
}; |
Oops, something went wrong.