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

update query form for new schema #32

Merged
merged 3 commits into from
Mar 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
17 changes: 13 additions & 4 deletions app/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ if (process.env.NEXT_PUBLIC_MESHDB_URL === undefined) {
throw new Error('Expected API url environment variable');
}

//if (process.env.MESHDB_TOKEN === undefined) {
// throw new Error('Expected API token environment variable');
//}

const API_BASE = new URL(process.env.NEXT_PUBLIC_MESHDB_URL as string + "/api/v1/");

export const JoinFormInput = z.object({
Expand Down Expand Up @@ -54,7 +58,11 @@ export const QueryFormInput = z.object({
})
export type QueryFormInput = z.infer<typeof QueryFormInput>

export const QueryFormResponse = z.array(z.object({
export const QueryFormResponse = z.object({
count: z.number(),
next: z.string().nullable(),
previous: z.string().nullable(),
results: z.array(z.object({
install_number: z.number(),
street_address: z.string().nullable(),
unit: z.string(),
Expand All @@ -67,8 +75,9 @@ export const QueryFormResponse = z.array(z.object({
additional_email_addresses: z.array(z.string()).nullable(),
notes: z.string(),
network_number: z.number().nullable(),
install_status: z.string(),
}))
status: z.string(),
}))
})
export type QueryFormResponse = z.infer<typeof QueryFormResponse>

const get = async <S extends z.Schema>(url: string, schema: S, auth?: string, nextOptions?: NextFetchRequestConfig): Promise<ReturnType<S['parse']>> => {
Expand Down Expand Up @@ -102,4 +111,4 @@ const post = async <S extends z.Schema>(url: string, schema: S, input: unknown,
export const submitJoinForm = (input: JoinFormInput) => post(`/api/v1/join/`, JoinFormResponse, JoinFormInput.parse(input))
export const submitNNAssignForm = (input: NNAssignFormInput) => post(`/api/v1/nn-assign/`, NNAssignFormResponse, NNAssignFormInput.parse(input))

export const submitQueryForm = (route: string, input_type: string, input: string, password: string) => get(`/api/v1/query/${route}/?${input_type}=${input}&password=${password}`, QueryFormResponse)
export const submitQueryForm = (route: string, input_type: string, input: string, password: string) => get(`/api/v1/query/${route}/?${input_type}=${input}`, QueryFormResponse, password)
5 changes: 3 additions & 2 deletions components/QueryForm/QueryForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,16 @@ export function QueryForm() {
queryForm.data,
queryForm.password
);
console.log('response is:');
console.log(resp);
if (resp.length === 0) {
if (resp.results.length === 0) {
toast.warning('Query returned no results.', {
hideProgressBar: true,
});
setIsLoading(false);
return;
}
setQueryResult(resp);
setQueryResult(resp.results);
toast.success('Success!', {
hideProgressBar: true,
});
Expand Down
Loading