-
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.
Merge pull request #66 from itizaworld/feature/implement-create-task-…
…modal TaskListを実装した
- Loading branch information
Showing
20 changed files
with
316 additions
and
67 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
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
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,26 @@ | ||
'use server'; | ||
|
||
import { API_TASK } from '../_constants/apiUrls'; | ||
import { apiPost } from '~/libs/apiClient'; | ||
import { Task } from '~/domains/Task'; | ||
|
||
export const postTask = async ({ | ||
body, | ||
title, | ||
dueDate, | ||
objectiveId, | ||
}: { | ||
body: string; | ||
title: string; | ||
dueDate: Date; | ||
objectiveId: string; | ||
}) => { | ||
return await apiPost<{ task: Task }>(API_TASK(), { | ||
body: JSON.stringify({ | ||
body, | ||
title, | ||
dueDate, | ||
objectiveId, | ||
}), | ||
}); | ||
}; |
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
88 changes: 88 additions & 0 deletions
88
src/app/_components/domains/Task/CreateTaskModal/CreateTaskModal.tsx
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,88 @@ | ||
'use client'; | ||
|
||
import { FC, useCallback, useState } from 'react'; | ||
import { Button, Input, Modal, ModalBody, ModalContent, ModalFooter } from '@nextui-org/react'; | ||
import { Controller, SubmitHandler, useForm } from 'react-hook-form'; | ||
import { TaskEditor } from '../TaskEditor'; | ||
import { postTask } from '~/app/_actions/taskActions'; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
onOpenChange: () => void; | ||
objectiveId: string; | ||
}; | ||
|
||
interface IFormInput { | ||
title: string; | ||
body: string; | ||
} | ||
|
||
export const CreateModal: FC<Props> = ({ isOpen, onOpenChange, objectiveId }) => { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const { control, formState, handleSubmit, reset } = useForm<IFormInput>({ | ||
defaultValues: { | ||
title: '', | ||
body: '', | ||
}, | ||
}); | ||
|
||
const handleOpenChange = useCallback(() => { | ||
reset(); | ||
onOpenChange(); | ||
}, [onOpenChange, reset]); | ||
|
||
const onSubmit: SubmitHandler<IFormInput> = useCallback( | ||
async (inputData) => { | ||
if (isLoading) return; | ||
setIsLoading(true); | ||
postTask({ title: inputData.title, body: inputData.body, dueDate: new Date(), objectiveId }) | ||
.catch((error) => { | ||
// TODO: 本来はコンソールに出すのではなく、ユーザーにエラーを通知する | ||
console.error(error); | ||
}) | ||
.then(() => { | ||
handleOpenChange(); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
}, | ||
[handleOpenChange, isLoading, objectiveId], | ||
); | ||
|
||
return ( | ||
<Modal isOpen={isOpen} onOpenChange={handleOpenChange} placement="center" hideCloseButton size="xl"> | ||
<ModalContent> | ||
<ModalBody className="my-[8px]"> | ||
<Controller | ||
name="title" | ||
control={control} | ||
rules={{ required: true }} | ||
render={({ field, fieldState }) => ( | ||
<Input | ||
{...field} | ||
placeholder="タスクの名前" | ||
isInvalid={fieldState.isDirty && field.value.length === 0} | ||
errorMessage={fieldState.isDirty && field.value.length === 0 && 'タイトルを入力してください'} | ||
variant="underlined" | ||
size="lg" | ||
/> | ||
)} | ||
/> | ||
<Controller | ||
name="body" | ||
control={control} | ||
rules={{ required: true, minLength: 1 }} | ||
render={({ field }) => <TaskEditor onChangeText={(body) => field.onChange(body)} />} | ||
/> | ||
</ModalBody> | ||
<ModalFooter> | ||
<Button color="primary" onClick={handleSubmit(onSubmit)} isLoading={isLoading} isDisabled={!formState.isValid}> | ||
タスクを作成 | ||
</Button> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; |
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 @@ | ||
export { CreateModal } from './CreateTaskModal'; |
12 changes: 12 additions & 0 deletions
12
src/app/_components/domains/Task/TaskEditor/TaskEditor.tsx
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,12 @@ | ||
'use client'; | ||
|
||
import { FC } from 'react'; | ||
import { Editor } from '~/app/_components/uiParts/Editor'; | ||
|
||
type Props = { | ||
onChangeText: (body: string) => void; | ||
}; | ||
|
||
export const TaskEditor: FC<Props> = ({ onChangeText }) => { | ||
return <Editor onChange={onChangeText} placeholder="タスクの内容を記入しましょう" />; | ||
}; |
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 @@ | ||
export { TaskEditor } from './TaskEditor'; |
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,35 @@ | ||
'use client'; | ||
|
||
import { Button, Card, useDisclosure } from '@nextui-org/react'; | ||
import { FC } from 'react'; | ||
import { CreateModal } from '../CreateTaskModal'; | ||
import { Icon } from '~/app/_components/uiParts/icons'; | ||
import { Task } from '~/domains/Task'; | ||
|
||
type Props = { | ||
objectiveId: string; | ||
tasks: Task[]; | ||
}; | ||
|
||
export const TaskList: FC<Props> = ({ objectiveId, tasks }) => { | ||
const { isOpen, onOpen, onOpenChange } = useDisclosure(); | ||
|
||
return ( | ||
<div className="w-full"> | ||
<p className="text-md font-bold mb-[16px] text-gray-700">タスク一覧</p> | ||
<div className="flex flex-col gap-[8px]"> | ||
{tasks.map((task) => { | ||
return ( | ||
<Card key={task._id} className="md:p-[8px] p-[4px] flex flex-col rounded-[8px] cursor-pointer" shadow="none" radius="none"> | ||
ここにタスク一覧が表示されます | ||
</Card> | ||
); | ||
})} | ||
</div> | ||
<Button className="mt-[8px]" size="sm" color="primary" variant="light" startContent={<Icon icon="PLUS" />} onClick={() => onOpen()}> | ||
タスクの追加 | ||
</Button> | ||
<CreateModal isOpen={isOpen} onOpenChange={onOpenChange} objectiveId={objectiveId} /> | ||
</div> | ||
); | ||
}; |
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 @@ | ||
export { TaskList } from './TaskList'; |
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,31 @@ | ||
'use client'; | ||
|
||
import './styles.scss'; | ||
|
||
import { useEffect, useState, FC, useCallback } from 'react'; | ||
import { Editor } from './Editor'; | ||
import { useDebounce } from '~/libs/useDebounce'; | ||
|
||
type Props = { | ||
body?: string; | ||
placeholder: string; | ||
onChange: (body: string) => Promise<void>; | ||
}; | ||
|
||
export const DebounceEditor: FC<Props> = ({ body, placeholder, onChange }) => { | ||
const [inputText, setInputText] = useState<string>(); | ||
const debouncedInputText = useDebounce({ value: inputText, delay: 200 }); | ||
|
||
const handleEditorChange = useCallback( | ||
async (body: string) => { | ||
await onChange(body); | ||
}, | ||
[onChange], | ||
); | ||
|
||
useEffect(() => { | ||
debouncedInputText && handleEditorChange(debouncedInputText); | ||
}, [debouncedInputText, handleEditorChange]); | ||
|
||
return <Editor body={body} placeholder={placeholder} onChange={(body) => setInputText(body)} />; | ||
}; |
Oops, something went wrong.