Skip to content

Commit

Permalink
add contributor routes
Browse files Browse the repository at this point in the history
  • Loading branch information
SalmanAsh committed Aug 21, 2024
1 parent 7f24f04 commit c227e3e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 64 deletions.
65 changes: 1 addition & 64 deletions src/api/contributor.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {
type CreateArg,
type CreateResult,
type DestroyArg,
type DestroyResult,
type ListArg,
type ListResult,
type Model,
type RetrieveArg,
type RetrieveResult,
type UpdateArg,
type UpdateResult,
buildUrl,
modelUrls,
tagData,
Expand All @@ -25,7 +19,7 @@ export type Contributor = Model<
location: string
html_url: string
avatar_url: string
last_login: string
last_login: Date
}
>

Expand All @@ -43,28 +37,6 @@ export type ListContributorsResult = ListResult<
>
export type ListContributorsArg = ListArg

export type CreateContributorResult = CreateResult<
Contributor,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>
export type CreateContributorArg = CreateArg<
Contributor,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>

export type UpdateContributorResult = UpdateResult<
Contributor,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>
export type UpdateContributorArg = UpdateArg<
Contributor,
never,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>

export type DestroyContributorResult = DestroyResult
export type DestroyContributorArg = DestroyArg<Contributor>

const contributorApi = api.injectEndpoints({
endpoints: build => ({
retrieveContributor: build.query<
Expand All @@ -84,38 +56,6 @@ const contributorApi = api.injectEndpoints({
}),
providesTags: tagData("Contributor", { includeListTag: true }),
}),
createContributor: build.mutation<
CreateContributorResult,
CreateContributorArg
>({
query: body => ({
url: contributorUrls.list,
method: "POST",
body,
}),
invalidatesTags: tagData("Contributor", { includeListTag: true }),
}),
updateContributor: build.mutation<
UpdateContributorResult,
UpdateContributorArg
>({
query: ({ id, ...body }) => ({
url: buildUrl(contributorUrls.detail, { url: { id } }),
method: "PATCH",
body,
}),
invalidatesTags: tagData("Contributor"),
}),
destroyContributor: build.mutation<
DestroyContributorResult,
DestroyContributorArg
>({
query: id => ({
url: buildUrl(contributorUrls.detail, { url: { id } }),
method: "DELETE",
}),
invalidatesTags: tagData("Contributor", { includeListTag: true }),
}),
}),
})

Expand All @@ -125,7 +65,4 @@ export const {
useLazyRetrieveContributorQuery,
useListContributorsQuery,
useLazyListContributorsQuery,
useCreateContributorMutation,
useUpdateContributorMutation,
useDestroyContributorMutation,
} = contributorApi
55 changes: 55 additions & 0 deletions src/pages/contributorDetail/ContributorDetail.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import * as forms from "codeforlife/components/form"
import * as pages from "codeforlife/components/page"
import * as yup from "yup"
import { Typography } from "@mui/material"
// eslint-disable-next-line sort-imports
import { type FC } from "react"
import { Link } from "codeforlife/components/router"
import { handleQueryState } from "codeforlife/utils/api"
import { useParamsRequired } from "codeforlife/hooks"
// eslint-disable-next-line sort-imports
import { useLazyRetrieveContributorQuery } from "../../api/contributor"
// eslint-disable-next-line sort-imports
import { paths } from "../../routes"

export interface ContributorDetailProps {}

const ContributorDetail: FC<ContributorDetailProps> = () => {
const [retrieveContributor, retrieveContributorResult] =
useLazyRetrieveContributorQuery()

return useParamsRequired({
shape: { id: yup.number().required().min(1) },
children: () =>
handleQueryState(retrieveContributorResult, contributor => (
<pages.Page>
<pages.Section>
<Typography variant="h1">Update fruit</Typography>
<forms.Form
initialValues={contributor}
onSubmit={alert}
></forms.Form>
</pages.Section>
<pages.Section>
<Link className="back-to" to={paths.contributors._}>
Contributor list
</Link>
</pages.Section>
</pages.Page>
)),
onValidationSuccess: params => {
void retrieveContributor(params.id, true)
},
onValidationError: navigate => {
navigate(paths.contributors._, {
state: {
notifications: [
{ props: { error: true, children: "Failed to get params" } },
],
},
})
},
})
}

export default ContributorDetail
43 changes: 43 additions & 0 deletions src/pages/contributorList/ContributorList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as pages from "codeforlife/components/page"
import { Stack, Typography } from "@mui/material"
import { type FC } from "react"
import { LinkIconButton } from "codeforlife/components/router"
import { TablePagination } from "codeforlife/components"
import { generatePath } from "react-router"
import { paths } from "../../routes"
import { useLazyListContributorsQuery } from "../../api/contributor"

export interface ContributorListProps {}

const ContributorList: FC<ContributorListProps> = () => {
return (
<pages.Page>
<pages.Section>
<Typography fontWeight="bold">Helllllloooo </Typography>
<TablePagination
useLazyListQuery={useLazyListContributorsQuery}
preferCacheValue
>
{contributors =>
contributors.map(contributor => (
<Stack direction="row" key={contributor.id} gap={5}>
<Typography fontWeight="bold">{contributor.id}</Typography>
<Typography>
{contributor.name} ({contributor.email})
</Typography>

<LinkIconButton
to={generatePath(paths.contributors.id._, {
id: contributor.id,
})}
></LinkIconButton>
</Stack>
))
}
</TablePagination>
</pages.Section>
</pages.Page>
)
}

export default ContributorList
14 changes: 14 additions & 0 deletions src/routes/contributor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Route } from "react-router-dom"

import ContributorDetail from "../pages/contributorDetail/ContributorDetail"
import ContributorList from "../pages/contributorList/ContributorList"
import paths from "./paths"

const fruit = (
<>
<Route path={paths.contributors.id._} element={<ContributorDetail />} />
<Route path={paths.contributors._} element={<ContributorList />} />
</>
)

export default fruit
2 changes: 2 additions & 0 deletions src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contributor from "./contributor"
import fruit from "./fruit"
import general from "./general"
import paths from "./paths"
Expand All @@ -6,6 +7,7 @@ const routes = (
<>
{general}
{fruit}
{contributor}
</>
)

Expand Down

0 comments on commit c227e3e

Please sign in to comment.