Skip to content

Commit

Permalink
Endpoint definitions (#8)
Browse files Browse the repository at this point in the history
* add hook for session cookie

* Call contributor endpoint

* add contributor routes

* fix path and delete fruit

* add service name and call backend

* Add contributor endpoint

* add agreement signature endpoints

* Add create endpoint

* add new endpoint definition

* check sign endpoint

* add login endpoint

* check endpoint functionality

* Fix url and test functionality

* Fix type

* Remove pages

* remove unused import
  • Loading branch information
SalmanAsh authored Sep 2, 2024
1 parent 5a2e864 commit 3db39ca
Show file tree
Hide file tree
Showing 19 changed files with 641 additions and 625 deletions.
114 changes: 114 additions & 0 deletions src/api/agreementSignature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import {
type CreateArg,
type CreateResult,
type ListArg,
type ListResult,
type Model,
type RetrieveArg,
type RetrieveResult,
buildUrl,
modelUrls,
tagData,
} from "codeforlife/utils/api"

import api from "."

export type AgreementSignature = Model<
number,
{
contributor: number
agreement_id: string
signed_at: Date
}
>

const agreementSignatureUrls = modelUrls(
"agreement-signatures/",
"agreement-signatures/<id>/",
)

export type RetrieveAgreementSignatureResult = RetrieveResult<
AgreementSignature,
"contributor" | "agreement_id" | "signed_at"
>
export type RetrieveAgreementSignatureArg = RetrieveArg<AgreementSignature>

export type ListAgreementSignaturesResult = ListResult<
AgreementSignature,
"contributor" | "agreement_id" | "signed_at"
>
export type ListAgreementSignaturesArg = ListArg

export type CreateAgreementSignatureResult = CreateResult<
AgreementSignature,
"contributor" | "agreement_id" | "signed_at"
>
export type CreateAgreementSignatureArg = CreateArg<
AgreementSignature,
"contributor" | "agreement_id" | "signed_at"
>

export type CheckSignedAgreementSignatureResult =
| { latest_commit_id: string; status: 404 | 451 }
| { status: 200 }
export type CheckSignedAgreementSignatureArg = { contributor_pk: number }

const agreementSignatureApi = api.injectEndpoints({
endpoints: build => ({
retrieveAgreementSignature: build.query<
RetrieveAgreementSignatureResult,
RetrieveAgreementSignatureArg
>({
query: id => ({
url: buildUrl(agreementSignatureUrls.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData("AgreementSignature"),
}),
listAgreementSignatures: build.query<
ListAgreementSignaturesResult,
ListAgreementSignaturesArg
>({
query: search => ({
url: buildUrl(agreementSignatureUrls.list, { search }),
method: "GET",
}),
providesTags: tagData("AgreementSignature", { includeListTag: true }),
}),
createAgreementSignature: build.mutation<
CreateAgreementSignatureResult,
CreateAgreementSignatureArg
>({
query: body => ({
url: agreementSignatureUrls.list,
method: "POST",
body,
}),
invalidatesTags: tagData("AgreementSignature", { includeListTag: true }),
}),
checkSignedAgreementSignature: build.query<
CheckSignedAgreementSignatureResult,
CheckSignedAgreementSignatureArg
>({
query: ({ contributor_pk }) => ({
url: buildUrl(
agreementSignatureUrls.list + `check-signed/${contributor_pk}`,
{
url: { contributor_pk },
},
),
method: "GET",
}),
}),
}),
})

export default agreementSignatureApi
export const {
useRetrieveAgreementSignatureQuery,
useLazyRetrieveAgreementSignatureQuery,
useListAgreementSignaturesQuery,
useLazyListAgreementSignaturesQuery,
useCreateAgreementSignatureMutation,
useCheckSignedAgreementSignatureQuery,
} = agreementSignatureApi
68 changes: 68 additions & 0 deletions src/api/contributor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
type ListArg,
type ListResult,
type Model,
type RetrieveArg,
type RetrieveResult,
buildUrl,
modelUrls,
tagData,
} from "codeforlife/utils/api"

import api from "."

export type Contributor = Model<
number,
{
email: string
name: string
location?: string
html_url: string
avatar_url: string
last_login?: Date
}
>

const contributorUrls = modelUrls("contributors/", "contributors/<id>/")

export type RetrieveContributorResult = RetrieveResult<
Contributor,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>
export type RetrieveContributorArg = RetrieveArg<Contributor>

export type ListContributorsResult = ListResult<
Contributor,
"email" | "name" | "location" | "html_url" | "avatar_url" | "last_login"
>
export type ListContributorsArg = ListArg

const contributorApi = api.injectEndpoints({
endpoints: build => ({
retrieveContributor: build.query<
RetrieveContributorResult,
RetrieveContributorArg
>({
query: id => ({
url: buildUrl(contributorUrls.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData("Contributor"),
}),
listContributors: build.query<ListContributorsResult, ListContributorsArg>({
query: search => ({
url: buildUrl(contributorUrls.list, { search }),
method: "GET",
}),
providesTags: tagData("Contributor", { includeListTag: true }),
}),
}),
})

export default contributorApi
export const {
useRetrieveContributorQuery,
useLazyRetrieveContributorQuery,
useListContributorsQuery,
useLazyListContributorsQuery,
} = contributorApi
109 changes: 0 additions & 109 deletions src/api/fruit.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createApi } from "codeforlife/api"

const api = createApi({
tagTypes: ["Fruit"],
tagTypes: ["Contributor", "AgreementSignature"],
})

export default api
Expand Down
21 changes: 21 additions & 0 deletions src/api/session.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { type SessionMetadata } from "../app/hooks"

export type LoginWithGithubResult = SessionMetadata
export type LoginWithGithubArg = { code: string }

import api from "."

const sessionApi = api.injectEndpoints({
endpoints: build => ({
loginWithGithub: build.mutation<LoginWithGithubResult, LoginWithGithubArg>({
query: body => ({
url: "session/login/",
method: "POST",
body,
}),
}),
}),
})

export const { useLoginWithGithubMutation } = sessionApi
export default sessionApi
3 changes: 0 additions & 3 deletions src/app/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
import * as yup from "yup"

export const fruitNameSchema = yup.string().max(50)
19 changes: 0 additions & 19 deletions src/components/FruitNameField.tsx

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/index.tsx
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
export { default as FruitNameField } from "./FruitNameField"
export * from "./FruitNameField"
Loading

0 comments on commit 3db39ca

Please sign in to comment.