-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from UNIwise/MP-2460-API-Projects-Hooks-and-mocks
Mp 2460 api projects and versions hooks and mocks
- Loading branch information
Showing
17 changed files
with
337 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { GetVersionsResponse } from '../../interfaces/versions'; | ||
import client from '../client'; | ||
import '../mocks/useDeleteVersion.mock'; | ||
|
||
const deleteVersion = async (projectId?: string, version?: number) => { | ||
const response = await client.delete<GetVersionsResponse>( | ||
`/api/v1/projects/${projectId}/versions/${version}`, | ||
); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useDeleteVersion = (projectId?: string, version?: number) => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
onSuccess: async () => { | ||
await queryClient.invalidateQueries({ | ||
queryKey: ['api', 'projects', projectId, 'versions'], | ||
exact: true, | ||
}, | ||
{ throwOnError: true }); | ||
}, | ||
mutationFn: () => deleteVersion(projectId, version), | ||
}); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { Project } from '../../interfaces/projects'; | ||
import client from '../client'; | ||
import '../mocks/useGetProject.mock'; | ||
|
||
const getProject = async (projectId?: string) => { | ||
const response = await client.get<Project>( | ||
`/api/v1/projects/${projectId}`, | ||
); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useGetProject = (projectId?: string) => { | ||
return useQuery({ | ||
queryKey: ['api', 'projects', projectId], | ||
queryFn: () => getProject(projectId), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { GetProjectsResponse } from '../../interfaces/projects'; | ||
import client from '../client'; | ||
import '../mocks/useGetProjects.mock'; | ||
|
||
const getProjects = async () => { | ||
const response = await client.get<GetProjectsResponse>( | ||
`/api/v1/projects`, | ||
); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useGetProjects = () => { | ||
return useQuery({ | ||
queryKey: ['api', 'projects'], | ||
queryFn: () => getProjects(), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { GetVersionsResponse } from '../../interfaces/versions'; | ||
import client from '../client'; | ||
import '../mocks/useGetVersions.mock'; | ||
|
||
const getVersions = async (projectId?: string) => { | ||
const response = await client.get<GetVersionsResponse>( | ||
`/api/v1/projects/${projectId}/versions`, | ||
); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const useGetVersions = (projectId?: string) => { | ||
return useQuery({ | ||
queryKey: ['api', 'projects', projectId, 'versions'], | ||
queryFn: () => getVersions(projectId), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query'; | ||
import { GetVersionsResponse } from '../../interfaces/versions'; | ||
import client from '../client'; | ||
import '../mocks/useGetVersions.mock'; | ||
|
||
const postVersion = async (projectId: number) => { | ||
const response = await client.post<GetVersionsResponse>( | ||
`/api/v1/projects/${projectId}/versions`, | ||
); | ||
|
||
return response.data; | ||
}; | ||
|
||
export const usePostVersion = (projectId: number) => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useMutation({ | ||
onSuccess: async () => { | ||
await queryClient.invalidateQueries({ | ||
queryKey: ['api', 'projects', projectId, 'versions'], | ||
exact: true, | ||
}, | ||
{ throwOnError: true }); | ||
}, | ||
mutationFn: () => postVersion(projectId), | ||
}); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { mock } from "../client"; | ||
import { mockedVersionsResponse } from "./useGetVersions.mock"; | ||
|
||
mock.onDelete(/^\/api\/v1\/projects\/\d+\/versions\/\d+$/).reply((req) => { | ||
const versionId = parseInt(req.url!.split("/")[6]); | ||
|
||
const versionIndex = mockedVersionsResponse.versions.findIndex( | ||
(version) => version.id === versionId | ||
); | ||
|
||
mockedVersionsResponse.versions.splice(versionIndex, 1); | ||
|
||
return [204]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { mock } from "../client"; | ||
import { mockedProjectsResponse } from "./useGetProjects.mock"; | ||
|
||
mock.onGet(/^\/api\/v1\/projects\/\d+$/).reply((req) => { | ||
const projectId = parseInt(req.url!.split("/")[4]); | ||
|
||
const project = mockedProjectsResponse.projects.find( | ||
(project) => project.id === projectId | ||
); | ||
|
||
return [ | ||
200, | ||
project | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Version } from "../../interfaces/versions"; | ||
import { mock } from "../client"; | ||
import { mockedVersionsResponse } from "./useGetVersions.mock"; | ||
|
||
mock.onPost(/^\/api\/v1\/projects\/\d+\/versions$/).reply((req) => { | ||
const request = JSON.parse(req.data); | ||
|
||
const newVersion: Version = { | ||
id: Math.floor(Math.random() * 1000), | ||
name: request.name, | ||
createdAt: new Date().toISOString(), | ||
}; | ||
|
||
mockedVersionsResponse.versions.push(newVersion); | ||
|
||
return [201, newVersion]; | ||
}); | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export interface getProjectsResponse { | ||
export interface GetProjectsResponse { | ||
projects: Project[]; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 48 additions & 4 deletions
52
dashboard/src/views/Projects/Versions/components/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,49 @@ | ||
const AddNewVersionModal = () => { | ||
return <div>AddNewVersion</div>; | ||
}; | ||
import { Delete } from "@mui/icons-material"; | ||
import { Button, Typography } from "@mui/joy"; | ||
import { FC } from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import { useDeleteVersion } from "../../../../api/hooks/useDeleteVersion"; | ||
|
||
|
||
interface VersionTableRowProps { | ||
versionId: number; | ||
versionName: string; | ||
createdAt: string; | ||
} | ||
|
||
export const VersionTableRow: FC<VersionTableRowProps> = ({ | ||
versionId, | ||
versionName, | ||
createdAt, | ||
}) => { | ||
const formatIsoDateToLocaleString = (isoDate: string) => { | ||
return new Date(isoDate).toLocaleString(); | ||
}; | ||
|
||
const { projectId } = useParams(); | ||
const { mutate: deleteVersion } = useDeleteVersion(projectId, versionId); | ||
|
||
export default AddNewVersionModal; | ||
const createdAtDate = formatIsoDateToLocaleString(createdAt); | ||
|
||
return ( | ||
<tr> | ||
<td style={{ paddingLeft: "1.5rem" }}> | ||
<Typography level="body-xs">{versionName}</Typography> | ||
</td> | ||
|
||
<td style={{ paddingLeft: "0.5rem" }}> | ||
<Typography level="body-xs">{createdAtDate}</Typography> | ||
</td> | ||
|
||
<td | ||
style={{ | ||
textAlign: "end", | ||
padding: "0.5rem 5rem", | ||
verticalAlign: "center", | ||
}} | ||
> | ||
<Button sx={{ mb: "0.5rem", backgroundColor: '#0078ff' }} onClick={() => deleteVersion()}>Delete <Delete /></Button> | ||
</td> | ||
</tr> | ||
); | ||
}; |
Oops, something went wrong.