-
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 #33 from weaponsforge/dev
v1.0.3
- Loading branch information
Showing
9 changed files
with
316 additions
and
43 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,102 @@ | ||
import Link from 'next/link' | ||
import { useSelector } from 'react-redux' | ||
|
||
// MUI | ||
import Box from '@mui/material/Box' | ||
import Button from '@mui/material/Button' | ||
import ButtonGroup from '@mui/material/ButtonGroup' | ||
import CardActions from '@mui/material/CardActions' | ||
import TextField from '@mui/material/TextField' | ||
import Typography from '@mui/material/Typography' | ||
import styles from './styles' | ||
|
||
// Layout | ||
import AppContainer from '@/components/layout/appcontainer' | ||
import AppCard from '@/layout/appcard' | ||
import SimpleContainer from '@/layout/simplecontainer' | ||
|
||
function CreateTodo ({ | ||
state, | ||
inputStatus, | ||
onTextChange, | ||
onTextClick, | ||
onSaveClick, | ||
onResetClick, | ||
onCancelClick | ||
}) { | ||
const { loading, error } = useSelector((state) => state.todos) | ||
|
||
return ( | ||
<AppContainer maxWidth='sm'> | ||
<AppCard> | ||
<h1>Create a Todo</h1> | ||
|
||
<Box sx={styles.inputContainer}> | ||
<TextField | ||
id='title' | ||
label='Title' | ||
placeholder='Enter a title' | ||
variant='outlined' | ||
disabled={loading === 'pending'} | ||
value={state.title} | ||
onChange={onTextChange} | ||
onClick={onTextClick} | ||
/> | ||
|
||
<TextField | ||
id='description' | ||
label='Description' | ||
placeholder='Enter a description' | ||
variant='outlined' | ||
disabled={loading === 'pending'} | ||
value={state.description} | ||
onChange={onTextChange} | ||
onClick={onTextClick} | ||
/> | ||
|
||
<TextField | ||
id='content' | ||
label='Content' | ||
placeholder='Enter content' | ||
variant='outlined' | ||
disabled={loading === 'pending'} | ||
value={state.content} | ||
onChange={onTextChange} | ||
onClick={onTextClick} | ||
multiline | ||
rows={9} | ||
/> | ||
</Box> | ||
|
||
<Box sx={{ textAlign: 'left', color: 'red' }}> | ||
<Typography variant='caption'> | ||
{error || inputStatus || <br />} | ||
</Typography> | ||
</Box> | ||
|
||
<CardActions sx={styles.buttons}> | ||
<ButtonGroup | ||
variant='outlined' | ||
disabled={loading === 'pending'} | ||
> | ||
<Button onClick={onCancelClick}>Cancel</Button> | ||
<Button onClick={onResetClick}>Clear</Button> | ||
<Button | ||
variant='contained' | ||
onClick={onSaveClick} | ||
> | ||
Save | ||
</Button> | ||
</ButtonGroup> | ||
</CardActions> | ||
</AppCard> | ||
|
||
<SimpleContainer> | ||
<Link href='/'>Home</Link> | | ||
<Link href='/todo'>Todos</Link> | ||
</SimpleContainer> | ||
</AppContainer> | ||
) | ||
} | ||
|
||
export default CreateTodo |
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,19 @@ | ||
const styles = { | ||
inputContainer: { | ||
minHeight: 400, | ||
width: '100%', | ||
textAlign: 'left', | ||
'& .MuiTextField-root': { | ||
width: '100%', | ||
marginBottom: '16px !important' | ||
} | ||
}, | ||
buttons: { | ||
float: 'right', | ||
'& button': { | ||
width: '80px' | ||
} | ||
} | ||
} | ||
|
||
export default styles |
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,77 @@ | ||
import { useState } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { unwrapResult } from '@reduxjs/toolkit' | ||
import { useRouter } from 'next/router' | ||
import { createNewTodo, todosReset } from '@/store/todo/todoSlice' | ||
|
||
import CreateTodo from '@/components/todo/create' | ||
|
||
const defaultState = { | ||
title: '', description: '', content: '' | ||
} | ||
|
||
function CreateTodoContainer () { | ||
const [state, setState] = useState(defaultState) | ||
const [inputStatus, setStatus] = useState('') | ||
const dispatch = useDispatch() | ||
const router = useRouter() | ||
const error = useSelector((states) => states.todos.error) | ||
|
||
const resetErrorsMessages = () => { | ||
if (error !== '') { | ||
dispatch(todosReset()) | ||
} | ||
|
||
if (inputStatus !== '') { | ||
setStatus('') | ||
} | ||
} | ||
|
||
const handleInputChange = (e) => { | ||
const { id, value } = e.target | ||
setState({ ...state, [id]: value }) | ||
resetErrorsMessages() | ||
} | ||
|
||
const handleSave = () => { | ||
if (!Object.values(state).every(field => field !== '')) { | ||
setStatus('Please check your input.') | ||
return | ||
} | ||
|
||
dispatch(createNewTodo(state)) | ||
.then(unwrapResult) | ||
.then((todo) => { | ||
router.push(`/todo/${todo._id}`) | ||
}) | ||
} | ||
|
||
const handleCancel = () => { | ||
router.push('/todo') | ||
} | ||
|
||
const handleReset = () => { | ||
resetErrorsMessages() | ||
setState(defaultState) | ||
} | ||
|
||
const handleInputClick = (e) => { | ||
const { id } = e.target | ||
setState({ ...state, [id]: '' }) | ||
resetErrorsMessages() | ||
} | ||
|
||
return ( | ||
<CreateTodo | ||
state={state} | ||
inputStatus={inputStatus} | ||
onTextChange={handleInputChange} | ||
onTextClick={handleInputClick} | ||
onSaveClick={handleSave} | ||
onResetClick={handleReset} | ||
onCancelClick={handleCancel} | ||
/> | ||
) | ||
} | ||
|
||
export default CreateTodoContainer |
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
Oops, something went wrong.