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
1 parent
668fad1
commit 0bcc68c
Showing
13 changed files
with
252 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,89 @@ | ||
import { usePaginatedQuery, useQuery } from 'react-query'; | ||
import { useMutation, usePaginatedQuery, useQuery } from 'react-query'; | ||
|
||
import Request from '../util/api/engine/Request'; | ||
|
||
const FIVE_MINUTES_IN_MILLISECONDS = 1000 * 60 * 5; | ||
|
||
const STALE_TIME = { | ||
staleTime: FIVE_MINUTES_IN_MILLISECONDS, | ||
}; | ||
|
||
const REQUEST = { | ||
GET: 'get', | ||
POST: 'get', | ||
const REQUEST_TYPE = { | ||
POST: 'post', | ||
PUT: 'put', | ||
PATCH: 'patch', | ||
DELETE: 'delete', | ||
}; | ||
|
||
const _fetchData = (operation, url, dataParam, config) => { | ||
const _fetchData = (url, dataParam, config) => { | ||
return async () => { | ||
const res = await Request[operation](url, dataParam, config); | ||
const res = await Request.get(url, dataParam, config); | ||
return res.data; | ||
}; | ||
}; | ||
|
||
const _useOperation = (reactQueryKey, operation, url, dataParam, config) => { | ||
const { data, status } = useQuery(reactQueryKey, _fetchData(operation, url, dataParam, config), STALE_TIME); | ||
return { data, status }; | ||
const _performRequest = ({ operation, url, dataParam, requestConfig }) => { | ||
return Request[operation](url, dataParam, requestConfig); | ||
}; | ||
|
||
const _useOperation = (reactQueryKey, url, config) => { | ||
return useQuery(reactQueryKey, _fetchData(url, null, config)); | ||
}; | ||
|
||
const _useMutation = ({ operation, url, requestConfig, mutationOptions }) => { | ||
return useMutation((dataParam) => { | ||
_performRequest({ | ||
operation, | ||
url, | ||
dataParam, | ||
requestConfig, | ||
}); | ||
}, mutationOptions); | ||
}; | ||
|
||
const _usePaginatedOperation = (reactQueryKey, pageDependency, operation, url, dataParam, config) => { | ||
const _usePaginatedOperation = (reactQueryKey, pageDependency, url, config) => { | ||
const parameters = { | ||
params: { | ||
...dataParam, | ||
page: pageDependency, | ||
}, | ||
}; | ||
const { resolvedData, latestData, status } = usePaginatedQuery([reactQueryKey, pageDependency], _fetchData(operation, url, parameters, config), STALE_TIME); | ||
return { resolvedData, latestData, status }; | ||
return usePaginatedQuery([reactQueryKey, pageDependency], _fetchData(url, parameters, config)); | ||
}; | ||
|
||
export function useGet(reactQueryKey, url, config) { | ||
return _useOperation(reactQueryKey, REQUEST.GET, url, null, config ? config : null); | ||
return _useOperation(reactQueryKey, url, config); | ||
} | ||
|
||
export function usePaginatedGet(reactQueryKey, pageDependency, url, config) { | ||
return _usePaginatedOperation(reactQueryKey, pageDependency, REQUEST.GET, url, null, config ? config : null); | ||
return _usePaginatedOperation(reactQueryKey, pageDependency, url, config); | ||
} | ||
|
||
export function usePost(reactQueryKey, url, dataParam, config) { | ||
return _useOperation(reactQueryKey, REQUEST.POST, url, dataParam ? dataParam : null, config ? config : null); | ||
export function usePost({ url, requestConfig, mutationOptions }) { | ||
return _useMutation({ | ||
operation: REQUEST_TYPE.POST, | ||
url, | ||
requestConfig, | ||
mutationOptions, | ||
}); | ||
} | ||
|
||
export function usePut(reactQueryKey, url, dataParam, config) { | ||
return _useOperation(reactQueryKey, REQUEST.PUT, url, dataParam ? dataParam : null, config ? config : null); | ||
export function usePut({ url, requestConfig, mutationOptions }) { | ||
return _useMutation({ | ||
operation: REQUEST_TYPE.PUT, | ||
url, | ||
requestConfig, | ||
mutationOptions, | ||
}); | ||
} | ||
|
||
export function usePatch(reactQueryKey, url, dataParam, config) { | ||
return _useOperation(reactQueryKey, REQUEST.PATCH, url, dataParam ? dataParam : null, config ? config : null); | ||
export function usePatch({ url, requestConfig, mutationOptions }) { | ||
return _useMutation({ | ||
operation: REQUEST_TYPE.PATCH, | ||
url, | ||
requestConfig, | ||
mutationOptions, | ||
}); | ||
} | ||
|
||
export function useDelete(reactQueryKey, url, config) { | ||
return _useOperation(reactQueryKey, REQUEST.DELETE, url, null, config ? config : null); | ||
export function useDelete({ url, requestConfig, mutationOptions }) { | ||
return _useMutation({ | ||
operation: REQUEST_TYPE.DELETE, | ||
url, | ||
requestConfig, | ||
mutationOptions, | ||
}); | ||
} |
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,65 @@ | ||
import React from 'react'; | ||
import { Form, Formik } from 'formik'; | ||
import { Button, ButtonDesign, FlexBox, FlexBoxDirection, FlexBoxAlignItems } from '@ui5/webcomponents-react'; | ||
import { useQueryCache } from 'react-query'; | ||
|
||
import APIProvider from '../../../util/api/url/APIProvider'; | ||
import Constants from '../../../util/Constants'; | ||
import { usePost } from '../../../hooks/useRequest'; | ||
|
||
import TodoForm from '../Form/TodoForm'; | ||
import TodoFormValidationSchema from '../Form/TodoFormValidationSchema'; | ||
import FormData from '../Form/Data'; | ||
|
||
const styles = { | ||
flexBox: { | ||
width: '100%', | ||
margin: '10px 0', | ||
}, | ||
buttons: { | ||
marginRight: '10px', | ||
}, | ||
form: { | ||
margin: '0 15px', | ||
}, | ||
}; | ||
|
||
const AddTodoForm = ({ dialogClose }) => { | ||
const queryCache = useQueryCache(); | ||
const [addTodo] = usePost({ | ||
url: APIProvider.getUrl('CREATE_TODO'), | ||
mutationOptions: { | ||
onSuccess: () => { | ||
queryCache.invalidateQueries([Constants.REACT_QUERY.KEYS.RQ_GET_TODO_LIST]); | ||
}, | ||
}, | ||
}); | ||
|
||
const onSubmitEditForm = async (values, actions) => { | ||
actions.setSubmitting(true); | ||
await addTodo({ ...values, isCompleted: values.completed }); | ||
actions.setSubmitting(false); | ||
actions.resetForm(true); | ||
dialogClose(); | ||
}; | ||
|
||
return ( | ||
<Formik enableReinitialize initialValues={FormData} validationSchema={TodoFormValidationSchema} onSubmit={onSubmitEditForm}> | ||
{({ isSubmitting, handleSubmit }) => ( | ||
<Form style={styles.form}> | ||
<TodoForm /> | ||
<FlexBox direction={FlexBoxDirection.RowReverse} alignItems={FlexBoxAlignItems.Center} style={styles.flexBox}> | ||
<Button type="submit" disabled={isSubmitting} design={ButtonDesign.Emphasized} icon="paper-plane" style={styles.buttons} onClick={handleSubmit}> | ||
Submit | ||
</Button> | ||
<Button style={styles.buttons} onClick={dialogClose}> | ||
Cancel | ||
</Button> | ||
</FlexBox> | ||
</Form> | ||
)} | ||
</Formik> | ||
); | ||
}; | ||
|
||
export default AddTodoForm; |
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,10 @@ | ||
import TypeOptions from './TypeOptions'; | ||
import PriorityOptions from './PriorityOptions'; | ||
|
||
export default { | ||
name: '', | ||
description: '', | ||
completed: false, | ||
priority: PriorityOptions[0].id, | ||
type: TypeOptions[0].id, | ||
}; |
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,5 @@ | ||
export default [ | ||
{ id: 'LOW', text: 'Low' }, | ||
{ id: 'MEDIUM', text: 'Medium' }, | ||
{ id: 'HIGH', text: 'High' }, | ||
]; |
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,29 @@ | ||
import React from 'react'; | ||
import { Field } from 'formik'; | ||
import { InputType } from '@ui5/webcomponents-react'; | ||
|
||
import Input from '../../../components/Form/Input/Input'; | ||
import Switch from '../../../components/Form/Switch/Switch'; | ||
import TextArea from '../../../components/Form/TextArea/TextArea'; | ||
import Select from '../../../components/Form/Select/Select'; | ||
|
||
import TypeOptions from './TypeOptions'; | ||
import PriorityOptions from './PriorityOptions'; | ||
|
||
const TodoForm = () => ( | ||
<> | ||
<div> | ||
<h3>Basic Info</h3> | ||
<Field labelText="Name" name="name" required placeholder="Name goes here" type={InputType.Text} component={Input} /> | ||
<Field labelText="Description" name="description" required placeholder="Add your description here" rows={5} component={TextArea} /> | ||
<Field labelText="Completed" name="completed" required component={Switch} graphical={true} /> | ||
</div> | ||
<div> | ||
<h3>Custom Info</h3> | ||
<Field labelText="Priority" name="priority" required component={Select} options={PriorityOptions} /> | ||
<Field labelText="Type" name="type" required component={Select} options={TypeOptions} /> | ||
</div> | ||
</> | ||
); | ||
|
||
export default TodoForm; |
Oops, something went wrong.