|
1 | | -import { Stack, Grid } from '@mui/material'; |
| 1 | +import { Box, FormControl, Grid, Stack, Typography } from '@mui/material'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import { useForm } from 'react-hook-form'; |
| 4 | +import { yupResolver } from '@hookform/resolvers/yup'; |
| 5 | +import * as yup from 'yup'; |
2 | 6 | import EditDescription from './EditDescription'; |
3 | 7 | import EditFeaturedProjects from './EditFeaturedProjects'; |
4 | 8 | import EditLogo from './EditLogo'; |
| 9 | +import { |
| 10 | + useCurrentOrganization, |
| 11 | + useSetOrganizationPlatformLogoImage, |
| 12 | + useSetPlatformDescription |
| 13 | +} from '../../../hooks/organizations.hooks'; |
| 14 | +import { useGetImageUrl } from '../../../hooks/onboarding.hook'; |
| 15 | +import LoadingIndicator from '../../../components/LoadingIndicator'; |
| 16 | +import NERUploadButton from '../../../components/NERUploadButton'; |
| 17 | +import NERSuccessButton from '../../../components/NERSuccessButton'; |
| 18 | +import ReactHookTextField from '../../../components/ReactHookTextField'; |
| 19 | +import { useToast } from '../../../hooks/toasts.hooks'; |
| 20 | +import { MAX_FILE_SIZE } from 'shared'; |
| 21 | +import UsefulLinksTable from '../OnboardingConfig/UsefulLinks/UsefulLinksTable'; |
| 22 | +import LinkTypeTable from '../ProjectsConfig/LinkTypes/LinkTypeTable'; |
| 23 | + |
| 24 | +const platformDescriptionSchema = yup.object().shape({ |
| 25 | + platformDescription: yup.string().required() |
| 26 | +}); |
5 | 27 |
|
6 | 28 | const GuestViewConfig: React.FC = () => { |
| 29 | + const { data: organization } = useCurrentOrganization(); |
| 30 | + const { mutateAsync: setPlatformLogoImage, isLoading: platformLogoLoading } = useSetOrganizationPlatformLogoImage(); |
| 31 | + const { mutateAsync: setPlatformDescriptionMutation, isLoading: platformDescriptionSaving } = useSetPlatformDescription(); |
| 32 | + const { data: platformLogoImageUrl } = useGetImageUrl(organization?.platformLogoImageId ?? null); |
| 33 | + const toast = useToast(); |
| 34 | + |
| 35 | + const [addedPlatformLogo, setAddedPlatformLogo] = useState<File | undefined>(undefined); |
| 36 | + |
| 37 | + const formKey = organization?.organizationId ?? 'loading'; |
| 38 | + |
| 39 | + const { control, handleSubmit, reset } = useForm<{ platformDescription: string }>({ |
| 40 | + resolver: yupResolver(platformDescriptionSchema), |
| 41 | + defaultValues: { platformDescription: organization?.platformDescription ?? '' } |
| 42 | + }); |
| 43 | + |
| 44 | + const onPlatformDescriptionSubmit = async (data: { platformDescription: string }) => { |
| 45 | + try { |
| 46 | + const updated = await setPlatformDescriptionMutation(data.platformDescription); |
| 47 | + reset({ platformDescription: updated.platformDescription }); |
| 48 | + toast.success('Platform description saved.'); |
| 49 | + } catch (e) { |
| 50 | + toast.error(e instanceof Error ? e.message : 'Failed to save platform description'); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + const handlePlatformLogoUpload = async () => { |
| 55 | + if (!addedPlatformLogo) return; |
| 56 | + if (addedPlatformLogo.size >= MAX_FILE_SIZE) { |
| 57 | + toast.error( |
| 58 | + `Error uploading ${addedPlatformLogo.name}; file must be less than ${MAX_FILE_SIZE / 1024 / 1024} MB`, |
| 59 | + 5000 |
| 60 | + ); |
| 61 | + return; |
| 62 | + } |
| 63 | + try { |
| 64 | + await setPlatformLogoImage(addedPlatformLogo); |
| 65 | + toast.success('Platform logo uploaded successfully.'); |
| 66 | + setAddedPlatformLogo(undefined); |
| 67 | + } catch (e) { |
| 68 | + toast.error(e instanceof Error ? e.message : 'Failed to upload image'); |
| 69 | + } |
| 70 | + }; |
| 71 | + |
7 | 72 | return ( |
8 | 73 | <Grid container spacing={2}> |
9 | 74 | <Grid item xs={6}> |
10 | 75 | <Stack spacing={2}> |
11 | 76 | <EditDescription /> |
12 | 77 | <EditFeaturedProjects /> |
| 78 | + <Box> |
| 79 | + <Typography variant="h5" gutterBottom borderBottom={1} color="#ef4345" borderColor="white"> |
| 80 | + Platform Description |
| 81 | + </Typography> |
| 82 | + <form |
| 83 | + id="platform-description-form" |
| 84 | + key={formKey} |
| 85 | + onSubmit={(e) => { |
| 86 | + e.preventDefault(); |
| 87 | + e.stopPropagation(); |
| 88 | + handleSubmit(onPlatformDescriptionSubmit)(e); |
| 89 | + }} |
| 90 | + onKeyPress={(e) => { |
| 91 | + e.key === 'Enter' && e.preventDefault(); |
| 92 | + }} |
| 93 | + > |
| 94 | + <FormControl sx={{ width: '100%' }}> |
| 95 | + <ReactHookTextField |
| 96 | + name="platformDescription" |
| 97 | + control={control} |
| 98 | + multiline |
| 99 | + rows={5} |
| 100 | + fullWidth |
| 101 | + required |
| 102 | + placeholder="Enter platform description for the guest home page..." |
| 103 | + sx={{ mb: 1 }} |
| 104 | + /> |
| 105 | + </FormControl> |
| 106 | + <Box sx={{ display: 'flex', justifyContent: 'end' }}> |
| 107 | + <NERSuccessButton |
| 108 | + type="submit" |
| 109 | + form="platform-description-form" |
| 110 | + variant="contained" |
| 111 | + disabled={platformDescriptionSaving} |
| 112 | + sx={{ mt: 1 }} |
| 113 | + > |
| 114 | + {platformDescriptionSaving ? 'Saving...' : 'Save'} |
| 115 | + </NERSuccessButton> |
| 116 | + </Box> |
| 117 | + </form> |
| 118 | + </Box> |
13 | 119 | </Stack> |
14 | 120 | </Grid> |
15 | 121 | <Grid item xs={6}> |
16 | | - <EditLogo /> |
| 122 | + <Stack spacing={2}> |
| 123 | + <EditLogo /> |
| 124 | + <Box> |
| 125 | + <Typography variant="h5" gutterBottom borderBottom={1} color="#ef4345" borderColor="white"> |
| 126 | + Platform Logo |
| 127 | + </Typography> |
| 128 | + {platformLogoLoading ? ( |
| 129 | + <Box sx={{ height: '200px', display: 'flex', alignItems: 'center' }}> |
| 130 | + <LoadingIndicator /> |
| 131 | + </Box> |
| 132 | + ) : ( |
| 133 | + <Box> |
| 134 | + <NERUploadButton |
| 135 | + dataTypeId="platformLogo" |
| 136 | + handleFileChange={(e) => { |
| 137 | + if (e.target.files?.[0]) setAddedPlatformLogo(e.target.files[0]); |
| 138 | + }} |
| 139 | + onSubmit={handlePlatformLogoUpload} |
| 140 | + addedImage={addedPlatformLogo} |
| 141 | + setAddedImage={setAddedPlatformLogo} |
| 142 | + /> |
| 143 | + {!addedPlatformLogo && platformLogoImageUrl && ( |
| 144 | + <Box |
| 145 | + component="img" |
| 146 | + src={platformLogoImageUrl} |
| 147 | + alt="Platform Logo" |
| 148 | + sx={{ display: 'block', maxWidth: '200px', mb: 1, mt: 1 }} |
| 149 | + /> |
| 150 | + )} |
| 151 | + </Box> |
| 152 | + )} |
| 153 | + </Box> |
| 154 | + </Stack> |
| 155 | + </Grid> |
| 156 | + <Grid item> |
| 157 | + <Typography variant="h5" gutterBottom borderBottom={1} color="#ef4345" borderColor={'white'}> |
| 158 | + Links Config |
| 159 | + </Typography> |
| 160 | + <LinkTypeTable isOnGuestHomePage={true} /> |
| 161 | + </Grid> |
| 162 | + <Grid item> |
| 163 | + <Box |
| 164 | + sx={{ |
| 165 | + backgroundColor: (theme) => theme.palette.background.paper, |
| 166 | + height: '100%', |
| 167 | + borderRadius: '10px', |
| 168 | + padding: '16px', |
| 169 | + width: '100%' |
| 170 | + }} |
| 171 | + > |
| 172 | + <Typography |
| 173 | + variant="h6" |
| 174 | + sx={{ |
| 175 | + color: 'white', |
| 176 | + fontWeight: 'bold', |
| 177 | + marginBottom: '12px' |
| 178 | + }} |
| 179 | + > |
| 180 | + Guest Page Links |
| 181 | + </Typography> |
| 182 | + <UsefulLinksTable isOnGuestHomePage={true} /> |
| 183 | + </Box> |
17 | 184 | </Grid> |
18 | 185 | </Grid> |
19 | 186 | ); |
|
0 commit comments