Skip to content

Commit

Permalink
#3060-created hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
caiodasilva2005 committed Dec 9, 2024
1 parent 86e033f commit db222ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/frontend/src/apis/organizations.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,15 @@ export const setOrganizationDescription = async (description: string) => {
description
});
};

export const getOrganizationLogo = async () => {
return axios.get<string>(apiUrls.organizationsLogoImage(), {
transformResponse: (data) => JSON.parse(data)
});
};

export const setOrganizationLogo = async (file: File) => {
const formData = new FormData();
formData.append('logo', file);
return axios.post(apiUrls.organizationsSetLogoImage(), formData);
};
26 changes: 25 additions & 1 deletion src/frontend/src/hooks/organizations.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { useContext, useState } from 'react';
import { OrganizationContext } from '../app/AppOrganizationContext';
import { useMutation, useQuery, useQueryClient } from 'react-query';
import { Organization, Project } from 'shared';
import { getFeaturedProjects, getCurrentOrganization, setOrganizationDescription } from '../apis/organizations.api';
import {
getFeaturedProjects,
getCurrentOrganization,
setOrganizationDescription,
getOrganizationLogo,
setOrganizationLogo
} from '../apis/organizations.api';
import { downloadGoogleImage } from '../apis/finance.api';

interface OrganizationProvider {
organizationId: string;
Expand Down Expand Up @@ -64,3 +71,20 @@ export const useSetOrganizationDescription = () => {
}
);
};

export const useSetOrganizationLogo = () => {
return useMutation<Organization, Error, File>(['reimbursement-requsts', 'edit'], async (file: File) => {
const { data } = await setOrganizationLogo(file);
return data;
});
};

export const useOrganizationLogo = () => {
return useQuery<string, Error>(['organizations', 'logo'], async () => {
const { data: fileId } = await getOrganizationLogo();

const imageBlob = await downloadGoogleImage(fileId);

return URL.createObjectURL(imageBlob);
});
};
4 changes: 4 additions & 0 deletions src/frontend/src/utils/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ const organizationsUsefulLinks = () => `${organizations()}/useful-links`;
const organizationsSetUsefulLinks = () => `${organizationsUsefulLinks()}/set`;
const organizationsSetDescription = () => `${organizations()}/description/set`;
const organizationsFeaturedProjects = () => `${organizations()}/featured-projects`;
const organizationsLogoImage = () => `${organizations()}/logo`;
const organizationsSetLogoImage = () => `${organizations()}/logo/update`;

/******************* Car Endpoints ********************/
const cars = () => `${API_URL}/cars`;
Expand Down Expand Up @@ -336,6 +338,8 @@ export const apiUrls = {
organizationsSetUsefulLinks,
organizationsFeaturedProjects,
organizationsSetDescription,
organizationsLogoImage,
organizationsSetLogoImage,

cars,
carsCreate,
Expand Down

0 comments on commit db222ba

Please sign in to comment.