|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import type { InterestSubject } from "common/types"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { enqueueSnackbar } from "notistack"; |
| 6 | +import { useEffect, useState } from "react"; |
| 7 | +import { MdAdd, MdClose } from "react-icons/md"; |
| 8 | +import FullScreenCircularProgress from "~/components/common/FullScreenCircularProgress"; |
| 9 | +import { useAlert } from "~/components/common/alert/AlertProvider"; |
| 10 | +import * as subject from "../../../api/subject"; |
| 11 | +import type { BackProp } from "../common"; |
| 12 | + |
| 13 | +export default function Step5({ back }: BackProp) { |
| 14 | + const { state } = subject.useMyInterests(); |
| 15 | + const data = state.data; |
| 16 | + const error = state.current === "error" ? state.error : null; |
| 17 | + const loading = state.current === "loading"; |
| 18 | + |
| 19 | + const router = useRouter(); |
| 20 | + const { showAlert } = useAlert(); |
| 21 | + |
| 22 | + const [allSubjects, setAllSubjects] = useState<InterestSubject[]>([]); |
| 23 | + const [filteredSubjects, setFilteredSubjects] = useState<InterestSubject[]>( |
| 24 | + [], |
| 25 | + ); |
| 26 | + const [draftSubjects, setDraftSubjects] = useState<InterestSubject[]>( |
| 27 | + data ?? [], |
| 28 | + ); |
| 29 | + const [isOpen, setIsOpen] = useState(false); |
| 30 | + const [newSubjectName, setNewSubjectName] = useState(""); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + getSubjects(); |
| 34 | + }, []); |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + setDraftSubjects(data ?? []); |
| 38 | + }, [data]); |
| 39 | + |
| 40 | + async function getSubjects() { |
| 41 | + const subjects = await subject.getAll(); |
| 42 | + setAllSubjects(subjects); |
| 43 | + setFilteredSubjects(subjects); |
| 44 | + } |
| 45 | + |
| 46 | + async function updateInterests(data: { |
| 47 | + interestSubjects: InterestSubject[]; |
| 48 | + }) { |
| 49 | + const ids = data.interestSubjects.map((d) => d.id); |
| 50 | + const result = await subject.update(ids); |
| 51 | + if (!result.ok) { |
| 52 | + enqueueSnackbar("興味分野の保存に失敗しました", { variant: "error" }); |
| 53 | + } else { |
| 54 | + enqueueSnackbar("興味分野を保存しました", { variant: "success" }); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async function createSubject(name: string) { |
| 59 | + const result = await subject.create(name); |
| 60 | + if (!result.ok) { |
| 61 | + enqueueSnackbar("興味分野の作成に失敗しました", { variant: "error" }); |
| 62 | + } else { |
| 63 | + enqueueSnackbar("興味分野を作成しました", { variant: "success" }); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + function handleBack() { |
| 68 | + // TODO: 差分がないときは確認なしで戻る |
| 69 | + showAlert({ |
| 70 | + AlertMessage: "変更がある場合は、破棄されます。", |
| 71 | + subAlertMessage: "本当にページを移動しますか?", |
| 72 | + yesMessage: "移動", |
| 73 | + clickYes: () => { |
| 74 | + back(); |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return loading ? ( |
| 80 | + <FullScreenCircularProgress /> |
| 81 | + ) : error ? ( |
| 82 | + <p>Error: {error.message}</p> |
| 83 | + ) : !data ? ( |
| 84 | + <p>データがありません。</p> |
| 85 | + ) : ( |
| 86 | + <> |
| 87 | + <div className="h-full overflow-y-scroll"> |
| 88 | + <div className="mx-auto flex h-full max-w-lg flex-col px-4"> |
| 89 | + <div className="flex-1"> |
| 90 | + <div className="flex flex-wrap gap-2 p-2"> |
| 91 | + {draftSubjects.map((subject, index) => ( |
| 92 | + <span |
| 93 | + key={subject.id} |
| 94 | + className="rounded-md bg-[#F7FCFF] px-2 py-1 text-md text-primary" |
| 95 | + > |
| 96 | + #{subject.name} |
| 97 | + <button |
| 98 | + type="button" |
| 99 | + className="btn btn-circle btn-xs ml-1" |
| 100 | + onClick={() => |
| 101 | + setDraftSubjects((prev) => { |
| 102 | + const copy = [...prev]; |
| 103 | + copy.splice(index, 1); |
| 104 | + return copy; |
| 105 | + }) |
| 106 | + } |
| 107 | + > |
| 108 | + <MdClose className="text-xs" /> |
| 109 | + </button> |
| 110 | + </span> |
| 111 | + ))} |
| 112 | + </div> |
| 113 | + <div className="mt-2 w-full"> |
| 114 | + <input |
| 115 | + type="text" |
| 116 | + onChange={(e) => { |
| 117 | + const newFilteredSubjects = allSubjects.filter((subject) => |
| 118 | + subject.name.includes(e.target.value.trim()), |
| 119 | + ); |
| 120 | + setFilteredSubjects(newFilteredSubjects); |
| 121 | + }} |
| 122 | + placeholder="興味分野タグを絞り込み" |
| 123 | + className="input input-bordered w-full" |
| 124 | + /> |
| 125 | + </div> |
| 126 | + <ul className="mt-2"> |
| 127 | + {filteredSubjects.length !== 0 ? ( |
| 128 | + filteredSubjects |
| 129 | + .filter( |
| 130 | + (subject) => |
| 131 | + !draftSubjects.some((draft) => draft.id === subject.id), |
| 132 | + ) |
| 133 | + .map((subject) => ( |
| 134 | + <li key={subject.id}> |
| 135 | + <button |
| 136 | + type="button" |
| 137 | + className="btn btn-ghost inline-flex h-full w-full justify-start p-2" |
| 138 | + onClick={() => |
| 139 | + setDraftSubjects((prev) => [...prev, subject]) |
| 140 | + } |
| 141 | + > |
| 142 | + <span className="font-normal text-lg"> |
| 143 | + #{subject.name} |
| 144 | + </span> |
| 145 | + </button> |
| 146 | + </li> |
| 147 | + )) |
| 148 | + ) : ( |
| 149 | + <li key="empty" className="p-2 text-gray-500"> |
| 150 | + 検索結果がありません |
| 151 | + </li> |
| 152 | + )} |
| 153 | + <li className="flex w-full items-center justify-center py-2"> |
| 154 | + <button |
| 155 | + type="button" |
| 156 | + className="btn btn-secondary px-6 font-normal" |
| 157 | + onClick={() => setIsOpen(true)} |
| 158 | + > |
| 159 | + <MdAdd /> |
| 160 | + タグを新規作成 |
| 161 | + </button> |
| 162 | + </li> |
| 163 | + </ul> |
| 164 | + </div> |
| 165 | + <div className="my-2 flex justify-between"> |
| 166 | + <button type="button" className="btn " onClick={handleBack}> |
| 167 | + 前へ |
| 168 | + </button> |
| 169 | + <button |
| 170 | + type="button" |
| 171 | + className="btn btn-primary" |
| 172 | + onClick={() => { |
| 173 | + updateInterests({ interestSubjects: draftSubjects }); |
| 174 | + router.push("/tutorial"); |
| 175 | + }} |
| 176 | + > |
| 177 | + 次へ |
| 178 | + </button> |
| 179 | + </div> |
| 180 | + </div> |
| 181 | + </div> |
| 182 | + {isOpen && ( |
| 183 | + <dialog |
| 184 | + id="add-dialog" |
| 185 | + className="modal modal-open" |
| 186 | + onClose={() => setIsOpen(false)} |
| 187 | + > |
| 188 | + <div className="modal-box"> |
| 189 | + <h3 className="mb-4 font-bold text-lg">興味分野タグの作成</h3> |
| 190 | + <input |
| 191 | + type="text" |
| 192 | + className="input input-bordered my-2 w-full" |
| 193 | + value={newSubjectName} |
| 194 | + onChange={(e) => setNewSubjectName(e.target.value)} |
| 195 | + placeholder="タグ名を入力" |
| 196 | + /> |
| 197 | + {newSubjectName && ( |
| 198 | + <p className="py-4"> |
| 199 | + 興味分野タグ{" "} |
| 200 | + <span className="text-primary">#{newSubjectName}</span>{" "} |
| 201 | + を作成します |
| 202 | + </p> |
| 203 | + )} |
| 204 | + <div className="modal-action"> |
| 205 | + <form method="dialog"> |
| 206 | + <div className="flex gap-3"> |
| 207 | + <button |
| 208 | + type="button" |
| 209 | + className="btn" |
| 210 | + onClick={() => { |
| 211 | + setIsOpen(false); |
| 212 | + setNewSubjectName(""); |
| 213 | + }} |
| 214 | + > |
| 215 | + キャンセル |
| 216 | + </button> |
| 217 | + <button |
| 218 | + type="button" |
| 219 | + className="btn btn-primary" |
| 220 | + onClick={async () => { |
| 221 | + await createSubject(newSubjectName); |
| 222 | + setIsOpen(false); |
| 223 | + getSubjects(); |
| 224 | + setNewSubjectName(""); |
| 225 | + }} |
| 226 | + > |
| 227 | + 作成 |
| 228 | + </button> |
| 229 | + </div> |
| 230 | + </form> |
| 231 | + </div> |
| 232 | + </div> |
| 233 | + </dialog> |
| 234 | + )} |
| 235 | + </> |
| 236 | + ); |
| 237 | +} |
0 commit comments