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: Portal frontend 50 #56

Merged
merged 6 commits into from
Sep 10, 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
56 changes: 30 additions & 26 deletions src/api/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import type { CountryIsoCodes, UkCounties } from "../utils/general"
// User Models
// -----------------------------------------------------------------------------

type _UserTeacher<T extends Teacher> = Omit<T, "user">
type _UserStudent<S extends Student> = Omit<S, "user" | "auto_gen_password">

export type User = Model<
number,
{
Expand All @@ -16,49 +19,49 @@ export type User = Model<
is_staff: boolean
is_active: boolean
date_joined: Date
requesting_to_join_class?: string
teacher?: Teacher
student?: Student
requesting_to_join_class?: Class["id"]
teacher?: _UserTeacher<Teacher>
student?: _UserStudent<Student>
}
>

export type TeacherUser<Fields = User> = Fields & {
email: string
last_name: string
teacher: Teacher
student: undefined
teacher: _UserTeacher<Teacher>
student?: undefined
}

export type SchoolTeacherUser<Fields = User> = TeacherUser<Fields> & {
teacher: SchoolTeacher
teacher: _UserTeacher<SchoolTeacher>
}

export type AdminSchoolTeacherUser<Fields = User> =
SchoolTeacherUser<Fields> & {
teacher: AdminSchoolTeacher
teacher: _UserTeacher<AdminSchoolTeacher>
}

export type NonAdminSchoolTeacherUser<Fields = User> =
SchoolTeacherUser<Fields> & {
teacher: NonAdminSchoolTeacher
teacher: _UserTeacher<NonAdminSchoolTeacher>
}

export type NonSchoolTeacherUser<Fields = User> = TeacherUser<Fields> & {
teacher: NonSchoolTeacher
teacher: _UserTeacher<NonSchoolTeacher>
}

export type StudentUser<Fields = User> = Fields & {
email: undefined
last_name: undefined
teacher: undefined
student: Student
email?: undefined
last_name?: undefined
teacher?: undefined
student: _UserStudent<Student>
}

export type IndependentUser<Fields = User> = Fields & {
email: string
last_name: string
teacher: undefined
student: undefined
teacher?: undefined
student?: undefined
}

// -----------------------------------------------------------------------------
Expand All @@ -68,14 +71,14 @@ export type IndependentUser<Fields = User> = Fields & {
export type Teacher = Model<
number,
{
user: number
school?: number
user: User["id"]
school?: School["id"]
is_admin: boolean
}
>

export type SchoolTeacher<Fields = Teacher> = Fields & {
school: number
school: School["id"]
}

export type AdminSchoolTeacher<Fields = Teacher> = SchoolTeacher<Fields> & {
Expand All @@ -87,7 +90,7 @@ export type NonAdminSchoolTeacher<Fields = Teacher> = SchoolTeacher<Fields> & {
}

export type NonSchoolTeacher<Fields = Teacher> = Fields & {
school: undefined
school?: undefined
}

// -----------------------------------------------------------------------------
Expand All @@ -97,9 +100,10 @@ export type NonSchoolTeacher<Fields = Teacher> = Fields & {
export type Student = Model<
number,
{
user: number
school: number
klass: string
user: User["id"]
school: School["id"]
klass: Class["id"]
auto_gen_password: string
}
>

Expand All @@ -116,8 +120,8 @@ export type Class = Model<
string,
{
name: string
teacher: number
school: number
teacher: Teacher["id"]
school: School["id"]
read_classmates_data: boolean
receive_requests_until?: Date
}
Expand All @@ -126,15 +130,15 @@ export type Class = Model<
export type AuthFactor = Model<
number,
{
user: number
user: User["id"]
type: "otp"
}
>

export type OtpBypassToken = Model<
number,
{
user: number
user: User["id"]
token: string
}
>
4 changes: 2 additions & 2 deletions src/components/TablePagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from "react"

import { type Pagination, usePagination } from "../hooks/api"
import { type ListArg, type ListResult, handleQueryState } from "../utils/api"
import { type ListArg, type ListResult, handleResultState } from "../utils/api"

export type TablePaginationProps<
QueryArg extends ListArg,
Expand Down Expand Up @@ -91,7 +91,7 @@ const TablePagination = <

return (
<Stack {...stackProps}>
{handleQueryState(result, ({ data }) =>
{handleResultState(result, ({ data }) =>
children(data, {
limit,
page,
Expand Down
32 changes: 14 additions & 18 deletions src/hooks/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,23 @@ import {
type TryValidateSyncRT,
} from "../utils/schema"

export type Navigate<State extends Record<string, any> = Record<string, any>> =
{
(
to: To,
options?: Omit<NavigateOptions, "state"> & {
state?: State & Partial<PageState>
next?: boolean
},
): void
(delta: number): void
}
export type Navigate = {
<State extends Record<string, any> = Record<string, any>>(
to: To,
options?: Omit<NavigateOptions, "state"> & {
state?: State & Partial<PageState>
next?: boolean
},
): void
(delta: number): void
}

export function useNavigate<
State extends Record<string, any> = Record<string, any>,
>(): Navigate<State> {
export function useNavigate(): Navigate {
const navigate = _useNavigate()
const searchParams = useSearchParams()

return (
toOrDelta,
toOrDelta: To | number,
options: (NavigateOptions & { next?: boolean }) | undefined = undefined,
) => {
if (typeof toOrDelta === "number") navigate(toOrDelta)
Expand Down Expand Up @@ -128,7 +125,6 @@ export function useParams<
export function useParamsRequired<
OnErrorRT extends TryValidateSyncOnErrorRT<ObjectSchemaFromShape<Shape>>,
Shape extends ObjectShape = {},
State extends Record<string, any> = Record<string, any>,
>({
shape,
children,
Expand All @@ -142,7 +138,7 @@ export function useParamsRequired<
TryValidateSyncRT<ObjectSchemaFromShape<Shape>, OnErrorRT>
>,
) => ReactNode
onValidationError: (navigate: Navigate<State>) => void
onValidationError: (navigate: Navigate) => void
onValidationSuccess?: (
params: NonNullable<
TryValidateSyncRT<ObjectSchemaFromShape<Shape>, OnErrorRT>
Expand All @@ -154,7 +150,7 @@ export function useParamsRequired<
>
}) {
const params = useParams(shape, validateOptions)
const navigate = useNavigate<State>()
const navigate = useNavigate()

useEffect(
() => {
Expand Down
6 changes: 4 additions & 2 deletions src/utils/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
FetchBaseQueryError,
TypedUseQueryHookResult,
TypedUseQueryStateResult,
TypedUseMutationResult,
} from "@reduxjs/toolkit/query/react"
import { type ReactNode } from "react"

Expand Down Expand Up @@ -292,10 +293,11 @@ export type HandleQueryStateOptions = Partial<{
error: ReactNode
}>

export function handleQueryState<QueryArg, ResultType>(
export function handleResultState<QueryArg, ResultType>(
result:
| TypedUseQueryHookResult<ResultType, QueryArg, any>
| TypedUseQueryStateResult<ResultType, QueryArg, any>,
| TypedUseQueryStateResult<ResultType, QueryArg, any>
| TypedUseMutationResult<ResultType, QueryArg, any>,
children: (data: NonNullable<ResultType>) => ReactNode,
options?: HandleQueryStateOptions,
): ReactNode {
Expand Down