Skip to content

Commit

Permalink
refactor(types): replace 'any' with generic types in PUT authenticate…
Browse files Browse the repository at this point in the history
… request
  • Loading branch information
sunilsabatp committed Jan 17, 2025
1 parent 07d99a3 commit f91b377
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 29 deletions.
15 changes: 10 additions & 5 deletions src/features/user/ManageProjects/components/ProjectMedia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ import { useUserProps } from '../../../common/Layout/UserPropsContext';
import { ProjectCreationTabs } from '..';
import { useTenant } from '../../../common/Layout/TenantContext';

type SubmitData = {
type PostSubmitData = {
imageFile: string;
description: string | null;
isDefault: boolean;
};

type VideoSubmitData = {
videoUrl: string;
};

export default function ProjectMedia({
handleBack,
token,
Expand Down Expand Up @@ -90,14 +94,14 @@ export default function ProjectMedia({
const uploadPhotos = async (image: string) => {
setIsUploadingData(true);

const submitData: SubmitData = {
const submitData: PostSubmitData = {
imageFile: image,
description: null,
isDefault: false,
};

try {
const res = await postAuthenticatedRequest<UploadImage, SubmitData>({
const res = await postAuthenticatedRequest<UploadImage, PostSubmitData>({
tenant: tenantConfig?.id,
url: `/app/projects/${projectGUID}/images`,
data: submitData,
Expand Down Expand Up @@ -171,13 +175,14 @@ export default function ProjectMedia({
const onSubmit = async (data: { youtubeURL: string }) => {
// Add isDirty test here
setIsUploadingData(true);
const submitData = {
const submitData: VideoSubmitData = {
videoUrl: data.youtubeURL,
};

try {
const res = await putAuthenticatedRequest<
ProfileProjectTrees | ProfileProjectConservation
ProfileProjectTrees | ProfileProjectConservation,
VideoSubmitData
>({
tenant: tenantConfig?.id,
url: `/app/projects/${projectGUID}`,
Expand Down
16 changes: 8 additions & 8 deletions src/features/user/ManageProjects/components/ProjectSites.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ const Map = dynamic(() => import('./MapComponent'), {
loading: () => <p></p>,
});

type SubmitData = {
name: string;
geometry: GeoJson<Geometry, GeoJsonProperties>;
status: string;
};

function EditSite({
openModal,
handleModalClose,
Expand Down Expand Up @@ -102,14 +108,14 @@ function EditSite({
const editProjectSite = async (data: ProjectSitesFormData) => {
if (geoJson && geoJson.features && geoJson.features.length !== 0) {
setIsUploadingData(true);
const submitData = {
const submitData: SubmitData = {
name: siteDetails.name,
geometry: geoJson,
status: data.status,
};

try {
const res = await putAuthenticatedRequest<Site>({
const res = await putAuthenticatedRequest<Site, SubmitData>({
tenant: tenantConfig?.id,
url: `/app/projects/${projectGUID}/sites/${siteGUID}`,
data: submitData,
Expand Down Expand Up @@ -262,12 +268,6 @@ interface ProjectSitesFormData {
status: string;
}

type SubmitData = {
name: string;
geometry: GeoJson<Geometry, GeoJsonProperties>;
status: string;
};

export default function ProjectSites({
handleBack,
token,
Expand Down
12 changes: 10 additions & 2 deletions src/features/user/Profile/ForestProgress/TargetsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ interface TargetsModalProps {
conservTarget: number;
}

type SubmitData = {
targets: {
treesDonated: number;
areaRestored: number;
areaConserved: number;
};
};

const TargetsModal = ({
open,
setOpen,
Expand Down Expand Up @@ -66,7 +74,7 @@ const TargetsModal = ({
const handleTargets = async () => {
setIsTargetModalLoading(true);
if (contextLoaded && token && open && !isTargetModalLoading) {
const bodyToSend = {
const bodyToSend: SubmitData = {
targets: {
treesDonated: isTreesPlantedTargetActive
? treesPlantedTargetLocal
Expand All @@ -80,7 +88,7 @@ const TargetsModal = ({
},
};
try {
const res = await putAuthenticatedRequest<User>({
const res = await putAuthenticatedRequest<User, SubmitData>({
tenant: tenantConfig?.id,
url: `/app/profile`,
data: bodyToSend,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import type { ExtendedCountryCode } from '../../../../common/types/country';
import type { SetState } from '../../../../common/types/common';
import type { Address, APIError } from '@planet-sdk/common';
import type {
Address,
AddressType,
APIError,
CountryCode,
} from '@planet-sdk/common';
import type { FormData } from './AddAddress';
import type { AddressAction } from '../../../../common/types/profile';

Expand All @@ -21,6 +26,11 @@ interface Props {
setAddressAction: SetState<AddressAction | null>;
}

type SubmitData = FormData & {
country: CountryCode | string;
type: AddressType;
};

const EditAddress = ({
setIsModalOpen,
selectedAddressForAction,
Expand Down Expand Up @@ -48,13 +58,13 @@ const EditAddress = ({
async (data: FormData) => {
if (!contextLoaded || !user || !token) return;
setIsLoading(true);
const bodyToSend = {
const bodyToSend: SubmitData = {
...data,
country,
type: selectedAddressForAction?.type,
};
try {
const res = await putAuthenticatedRequest<Address>({
const res = await putAuthenticatedRequest<Address, SubmitData>({
tenant: tenantConfig.id,
url: `/app/addresses/${selectedAddressForAction?.id}`,
data: bodyToSend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ interface Props {
selectedAddressForAction: Address;
}

type SubmitData = {
type: 'other';
};

const UnsetBillingAddress = ({
addressType,
setIsModalOpen,
Expand All @@ -39,11 +43,11 @@ const UnsetBillingAddress = ({
const unsetAddress = async () => {
if (!contextLoaded || !user || !token) return;
setIsLoading(true);
const bodyToSend = {
const bodyToSend: SubmitData = {
type: ADDRESS_TYPE.OTHER,
};
try {
const res = await putAuthenticatedRequest<Address>({
const res = await putAuthenticatedRequest<Address, SubmitData>({
tenant: tenantConfig.id,
url: `/app/addresses/${selectedAddressForAction.id}`,
data: bodyToSend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ interface Props {
setAddressAction: SetState<AddressAction | null>;
}

type SubmitData = {
type: 'primary' | 'mailing';
};

const UpdateAddressType = ({
addressType,
setIsModalOpen,
Expand All @@ -41,11 +45,11 @@ const UpdateAddressType = ({
const updateAddress = async (addressType: 'primary' | 'mailing') => {
if (!contextLoaded || !user || !token) return;
setIsUploadingData(true);
const bodyToSend = {
const bodyToSend: SubmitData = {
type: addressType,
};
try {
const res = await putAuthenticatedRequest<Address>({
const res = await putAuthenticatedRequest<Address, SubmitData>({
tenant: tenantConfig.id,
url: `/app/addresses/${selectedAddressForAction.id}`,
data: bodyToSend,
Expand Down
9 changes: 5 additions & 4 deletions src/features/user/Settings/EditProfile/EditProfileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type ProfileTypeOption = {
value: 'individual' | 'organization' | 'education';
};

type SubmitData = {
imageFile: string | ArrayBuffer | null | undefined;
};
export default function EditProfileForm() {
const [snackbarOpen, setSnackbarOpen] = useState(false);
const { setErrors } = React.useContext(ErrorHandlingContext);
Expand Down Expand Up @@ -160,11 +163,9 @@ export default function EditProfileForm() {
reset();
}, [type]);

const handleUserProfileImage = async (bodyToSend: {
imageFile: string | ArrayBuffer | null | undefined;
}) => {
const handleUserProfileImage = async (bodyToSend: SubmitData) => {
try {
const res = await putAuthenticatedRequest<User>({
const res = await putAuthenticatedRequest<User, SubmitData>({
tenant: tenantConfig?.id,
url: `/app/profile`,
data: bodyToSend,
Expand Down
2 changes: 1 addition & 1 deletion src/features/user/Settings/ImpersonateUser/SupportPin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const SupportPin = () => {
token,
logoutUser,
});
if (response) {
if (response && user) {
const updateUserData = { ...user };
updateUserData['supportPin'] = response?.supportPin;
setUser(updateUserData);
Expand Down
7 changes: 5 additions & 2 deletions src/features/user/Widget/EmbedModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ interface Props {
setEmbedModalOpen: Function;
}

type SubmitData = {
isPrivate: boolean;
};
const Alert = styled(MuiAlert)(({ theme }) => {
return {
backgroundColor: theme.palette.primary.main,
Expand Down Expand Up @@ -56,12 +59,12 @@ export default function EmbedModal({

const saveProfile = async () => {
setIsUploadingData(true);
const bodyToSend = {
const bodyToSend: SubmitData = {
isPrivate: false,
};
if (contextLoaded && token) {
try {
const res = await putAuthenticatedRequest<User>({
const res = await putAuthenticatedRequest<User, SubmitData>({
tenant: tenantConfig?.id,
url: `/app/profile`,
data: bodyToSend,
Expand Down

0 comments on commit f91b377

Please sign in to comment.