mirror of
https://github.com/traberph/short.git
synced 2026-11-22 22:55:12 +00:00
basic url short functionality
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
"use client"
|
||||
|
||||
import { useFormStatus } from "react-dom";
|
||||
|
||||
export default function SubmitButton({children}: {children?: React.ReactNode}) {
|
||||
const { pending } = useFormStatus();
|
||||
return (
|
||||
<button disabled={pending} type="submit">
|
||||
{children?children:"Submit"}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import React from 'react';
|
||||
import type { URL as PrismaURL } from '@prisma/client';
|
||||
import DeleteUrlForm from './forms/deleteUrlForm';
|
||||
import Image from 'next/image';
|
||||
import Link from 'next/link';
|
||||
|
||||
export default function UrlComponent({ url }: { url: PrismaURL }) {
|
||||
|
||||
return (
|
||||
<Link href={`/~/dash/${url.short_code}`}>
|
||||
<div className="w-full my-3 flex items-center hover:bg-slate-600 rounded p-1">
|
||||
<div className="flex-shrink-0 h-8 w-8 flex items-center justify-center mr-4">
|
||||
<Image
|
||||
loading='lazy'
|
||||
width={100}
|
||||
height={100}
|
||||
src={`https://www.google.com/s2/favicons?domain=${url.original_url}`}
|
||||
alt="favicon"
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-bold text-lg text-white">{url.short_code}</p>
|
||||
<p className="text-sm text-gray-400">{url.original_url}</p>
|
||||
</div>
|
||||
<div className="ml-auto">
|
||||
<DeleteUrlForm url_id={url.url_id} />
|
||||
</div>
|
||||
</div>
|
||||
</Link>
|
||||
|
||||
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client"
|
||||
|
||||
import { createUrl } from "@/actions/url_actions";
|
||||
import { useActionState } from "react";
|
||||
import SubmitButton from "../SubmitButton";
|
||||
import { error } from "console";
|
||||
|
||||
export default function CreateUrlForm() {
|
||||
const initialState = {
|
||||
message: "",
|
||||
error: {}
|
||||
};
|
||||
const [state, formAction] = useActionState(createUrl, initialState);
|
||||
|
||||
console.log(state);
|
||||
|
||||
return (
|
||||
<form action={formAction}>
|
||||
<div className="form-group">
|
||||
<label htmlFor='original_url'>Original URL</label>
|
||||
<input type='text' name='original_url' id='original_url' className={state.error?.original_url?"border-red-400":""}/>
|
||||
<p className="form-label-error">{ state.error?.original_url }</p>
|
||||
</div>
|
||||
<div className="form-group">
|
||||
<label htmlFor='short_code'>Short Code</label>
|
||||
<input type='text' name='short_code' id='short_code' className={state.error?.short_code?"border-red-400":""}/>
|
||||
<p className="form-label-error">{ state.error?.short_code }</p>
|
||||
</div>
|
||||
<p>{state.message}</p>
|
||||
<SubmitButton/>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
"use client"
|
||||
|
||||
import { deleteUrl } from "@/actions/url_actions";
|
||||
import { useActionState } from "react";
|
||||
import SubmitButton from "../SubmitButton";
|
||||
|
||||
export default function DeleteUrlForm({url_id}: {url_id: number}) {
|
||||
|
||||
const initialState = {
|
||||
message: "",
|
||||
error: {}
|
||||
};
|
||||
const [state, formAction] = useActionState(deleteUrl, initialState);
|
||||
|
||||
return (
|
||||
<form action={formAction}>
|
||||
<input type="hidden" name="url_id" value={url_id}/>
|
||||
<SubmitButton>Delete</SubmitButton>
|
||||
</form>
|
||||
);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user