|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import styles from "./UploadPackageForm.module.css"; |
| 4 | +import { |
| 5 | + ApiForm, |
| 6 | + uploadPackageFormSchema, |
| 7 | +} from "@thunderstore/ts-api-react-forms"; |
| 8 | +import { |
| 9 | + FormSubmitButton, |
| 10 | + FormMultiSelectSearch, |
| 11 | + FormSelectSearch, |
| 12 | + useFormToaster, |
| 13 | + CreateTeamForm, |
| 14 | + FormSwitch, |
| 15 | +} from "@thunderstore/cyberstorm-forms"; |
| 16 | +import { TextInput, Dialog, Button } from "@thunderstore/cyberstorm"; |
| 17 | +import { SettingItem } from "@thunderstore/cyberstorm/src/components/SettingItem/SettingItem"; |
| 18 | +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; |
| 19 | +import { faPlus } from "@fortawesome/free-solid-svg-icons"; |
| 20 | +import { packageUpload } from "@thunderstore/thunderstore-api"; |
| 21 | + |
| 22 | +const options = [ |
| 23 | + { label: "Asd", value: "asd" }, |
| 24 | + { label: "Asd2", value: "asd2" }, |
| 25 | + { label: "Asd3", value: "asd3" }, |
| 26 | + { label: "Asd4", value: "asd4" }, |
| 27 | + { label: "Asd5", value: "asd5" }, |
| 28 | + { label: "Asd6", value: "asd6" }, |
| 29 | + { label: "Asd7", value: "asd7" }, |
| 30 | + { label: "Asd8", value: "asd8" }, |
| 31 | + { label: "Asd9", value: "asd9" }, |
| 32 | + { label: "Asd10", value: "asd10" }, |
| 33 | + { label: "Asd11", value: "asd11" }, |
| 34 | + { label: "Asd12", value: "asd12" }, |
| 35 | + { label: "Asd13", value: "asd13" }, |
| 36 | + { label: "Asd14", value: "asd14" }, |
| 37 | +]; |
| 38 | + |
| 39 | +interface UploadPackageFormProps { |
| 40 | + teams: { |
| 41 | + name: string; |
| 42 | + }[]; |
| 43 | +} |
| 44 | + |
| 45 | +export function UploadPackageForm(props: UploadPackageFormProps) { |
| 46 | + const toaster = useFormToaster({ |
| 47 | + successMessage: "Package submitted", |
| 48 | + }); |
| 49 | + |
| 50 | + // Parse categories to the right format for form |
| 51 | + function communityCategoriesParse( |
| 52 | + selected: { |
| 53 | + label: string; |
| 54 | + value: string; |
| 55 | + }[], |
| 56 | + onChange: (selected: { [key: string]: string[] }) => void |
| 57 | + ) { |
| 58 | + const communityCategories: { [key: string]: string[] } = {}; |
| 59 | + selected.map((x) => (communityCategories[x.value] = [x.value])); |
| 60 | + onChange(communityCategories); |
| 61 | + } |
| 62 | + |
| 63 | + // Parse communities to the right format for form |
| 64 | + function communitiesParse( |
| 65 | + selected: { |
| 66 | + label: string; |
| 67 | + value: string; |
| 68 | + }[], |
| 69 | + onChange: (selected: string[]) => void |
| 70 | + ) { |
| 71 | + onChange(selected.map((x) => x.value)); |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <ApiForm |
| 76 | + {...toaster} |
| 77 | + schema={uploadPackageFormSchema} |
| 78 | + endpoint={packageUpload} |
| 79 | + formProps={{ className: styles.root }} |
| 80 | + > |
| 81 | + <div className={styles.root}> |
| 82 | + <SettingItem |
| 83 | + title="Upload file" |
| 84 | + description="Upload your package as a ZIP file." |
| 85 | + content={<TextInput />} |
| 86 | + /> |
| 87 | + <div className={styles.line} /> |
| 88 | + <SettingItem |
| 89 | + title="Team" |
| 90 | + description="No teams available?" |
| 91 | + additionalLeftColumnContent={ |
| 92 | + <Dialog.Root |
| 93 | + title="Create Team" |
| 94 | + trigger={ |
| 95 | + <Button.Root colorScheme="primary" paddingSize="large"> |
| 96 | + <Button.ButtonLabel>Create team</Button.ButtonLabel> |
| 97 | + <Button.ButtonIcon> |
| 98 | + <FontAwesomeIcon icon={faPlus} /> |
| 99 | + </Button.ButtonIcon> |
| 100 | + </Button.Root> |
| 101 | + } |
| 102 | + > |
| 103 | + <CreateTeamForm /> |
| 104 | + </Dialog.Root> |
| 105 | + } |
| 106 | + content={ |
| 107 | + <FormSelectSearch |
| 108 | + name="team" |
| 109 | + schema={uploadPackageFormSchema} |
| 110 | + options={props.teams.map((t) => t.name)} |
| 111 | + placeholder="Choose a team..." |
| 112 | + /> |
| 113 | + } |
| 114 | + /> |
| 115 | + <SettingItem |
| 116 | + title="Communities" |
| 117 | + description="Select communities you want your package to be listed under. Current community is selected by default." |
| 118 | + content={ |
| 119 | + <FormMultiSelectSearch |
| 120 | + name="communities" |
| 121 | + schema={uploadPackageFormSchema} |
| 122 | + options={options} |
| 123 | + placeholder="Choose community..." |
| 124 | + onChangeParse={communitiesParse} |
| 125 | + /> |
| 126 | + } |
| 127 | + /> |
| 128 | + <SettingItem |
| 129 | + title="Categories" |
| 130 | + description="Select descriptive categories to help people discover your package." |
| 131 | + content={ |
| 132 | + <FormMultiSelectSearch |
| 133 | + name="community_categories" |
| 134 | + schema={uploadPackageFormSchema} |
| 135 | + options={options} |
| 136 | + placeholder="Choose categories..." |
| 137 | + onChangeParse={communityCategoriesParse} |
| 138 | + /> |
| 139 | + } |
| 140 | + /> |
| 141 | + <div className={styles.line} /> |
| 142 | + <SettingItem |
| 143 | + title="Contains NSFW content" |
| 144 | + description='A "NSFW" -tag will be applied to your package.' |
| 145 | + content={ |
| 146 | + <FormSwitch |
| 147 | + schema={uploadPackageFormSchema} |
| 148 | + name="has_nsfw_content" |
| 149 | + /> |
| 150 | + } |
| 151 | + /> |
| 152 | + <div className={styles.line} /> |
| 153 | + <SettingItem |
| 154 | + title="Submit" |
| 155 | + description='Double-check your selections and hit "Submit" when you are ready!' |
| 156 | + content={ |
| 157 | + <div className={styles.submit}> |
| 158 | + <Button.Root colorScheme="danger"> |
| 159 | + <Button.ButtonLabel>Reset</Button.ButtonLabel> |
| 160 | + </Button.Root> |
| 161 | + <FormSubmitButton text="Submit" /> |
| 162 | + </div> |
| 163 | + } |
| 164 | + /> |
| 165 | + </div> |
| 166 | + </ApiForm> |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +UploadPackageForm.displayName = "UploadPackageForm"; |
0 commit comments