Skip to content

Commit

Permalink
create place
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix-Asante committed Dec 13, 2023
1 parent f5ea031 commit 09650f4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
3 changes: 1 addition & 2 deletions src/actions/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ export async function addNewPlace(body: FormData) {
throw new Error(getErrorMessage(error));
}
}
export async function deletePlace(placeId: string, redirectHome = false) {
export async function deletePlace(placeId: string) {
try {
const endpoint = apiConfig.places.delete(placeId);
await apiHandler({
endpoint,
method: "DELETE",
});
revalidateTag(Tags.places);
redirectHome && redirect(DASHBOARD_PATHS.places.root);
} catch (error) {
console.log(error);
throw new Error(getErrorMessage(error));
Expand Down
25 changes: 12 additions & 13 deletions src/app/(dashboard)/places/sections/new/CreatePlaceForm.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
"use client";
import { addNewPlace } from "@/actions/place";
import FileUpload from "@/components/shared/input/FileUpload";
import TextField from "@/components/shared/input/TextField";
import { useReactHookForm } from "@/hooks/useReactHookForm";
import { Category } from "@/types/category";
import { Button, Select, SelectItem } from "@nextui-org/react";
import React from "react";
import { Controller } from "react-hook-form";
import CreatePlaceDetails from "./CreatePlaceDetails";
import HStack from "@/components/shared/layout/HStack";
import { DASHBOARD_PATHS } from "@/config/routes";
import { CreatePlaceField, createPlaceSchema } from "@/rules/validations/place";
import FileUpload from "@/components/shared/input/FileUpload";
import { useReactHookForm } from "@/hooks/useReactHookForm";
import { useServerAction } from "@/hooks/useServerAction";
import { addNewPlace } from "@/actions/place";
import { toast } from "sonner";
import { CreatePlaceField, createPlaceSchema } from "@/rules/validations/place";
import { Category } from "@/types/category";
import { getErrorMessage } from "@/utils/helpers";
import { Button } from "@nextui-org/react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import CreatePlaceDetails from "./CreatePlaceDetails";

interface Props {
categories: Category[];
Expand All @@ -23,14 +22,14 @@ export default function CreatePlaceForm({ categories }: Props) {
control,
handleSubmit,
formState: { errors, isValid },
reset,
register,
} = useReactHookForm<CreatePlaceField>(createPlaceSchema);

const [createPlace, { loading }] = useServerAction<any, typeof addNewPlace>(
addNewPlace,
);

const router = useRouter();
const onSubmit = async (data: CreatePlaceField) => {
try {
const {
Expand Down Expand Up @@ -60,7 +59,7 @@ export default function CreatePlaceForm({ categories }: Props) {

await createPlace(formData);
toast.success("New place created");
reset();
router.push(DASHBOARD_PATHS.places.root);
} catch (error) {
toast.error(getErrorMessage(error));
}
Expand All @@ -71,7 +70,7 @@ export default function CreatePlaceForm({ categories }: Props) {
<h4 className='text-black text-xl'>Add New Place</h4>
<HStack className='justify-end'>
<Button
href={DASHBOARD_PATHS.places.root}
onClick={() => router.push(DASHBOARD_PATHS.places.root)}
variant='bordered'
color='success'
radius='none'
Expand Down
2 changes: 1 addition & 1 deletion src/rules/validations/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const createPlaceSchema = z.object({
address: z.string(),
longitude: z.string(),
latitude: z.string(),
website: z.string().url(),
website: z.string().url()?.optional(),
averagePrice: z.string().transform((val) => +val),
deliveryFee: z
.string()
Expand Down

0 comments on commit 09650f4

Please sign in to comment.