2023-03-22 18:11:54 -05:00
|
|
|
import useCollectionStore from "@/store/collections";
|
2023-02-24 11:32:28 -06:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import CreatableSelect from "react-select/creatable";
|
|
|
|
import { styles } from "./styles";
|
|
|
|
import { Options } from "./types";
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
type Props = {
|
|
|
|
onChange: any;
|
|
|
|
defaultValue:
|
|
|
|
| {
|
|
|
|
label: string;
|
2023-05-27 11:29:39 -05:00
|
|
|
value?: number;
|
2023-03-28 02:31:50 -05:00
|
|
|
}
|
|
|
|
| undefined;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function ({ onChange, defaultValue }: Props) {
|
2023-03-22 18:11:54 -05:00
|
|
|
const { collections } = useCollectionStore();
|
2023-02-24 11:32:28 -06:00
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
const [options, setOptions] = useState<Options[]>([]);
|
|
|
|
|
|
|
|
const collectionId = Number(router.query.id);
|
|
|
|
|
|
|
|
const activeCollection = collections.find((e) => {
|
|
|
|
return e.id === collectionId;
|
|
|
|
});
|
|
|
|
|
2023-03-28 02:31:50 -05:00
|
|
|
if (activeCollection && !defaultValue) {
|
|
|
|
defaultValue = {
|
2023-02-24 22:22:33 -06:00
|
|
|
value: activeCollection?.id,
|
|
|
|
label: activeCollection?.name,
|
|
|
|
};
|
|
|
|
}
|
2023-02-24 11:32:28 -06:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const formatedCollections = collections.map((e) => {
|
2023-03-28 02:31:50 -05:00
|
|
|
return { value: e.id, label: e.name, ownerId: e.ownerId };
|
2023-02-24 11:32:28 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
setOptions(formatedCollections);
|
|
|
|
}, [collections]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CreatableSelect
|
|
|
|
isClearable
|
|
|
|
onChange={onChange}
|
|
|
|
options={options}
|
|
|
|
styles={styles}
|
2023-03-28 02:31:50 -05:00
|
|
|
defaultValue={defaultValue}
|
2023-05-01 15:00:23 -05:00
|
|
|
// menuPosition="fixed"
|
2023-02-24 11:32:28 -06:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|