add icon image selected preview
This commit is contained in:
parent
700f5582dc
commit
5dfb5e5a9a
@ -53,22 +53,30 @@ export type ResourceUpdateInput = RouterInputs["auditoryResource"]["update"];
|
|||||||
*
|
*
|
||||||
* File needs to be path relative to resource_logos/
|
* File needs to be path relative to resource_logos/
|
||||||
*/
|
*/
|
||||||
const SelectImageInput = ({ file }: { file?: string }) => {
|
const SelectImageInput = ({
|
||||||
const { getValues } = useFormContext<ResourceUpdateInput>();
|
file,
|
||||||
|
setIconFile,
|
||||||
|
}: {
|
||||||
|
file?: string;
|
||||||
|
setIconFile: (file: File) => void;
|
||||||
|
}) => {
|
||||||
|
const [previewImg, setPreviewImg] = useState<string | undefined>(
|
||||||
|
`/resource_logos/${file ?? ""}`
|
||||||
|
);
|
||||||
|
|
||||||
const onChange = async (event: ChangeEvent<HTMLInputElement>) => {
|
const onChange = (event: ChangeEvent<HTMLInputElement>) => {
|
||||||
const data = new FormData();
|
|
||||||
if (!event.target.files || !event.target.files[0]) {
|
if (!event.target.files || !event.target.files[0]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resourceId = getValues("id");
|
const file = event.target.files[0];
|
||||||
data.append("photo", event.target.files[0]);
|
const reader = new FileReader();
|
||||||
|
reader.onloadend = () => {
|
||||||
|
setPreviewImg(reader.result as string);
|
||||||
|
};
|
||||||
|
reader.readAsDataURL(file);
|
||||||
|
|
||||||
await fetch(`/api/resources/photo/${resourceId}`, {
|
setIconFile(file);
|
||||||
method: "POST",
|
|
||||||
body: data,
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -79,7 +87,7 @@ const SelectImageInput = ({ file }: { file?: string }) => {
|
|||||||
>
|
>
|
||||||
<Image
|
<Image
|
||||||
className="w-full"
|
className="w-full"
|
||||||
src={`/resource_logos/${file ?? ""}`}
|
src={previewImg ?? ""}
|
||||||
alt={`resource logo`}
|
alt={`resource logo`}
|
||||||
width={512}
|
width={512}
|
||||||
height={512}
|
height={512}
|
||||||
@ -89,13 +97,7 @@ const SelectImageInput = ({ file }: { file?: string }) => {
|
|||||||
</div>
|
</div>
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
onChange={(event) => {
|
onChange={onChange}
|
||||||
onChange(event).catch(() => {
|
|
||||||
throw new Error(
|
|
||||||
"Unexpected error occured when selecting new file."
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
accept="image/*"
|
accept="image/*"
|
||||||
id="resource-image-file"
|
id="resource-image-file"
|
||||||
type="file"
|
type="file"
|
||||||
@ -333,8 +335,10 @@ const PaymentTypeOption = ({
|
|||||||
*/
|
*/
|
||||||
function ResourceSummarySubForm({
|
function ResourceSummarySubForm({
|
||||||
resource,
|
resource,
|
||||||
|
setIconFile,
|
||||||
}: {
|
}: {
|
||||||
resource?: ResourceUpdateInput;
|
resource?: ResourceUpdateInput;
|
||||||
|
setIconFile: (file: File) => void;
|
||||||
}) {
|
}) {
|
||||||
const { register } = useFormContext<ResourceUpdateInput>();
|
const { register } = useFormContext<ResourceUpdateInput>();
|
||||||
|
|
||||||
@ -342,7 +346,7 @@ function ResourceSummarySubForm({
|
|||||||
<div className="space-y-4 px-4">
|
<div className="space-y-4 px-4">
|
||||||
<div className="flex flex-row space-x-4 sm:mt-4">
|
<div className="flex flex-row space-x-4 sm:mt-4">
|
||||||
<div className="flex w-20 flex-col justify-center space-y-2 sm:w-28">
|
<div className="flex w-20 flex-col justify-center space-y-2 sm:w-28">
|
||||||
<SelectImageInput file={resource?.icon} />
|
<SelectImageInput file={resource?.icon} setIconFile={setIconFile} />
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-col justify-center overflow-hidden rounded-xl border border-neutral-400 bg-white drop-shadow-lg sm:w-[300px] md:w-[400px]">
|
<div className="flex flex-col justify-center overflow-hidden rounded-xl border border-neutral-400 bg-white drop-shadow-lg sm:w-[300px] md:w-[400px]">
|
||||||
<h2 className="border-b border-neutral-300 px-2 text-center font-semibold">
|
<h2 className="border-b border-neutral-300 px-2 text-center font-semibold">
|
||||||
@ -462,10 +466,12 @@ const ResourceDescriptionSubForm = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const ResourceForm = ({
|
const ResourceForm = ({
|
||||||
|
setIconFile,
|
||||||
methods,
|
methods,
|
||||||
resource,
|
resource,
|
||||||
error,
|
error,
|
||||||
}: {
|
}: {
|
||||||
|
setIconFile: (file: File) => void;
|
||||||
resource?: ResourceUpdateInput;
|
resource?: ResourceUpdateInput;
|
||||||
methods: UseFormReturn<ResourceUpdateInput>;
|
methods: UseFormReturn<ResourceUpdateInput>;
|
||||||
error?: string;
|
error?: string;
|
||||||
@ -487,7 +493,10 @@ const ResourceForm = ({
|
|||||||
General
|
General
|
||||||
</h1>
|
</h1>
|
||||||
<div className="justify-left mx-auto flex max-w-lg flex-col space-y-4 pb-5">
|
<div className="justify-left mx-auto flex max-w-lg flex-col space-y-4 pb-5">
|
||||||
<ResourceSummarySubForm resource={resource} />
|
<ResourceSummarySubForm
|
||||||
|
resource={resource}
|
||||||
|
setIconFile={setIconFile}
|
||||||
|
/>
|
||||||
<ResourceDescriptionSubForm />
|
<ResourceDescriptionSubForm />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -46,6 +46,7 @@ const EditResourcePage = (
|
|||||||
props: InferGetServerSidePropsType<typeof getServerSideProps>
|
props: InferGetServerSidePropsType<typeof getServerSideProps>
|
||||||
) => {
|
) => {
|
||||||
const { resource } = props;
|
const { resource } = props;
|
||||||
|
const [updateIconFile, setIconFile] = useState<File | undefined>(undefined);
|
||||||
const [serverError, setServerError] = useState<string | undefined>(undefined);
|
const [serverError, setServerError] = useState<string | undefined>(undefined);
|
||||||
const formMethods = useForm<ResourceUpdateInput>({
|
const formMethods = useForm<ResourceUpdateInput>({
|
||||||
defaultValues: resource as ResourceUpdateInput,
|
defaultValues: resource as ResourceUpdateInput,
|
||||||
@ -59,6 +60,18 @@ const EditResourcePage = (
|
|||||||
});
|
});
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<ResourceUpdateInput> = (data) => {
|
const onSubmit: SubmitHandler<ResourceUpdateInput> = (data) => {
|
||||||
|
if (updateIconFile) {
|
||||||
|
console.log("we need to update the file!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// const data = new FormData();
|
||||||
|
// data.append("photo", event.target.files[0]);
|
||||||
|
|
||||||
|
// await fetch(`/api/resources/photo/${resourceId}`, {
|
||||||
|
// method: "POST",
|
||||||
|
// body: data,
|
||||||
|
// });
|
||||||
|
|
||||||
mutate(data);
|
mutate(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -102,6 +115,7 @@ const EditResourcePage = (
|
|||||||
>
|
>
|
||||||
<main className="mb-12">
|
<main className="mb-12">
|
||||||
<ResourceForm
|
<ResourceForm
|
||||||
|
setIconFile={setIconFile}
|
||||||
methods={formMethods}
|
methods={formMethods}
|
||||||
error={serverError}
|
error={serverError}
|
||||||
resource={resource as ResourceUpdateInput}
|
resource={resource as ResourceUpdateInput}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user