Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api types #50

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 0 additions & 134 deletions src/api/endpoints.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/api/endpoints/authFactor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
} from "../../utils/api"
import type { AuthFactor } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const AUTH_FACTOR_TAG: TagTypes = "AuthFactor"

export type ListAuthFactorsResult = ListResult<AuthFactor, "type">
export type ListAuthFactorsArg = ListArg

export default function getReadAuthFactorEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
listAuthFactors: build.query<ListAuthFactorsResult, ListAuthFactorsArg>({
query: search => ({
url: buildUrl(urls.authFactor.list, { search }),
method: "GET",
}),
providesTags: tagData(AUTH_FACTOR_TAG),
}),
}
}
47 changes: 47 additions & 0 deletions src/api/endpoints/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import getReadAuthFactorEndpoints, {
type ListAuthFactorsArg,
type ListAuthFactorsResult,
AUTH_FACTOR_TAG,
} from "./authFactor"
import getReadClassEndpoints, {
type ListClassesArg,
type ListClassesResult,
type RetrieveClassArg,
type RetrieveClassResult,
CLASS_TAG,
} from "./klass"
import getReadSchoolEndpoints, {
type RetrieveSchoolArg,
type RetrieveSchoolResult,
SCHOOL_TAG,
} from "./school"
import getReadUserEndpoints, {
type ListUsersArg,
type ListUsersResult,
type RetrieveUserArg,
type RetrieveUserResult,
USER_TAG,
} from "./user"

export {
AUTH_FACTOR_TAG,
CLASS_TAG,
getReadAuthFactorEndpoints,
getReadClassEndpoints,
getReadSchoolEndpoints,
getReadUserEndpoints,
SCHOOL_TAG,
USER_TAG,
type ListAuthFactorsArg,
type ListAuthFactorsResult,
type ListClassesArg,
type ListClassesResult,
type ListUsersArg,
type ListUsersResult,
type RetrieveClassArg,
type RetrieveClassResult,
type RetrieveSchoolArg,
type RetrieveSchoolResult,
type RetrieveUserArg,
type RetrieveUserResult,
}
56 changes: 56 additions & 0 deletions src/api/endpoints/klass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { Class } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const CLASS_TAG: TagTypes = "Class"

export type RetrieveClassResult = RetrieveResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>
export type RetrieveClassArg = RetrieveArg<Class>

export type ListClassesResult = ListResult<
Class,
| "name"
| "read_classmates_data"
| "receive_requests_until"
| "school"
| "teacher"
>
export type ListClassesArg = ListArg

export default function getReadClassEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveClass: build.query<RetrieveClassResult, RetrieveClassArg>({
query: id => ({
url: buildUrl(urls.class.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(CLASS_TAG),
}),
listClasses: build.query<ListClassesResult, ListClassesArg>({
query: search => ({
url: buildUrl(urls.class.list, { search }),
method: "GET",
}),
providesTags: tagData(CLASS_TAG),
}),
}
}
33 changes: 33 additions & 0 deletions src/api/endpoints/school.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { School } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const SCHOOL_TAG: TagTypes = "School"

export type RetrieveSchoolResult = RetrieveResult<
School,
"name" | "country" | "uk_county"
>
export type RetrieveSchoolArg = RetrieveArg<School>

export default function getReadSchoolEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveSchool: build.query<RetrieveSchoolResult, RetrieveSchoolArg>({
query: id => ({
url: buildUrl(urls.school.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(SCHOOL_TAG),
}),
}
}
62 changes: 62 additions & 0 deletions src/api/endpoints/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { type EndpointBuilder } from "@reduxjs/toolkit/query/react"

import {
buildUrl,
tagData,
type ListArg,
type ListResult,
type RetrieveArg,
type RetrieveResult,
} from "../../utils/api"
import type { User } from "../models"
import { type TagTypes } from "../tagTypes"
import urls from "../urls"

export const USER_TAG: TagTypes = "User"

export type RetrieveUserResult = RetrieveResult<
User,
| "first_name"
| "last_name"
| "email"
| "is_active"
| "date_joined"
| "requesting_to_join_class"
| "student"
| "teacher"
>
export type RetrieveUserArg = RetrieveArg<User>

export type ListUsersResult = ListResult<
User,
| "first_name"
| "last_name"
| "email"
| "is_active"
| "date_joined"
| "requesting_to_join_class"
| "student"
| "teacher"
>
export type ListUsersArg = ListArg<{ students_in_class: string }>

export default function getReadUserEndpoints(
build: EndpointBuilder<any, any, any>,
) {
return {
retrieveUser: build.query<RetrieveUserResult, RetrieveUserArg>({
query: id => ({
url: buildUrl(urls.user.detail, { url: { id } }),
method: "GET",
}),
providesTags: tagData(USER_TAG),
}),
listUsers: build.query<ListUsersResult, ListUsersArg>({
query: search => ({
url: buildUrl(urls.user.list, { search }),
method: "GET",
}),
providesTags: tagData(USER_TAG),
}),
}
}
Loading