-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
135 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useState } from "preact/hooks"; | ||
import { Topic } from "./model"; | ||
|
||
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL; | ||
|
||
type Props = { | ||
onTopicAdded?: (topic: Topic) => any; | ||
}; | ||
export function TopicForm({ onTopicAdded }: Props) { | ||
const [contentForm, setContentForm] = useState(""); | ||
const [choiceAForm, setChoiceAForm] = useState(""); | ||
const [choiceBForm, setChoiceBForm] = useState(""); | ||
|
||
const addTopic = async (e: Event) => { | ||
e.preventDefault(); | ||
const url = `${API_BASE_URL}/topic`; | ||
const res = await fetch(url, { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
Content: contentForm, | ||
Choices: [choiceAForm, choiceBForm], | ||
}), | ||
}); | ||
setContentForm(""); | ||
setChoiceAForm(""); | ||
setChoiceBForm(""); | ||
|
||
const topic = await res.json(); | ||
onTopicAdded?.(topic); | ||
}; | ||
|
||
return ( | ||
<form class="flex flex-col gap-1"> | ||
<div class="flex gap-1"> | ||
<div class="w-16 text-right text-neutral-600">토픽:</div> | ||
<input | ||
class="outline-none border border-neutral-300 rounded-md shadow-sm px-1" | ||
value={contentForm} | ||
onInput={(e) => setContentForm(e.currentTarget.value)} | ||
/> | ||
</div> | ||
<div class="flex gap-1"> | ||
<div class="w-16 text-right text-neutral-600">선택지A:</div> | ||
<input | ||
class="outline-none border border-neutral-300 rounded-md shadow-sm px-1" | ||
value={choiceAForm} | ||
onInput={(e) => setChoiceAForm(e.currentTarget.value)} | ||
/> | ||
</div> | ||
<div class="flex gap-1"> | ||
<div class="w-16 text-right text-neutral-600">선택지B:</div> | ||
<input | ||
class="outline-none border border-neutral-300 rounded-md shadow-sm px-1" | ||
value={choiceBForm} | ||
onInput={(e) => setChoiceBForm(e.currentTarget.value)} | ||
/> | ||
</div> | ||
<button | ||
class="rounded-md shadow-md bg-green-300 p-1 mt-4 font-bold text-sm text-neutral-600 transition-all hover:scale-[1.03]" | ||
onClick={addTopic} | ||
> | ||
추가하기 | ||
</button> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { useEffect, useMemo, useState } from "preact/hooks"; | ||
|
||
export function useLocalStorageState<T>(key: string, defaultValue: T): [T, (v: T) => any] { | ||
const [_value, _setValue] = useState<T>(); | ||
|
||
useEffect(() => { | ||
const v = localStorage.getItem(key); | ||
_setValue(v !== null ? JSON.parse(v) : defaultValue); | ||
}, [key]); | ||
|
||
const setValue = useMemo(() => { | ||
return (v: T) => { | ||
_setValue(v); | ||
localStorage.setItem(key, JSON.stringify(v)); | ||
}; | ||
}, [key]); | ||
|
||
return [_value, setValue]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters