Skip to content

Commit

Permalink
add possibility to create new entries to local db
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusLevang committed Jul 31, 2024
1 parent 113942b commit 41ff59b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/app/[id]/edit/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ export default function Page({params}: { params: { id: string } }) {
}, [params]);

useEffect(() => {
void getLocalTitle(params.id).then((data: title) => setLocalTitle(data));
void getLocalTitle(params.id)
.then((data: title) => setLocalTitle(data))
.catch((e: string) => {
if (e.toLowerCase().includes('not found')) {
setLocalTitle({
id: +params.id,
vendor: '',
/* eslint-disable @typescript-eslint/naming-convention */
contact_name: '',
contact_email: '',
contact_phone: '',
release_pattern: [0, 0, 0, 0, 0, 0, 0],
/* eslint-enable @typescript-eslint/naming-convention */
} as title);
} else {
alert('Noe gikk galt ved henting av tittelinformasjon. Kontakt tekst-teamet om problemet vedvarer.');
}
});
}, [params]);

function showSavedMessage() {
Expand Down
34 changes: 32 additions & 2 deletions src/app/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ export default function Page({params}: { params: { id: string } }) {
const [catalogTitle, setCatalogTitle] = useState<CatalogTitle>();
const [localTitle, setLocalTitle] = useState<title>();
const router = useRouter();
const [localTitleNotFound, setLocalTitleNotFound] = useState<boolean>(false);

useEffect(() => {
void fetchNewspaperTitleFromCatalog(params.id).then((data: CatalogTitle) => setCatalogTitle(data));
}, [params]);

useEffect(() => {
void getLocalTitle(params.id).then((data: title) => setLocalTitle(data));
void getLocalTitle(params.id)
.then((data: title) => {
setLocalTitle(data);
setLocalTitleNotFound(false);
})
.catch((e: string) => {
setLocalTitle(undefined);
if (e.toLowerCase().includes('not found')) {
setLocalTitleNotFound(true);
} else {
alert('Kunne ikke se etter kontakt- og utgivelsesinformasjon. Kontakt tekst-teamet om problemet vedvarer.');
}
});
}, [params]);

return (
Expand Down Expand Up @@ -67,7 +80,24 @@ export default function Page({params}: { params: { id: string } }) {
</button>

</div>
) : (<p> Henter lokal informasjon... </p>)
) : (
<>
{ !localTitleNotFound && <p> Henter kontakt- og utgivelsesinformasjon... </p> }
</>
)
}

{localTitleNotFound &&
<>
<p className="mt-10">Fant ikke kontakt- og utgivelsesinformasjon for denne tittelen. Ønsker du å legge til? </p>
<button
type="button"
className="bg-green-400 hover:bg-green-600 text-white font-bold py-2 px-4 rounded mt-5"
onClick={() => router.push(`/${params.id}/edit`)}
>
Legg til informasjon
</button>
</>
}
</div>
);
Expand Down
4 changes: 3 additions & 1 deletion src/pages/api/title/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ export default async function handle(
// GET api/title/[id]
async function handleGET(titleId: string, res: NextApiResponse) {
const id: number = +titleId;
const title = await prisma.title.findUnique({
const title = await prisma.title.findUniqueOrThrow({
where: { id }
}).catch(e => {
return res.status(404).json({error: `Failed to find title: ${e}`});
});

return res.status(200).json(title);
Expand Down
10 changes: 9 additions & 1 deletion src/services/local.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { title } from '@prisma/client';

export async function getLocalTitle(id: string): Promise<title> {
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_PATH}/api/title/${id}`);
return await response.json() as Promise<title>;

switch (response.status) {
case 200:
return await response.json() as Promise<title>;
case 404:
return Promise.reject('Title not found');
default:
return Promise.reject('Failed to fetch title');
}
}

export async function postLocalTitle(localTitle: title): Promise<Response> {
Expand Down

0 comments on commit 41ff59b

Please sign in to comment.