base con autenticacion, registro, modulo encuestas
This commit is contained in:
36
apps/web/feactures/location/actions/actions.ts
Normal file
36
apps/web/feactures/location/actions/actions.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
'use server';
|
||||
import { safeFetchApi } from '@/lib/fetch.api';
|
||||
import {responseStates, responseMunicipalities, responseParishes} from '../schemas/users';
|
||||
|
||||
// import { auth } from '@/lib/auth';
|
||||
|
||||
|
||||
export const getStateAction = async () => {
|
||||
const [error, response] = await safeFetchApi(
|
||||
responseStates,
|
||||
`/location/state/`,
|
||||
'GET'
|
||||
);
|
||||
if (error) throw new Error(error.message);
|
||||
return response;
|
||||
};
|
||||
|
||||
export const getMunicipalityAction = async (id : number) => {
|
||||
const [error, response] = await safeFetchApi(
|
||||
responseMunicipalities,
|
||||
`/location/municipality/${id}`,
|
||||
'GET'
|
||||
);
|
||||
if (error) throw new Error(error.message);
|
||||
return response;
|
||||
};
|
||||
|
||||
export const getParishAction = async (id : number) => {
|
||||
const [error, response] = await safeFetchApi(
|
||||
responseParishes,
|
||||
`/location/parish/${id}`,
|
||||
'GET'
|
||||
);
|
||||
if (error) throw new Error(error.message);
|
||||
return response;
|
||||
};
|
||||
16
apps/web/feactures/location/hooks/use-query-location.ts
Normal file
16
apps/web/feactures/location/hooks/use-query-location.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
'use client'
|
||||
import { useSafeQuery } from "@/hooks/use-safe-query";
|
||||
import { getStateAction, getMunicipalityAction, getParishAction } from "../actions/actions";
|
||||
|
||||
// Hook for users
|
||||
export function useStateQuery() {
|
||||
return useSafeQuery(['state'], () => getStateAction())
|
||||
}
|
||||
|
||||
export function useMunicipalityQuery( stateId : number ) {
|
||||
return useSafeQuery(['municipality', stateId], () => getMunicipalityAction(stateId))
|
||||
}
|
||||
|
||||
export function useParishQuery(municipalityId : number) {
|
||||
return useSafeQuery(['parish', municipalityId], () => getParishAction(municipalityId))
|
||||
}
|
||||
94
apps/web/feactures/location/schemas/users.ts
Normal file
94
apps/web/feactures/location/schemas/users.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export type SurveyTable = z.infer<typeof user>;
|
||||
export type CreateUser = z.infer<typeof createUser>;
|
||||
export type UpdateUser = z.infer<typeof updateUser>;
|
||||
|
||||
export const user = z.object({
|
||||
id: z.number().optional(),
|
||||
username: z.string(),
|
||||
email: z.string(),
|
||||
fullname: z.string(),
|
||||
phone: z.string().nullable(),
|
||||
isActive: z.boolean(),
|
||||
role: z.string(),
|
||||
state: z.string().optional().nullable(),
|
||||
municipality: z.string().optional().nullable(),
|
||||
parish: z.string().optional().nullable(),
|
||||
});
|
||||
|
||||
export const createUser = z.object({
|
||||
id: z.number().optional(),
|
||||
username: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }),
|
||||
password: z.string().min(8, { message: "Debe de tener 8 o más caracteres" }),
|
||||
email: z.string().email({ message: "Correo no válido" }),
|
||||
fullname: z.string(),
|
||||
phone: z.string(),
|
||||
confirmPassword: z.string(),
|
||||
role: z.number()
|
||||
})
|
||||
.refine((data) => data.password === data.confirmPassword, {
|
||||
message: 'La contraseña no coincide',
|
||||
path: ['confirmPassword'],
|
||||
})
|
||||
|
||||
export const updateUser = z.object({
|
||||
id: z.number(),
|
||||
username: z.string().min(5, { message: "Debe de tener 5 o más caracteres" }).or(z.literal('')),
|
||||
password: z.string().min(6, { message: "Debe de tener 6 o más caracteres" }).or(z.literal('')),
|
||||
email: z.string().email({ message: "Correo no válido" }).or(z.literal('')),
|
||||
fullname: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
role: z.number().optional(),
|
||||
isActive: z.boolean().optional(),
|
||||
state: z.number().optional().nullable(),
|
||||
municipality: z.number().optional().nullable(),
|
||||
parish: z.number().optional().nullable(),
|
||||
})
|
||||
|
||||
export const surveysApiResponseSchema = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(user),
|
||||
meta: z.object({
|
||||
page: z.number(),
|
||||
limit: z.number(),
|
||||
totalCount: z.number(),
|
||||
totalPages: z.number(),
|
||||
hasNextPage: z.boolean(),
|
||||
hasPreviousPage: z.boolean(),
|
||||
nextPage: z.number().nullable(),
|
||||
previousPage: z.number().nullable(),
|
||||
}),
|
||||
})
|
||||
|
||||
export const states = z.object({
|
||||
id: z.number(),
|
||||
name: z.string()
|
||||
})
|
||||
|
||||
export const municipalities = z.object({
|
||||
id: z.number(),
|
||||
stateId: z.number(),
|
||||
name: z.string()
|
||||
})
|
||||
|
||||
export const parishes = z.object({
|
||||
id: z.number(),
|
||||
municipalityId: z.number(),
|
||||
name: z.string()
|
||||
})
|
||||
|
||||
export const responseStates = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(states),
|
||||
})
|
||||
|
||||
export const responseMunicipalities = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(municipalities),
|
||||
})
|
||||
|
||||
export const responseParishes = z.object({
|
||||
message: z.string(),
|
||||
data: z.array(parishes),
|
||||
})
|
||||
Reference in New Issue
Block a user