-
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
9 changed files
with
239 additions
and
24 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "foo" | ||
| } |
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,5 +1,79 @@ | ||
| import React from 'react'; | ||
|
|
||
| export function EditGroup () { | ||
| return (<>Edit Group</>); | ||
| import { Prompt, useHistory } from 'react-router'; | ||
| import { useParams } from 'react-router-dom'; | ||
| import Translate from '../../i18n/components/translate'; | ||
| import { useGroups } from '../hooks'; | ||
| import { Schema } from '../../form/Schema'; | ||
| import { FormManager } from '../../form/FormManager'; | ||
|
|
||
| import { GroupForm } from '../component/GroupForm'; | ||
| import { GroupProvider } from '../hoc/GroupProvider'; | ||
|
|
||
| export function EditGroup() { | ||
|
|
||
| const { id } = useParams(); | ||
|
|
||
| const history = useHistory(); | ||
|
|
||
| const { post, response, loading } = useGroups({}); | ||
|
|
||
| const [blocking, setBlocking] = React.useState(false); | ||
|
|
||
| async function save(metadata) { | ||
| await post(``, metadata); | ||
| if (response.ok) { | ||
| gotoDetail({ refresh: true }); | ||
| } | ||
| }; | ||
|
|
||
| const cancel = () => { | ||
| gotoDetail(); | ||
| }; | ||
|
|
||
| const gotoDetail = (state = null) => { | ||
| setBlocking(false); | ||
| history.push(`/groups`, state); | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="container-fluid p-3"> | ||
| <Prompt | ||
| when={blocking} | ||
| message={location => | ||
| `message.unsaved-editor` | ||
| } | ||
| /> | ||
| <section className="section" tabIndex="0"> | ||
| <div className="section-header bg-info p-2 text-white"> | ||
| <div className="row justify-content-between"> | ||
| <div className="col-md-12"> | ||
| <span className="display-6"><Translate value="label.new-group">Add a new group</Translate></span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <div className="section-body p-4 border border-top-0 border-info"> | ||
| <GroupProvider id={id}> | ||
| {(group) => | ||
| <Schema path={`/assets/schema/groups/group.json`}> | ||
| {(schema) => | ||
| <>{group && | ||
| <FormManager initial={group}> | ||
| {(data, errors) => | ||
| <GroupForm | ||
| group={data} | ||
| errors={errors} | ||
| schema={schema} | ||
| loading={loading} | ||
| onSave={(data) => save(data)} | ||
| onCancel={() => cancel()} />} | ||
| </FormManager> | ||
| }</>} | ||
| </Schema> | ||
| } | ||
| </GroupProvider> | ||
| </div> | ||
| </section> | ||
| </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
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,24 @@ | ||
| import React from 'react'; | ||
| import { useGroup } from '../hooks'; | ||
|
|
||
| export function GroupProvider({ id, children }) { | ||
|
|
||
| const [group, setGroup] = React.useState(); | ||
|
|
||
|
|
||
| const { get, response } = useGroup({ | ||
| cachePolicy: 'no-cache' | ||
| }); | ||
|
|
||
| async function loadGroup() { | ||
| const group = await get(``); | ||
| if (response.ok) { | ||
| setGroup(group); | ||
| } | ||
| } | ||
|
|
||
| /*eslint-disable react-hooks/exhaustive-deps*/ | ||
| React.useEffect(() => { loadGroup() }, []); | ||
|
|
||
| return (<>{children(group)}</>); | ||
| } |
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,107 @@ | ||
| import React from 'react'; | ||
|
|
||
| const initialState = { | ||
| data: {}, | ||
| errors: [] | ||
| }; | ||
|
|
||
| const FormContext = React.createContext(); | ||
|
|
||
| const { Provider, Consumer } = FormContext; | ||
|
|
||
| export const FormActions = { | ||
| SET_FORM_ERROR: 'set form error', | ||
| SET_FORM_DATA: 'set form data' | ||
| }; | ||
|
|
||
| export const setFormDataAction = (payload) => { | ||
| return { | ||
| type: FormActions.SET_FORM_DATA, | ||
| payload | ||
| } | ||
| } | ||
|
|
||
| export const setFormErrorAction = (errors) => { | ||
| return { | ||
| type: FormActions.SET_FORM_ERROR, | ||
| payload: errors | ||
| } | ||
| } | ||
|
|
||
| function reducer(state, action) { | ||
| switch (action.type) { | ||
|
|
||
| case FormActions.SET_FORM_ERROR: | ||
| return { | ||
| ...state, | ||
| errors: action.payload | ||
| }; | ||
| case FormActions.SET_FORM_DATA: | ||
| return { | ||
| ...state, | ||
| data: action.payload | ||
| }; | ||
| default: | ||
| return state; | ||
| } | ||
| } | ||
|
|
||
| /*eslint-disable react-hooks/exhaustive-deps*/ | ||
| function FormManager({ children, initial = {} }) { | ||
|
|
||
| const data = { | ||
| ...initial | ||
| }; | ||
|
|
||
| const [state, dispatch] = React.useReducer(reducer, { | ||
| ...initialState, | ||
| data | ||
| }); | ||
|
|
||
|
|
||
| const contextValue = React.useMemo(() => ({ state, dispatch }), [state, dispatch]); | ||
|
|
||
| return ( | ||
| <React.Fragment> | ||
| <Provider value={contextValue}>{children(state.data, state.errors)}</Provider> | ||
| </React.Fragment> | ||
| ); | ||
| } | ||
|
|
||
| function useFormErrors() { | ||
| const { state } = React.useContext(FormContext); | ||
| const { errors } = state; | ||
|
|
||
| return errors; | ||
| } | ||
|
|
||
| function useFormContext() { | ||
| return React.useContext(FormContext); | ||
| } | ||
|
|
||
| function useFormDispatcher() { | ||
| const { dispatch } = useFormContext(); | ||
| return dispatch; | ||
| } | ||
|
|
||
| function useFormState() { | ||
| const { state } = useFormContext(); | ||
| return state; | ||
| } | ||
|
|
||
| function useFormData() { | ||
| const { data } = useFormContext(); | ||
| return data; | ||
| } | ||
|
|
||
| export { | ||
| useFormErrors, | ||
| useFormContext, | ||
| useFormDispatcher, | ||
| useFormState, | ||
| useFormData, | ||
| FormManager, | ||
| FormContext, | ||
| Provider as FormProvider, | ||
| Consumer as FormConsumer | ||
| }; |
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