Skip to content

Instantly share code, notes, and snippets.

import { contactList } from '@/actions/actions';
import Add from '@/components/add';
import Pagination from '@/components/pagination';
import Table from '@/components/table';
export default async function Home({ params, searchParams }) {
const { contacts, pages } = await contactList(searchParams);
return (
<div className="container mx-auto mb-16">
'use client';
import { useRouter, useSearchParams } from 'next/navigation';
import React, { useState } from 'react';
function Pagination({ pages }) {
const router = useRouter();
const searchParams = useSearchParams();
const totalPages = [...Array(pages)].map((_, i) => i + 1);
const [currPage, setCurrPage] = useState(searchParams.get('page') || 1);
export async function contactList(params) {
try {
const limit = 5;
const page = params.page || 1;
const skip = limit * (page - 1);
const contacts = await Contact.find()
.sort({ createdAt: -1 })
.sort({ updatedAt: -1 })
.limit(limit)
import { contactDelete } from '@/actions/actions';
import React, { startTransition, useState } from 'react';
function Delete({ dopen, setDopen, currId, setCurrId }) {
const [error, setError] = useState();
async function doDelete(id) {
const result = await contactDelete(id);
if (result.error) {
setError(result.error);
}
export async function contactDelete(id) {
try {
const contact = await Contact.findByIdAndDelete(id, { new: true });
revalidatePath('/');
const newContact = { ...contact._doc, _id: contact._doc._id.toString() };
return newContact;
} catch (e) {
return { error: e.message };
}
}
'use client';
import React from 'react';
function View({ vopen, setVopen, contact }) {
return (
<>
<dialog id="add_modal" className="modal" open={vopen}>
<div className="modal-box w-2/3 max-w-3xl ">
<h3 className="font-bold text-lg">Contact Detail</h3>
<div className="text-xs mb-2">ID: {contact?._id}</div>
'use client';
import { contactAdd } from '@/actions/actions';
import React, { useState } from 'react';
import { useForm } from 'react-hook-form';
import { useTransition } from 'react';
import { zodResolver } from '@hookform/resolvers/zod';
import zSchema from '@/models/zodSchema';
import { setDriver } from 'mongoose';
export async function contactAdd(data) {
try {
const contact = new Contact(data);
contact.save();
const newContact = { ...contact._doc, _id: contact._doc._id.toString() };
revalidatePath('/');
return newContact;
} catch (e) {
return { error: e.message };
}
import { contactList } from '@/actions/actions';
import Add from '@/components/add';
import Table from '@/components/table';
export default async function Home() {
const { contacts } = await contactList();
return (
<div className="container mx-auto mb-16">
<h3 className="text-3xl mb-4">MY AMAZING CONTACTS</h3>
@popeating
popeating / edit.js
Last active November 3, 2023 12:53
'use client';
import { contactUpdate } from '@/actions/actions';
import React, { useEffect, useState, useTransition } from 'react';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import zSchema from '@/models/zodSchema';
function Edit({ eopen, setEopen, contact }) {
const [isPending, startTransition] = useTransition();
const [error, setError] = useState();