Skip to content

Commit

Permalink
feat: 投稿画面を結合
Browse files Browse the repository at this point in the history
  • Loading branch information
SatooRu65536 committed Nov 5, 2024
1 parent 357a99f commit d074afe
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
44 changes: 41 additions & 3 deletions src/app/(use-header)/post/_components/logic/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import { useAtomValue } from 'jotai';
import { useState } from 'react';
import Page from '../view/Page';
import { authAtom } from '@/stores/authAtom';
import { langAtom } from '@/stores/langAtom';
import { type TablesInsert } from '@/types/supabase';
import insertPost from '@/utils/insertPost';

type Post = Partial<TablesInsert<'post'>>;

Expand All @@ -13,6 +15,7 @@ export default function PostPageLogic() {
const [tagString, setTagString] = useState<string>('');
const [languageString, setLanguageString] = useState<string>('');

const auth = useAtomValue(authAtom);
const languages = useAtomValue(langAtom);

function setCode(code: string) {
Expand Down Expand Up @@ -48,12 +51,47 @@ export default function PostPageLogic() {
// 投稿処理
function submit() {
const tags = changeToArray(tagString);
if (languageString === '') {
const languageId = changeToId(languageString);

if (auth === undefined) {
alert('ログインしてください');
return;
}

if (languageId === undefined) {
alert('言語を選択してください');
return;
}
const languageId = changeToId(languageString);
console.log(post, tags, languageId); // eslint-disable-line no-console

if (post.code === undefined) {
alert('コードを入力してください');
return;
}

if (post.description === undefined) {
alert('説明を入力してください');
return;
}

if (post.title === undefined) {
alert('タイトルを入力してください');
return;
}

const p: Omit<TablesInsert<'post'>, 'id' | 'created_at' | 'deleted_at'> = {
code: post.code,
description: post.description,
lang_id: languageId,
title: post.title,
user_uid: auth.uid,
};
void (async () => {
const res = await insertPost(p, tags);

if (res === undefined) {
alert('投稿に失敗しました');
}
})();
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
}

textarea {
padding: 10px;
width: 60vw;
height: 200px;
resize: vertical; // 縦のみ可変
Expand Down
12 changes: 9 additions & 3 deletions src/app/_components/LoginProvider/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@
grid-column: 1 / -1;

button[type='submit'] {
padding: 8px 10px;
border: solid 1px $primary-color;
border-radius: 4px;
padding: 5px 8px;
border: solid 1.5px;
border-radius: 10px;
background-color: $background-color;
cursor: pointer;
transition: all 0.3s;

&:hover {
background-color: color-mix(in srgb, $background-color, $on-background-color 10%);
}
}
}
}
Expand Down
32 changes: 26 additions & 6 deletions src/app/_components/LoginProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type ChangeEvent, type FormEvent, type ReactElement, useState, useSyncE
import styles from './index.module.scss';
import { authAtom } from '@/stores/authAtom';
import { supabase } from '@/utils/supabase/client';
import upsertUser from '@/utils/upsertUser';

interface Props {
children: ReactElement;
Expand All @@ -31,12 +32,18 @@ export default function LoginProvider({ children }: Props) {

function LoginPage() {
const setAuth = useSetAtom(authAtom);
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

const onChangeName = (e: ChangeEvent<HTMLInputElement>) => {
setName(e.target.value);
};

const onChangeEmail = (e: ChangeEvent<HTMLInputElement>) => {
setEmail(e.target.value);
};

const onChangePassword = (e: ChangeEvent<HTMLInputElement>) => {
setPassword(e.target.value);
};
Expand All @@ -45,13 +52,21 @@ function LoginPage() {
void (async () => {
e.preventDefault();

const { data, error } = await supabase.auth.signUp({ email, password });
if (error != null) throw new Error(error.message);
if (data.user?.id == null) throw new Error('No user id');
try {
const { data, error } = await supabase.auth.signInWithPassword({ email, password });
if (error != null) throw new Error(error.message);
if (data.user?.id == null) throw new Error('No user id');

// eslint-disable-next-line no-console
console.log(data);
setAuth({ email, password, uid: data.user?.id });
// eslint-disable-next-line no-console
console.log(data);
await upsertUser(name, data.user.id);
setAuth({ email, password, uid: data.user.id });
} catch (_) {
const { data, error } = await supabase.auth.signUp({ email, password });
if (error != null) throw new Error(error.message);
if (data.user?.id == null) throw new Error('No user id');
setAuth({ email, password, uid: data.user.id });
}
})();
};

Expand All @@ -60,6 +75,11 @@ function LoginPage() {
<h1>Login / Signin</h1>

<form onSubmit={onSubmit}>
<label htmlFor="email">
<span>Name</span>
<input id="name" onChange={onChangeName} type="text" value={name} />
</label>

<label htmlFor="email">
<span>Email</span>
<input id="email" onChange={onChangeEmail} type="email" value={email} />
Expand Down

0 comments on commit d074afe

Please sign in to comment.