1+ import { Activity , Cooperation , Tag } from '@open-source-bazaar/activityhub-service' ;
2+ import { computed } from 'mobx' ;
3+ import { observer } from 'mobx-react' ;
4+ import { ObservedComponent } from 'mobx-react-helper' ;
5+ import { Field , RestForm } from 'mobx-restful-table' ;
6+
7+ import { CooperationModel } from '../../models/Cooperation' ;
8+ import fileStore from '../../models/File' ;
9+ import { OrganizationModel } from '../../models/Organization' ;
10+ import { i18n , I18nContext } from '../../models/Translation' ;
11+ import { renderTagInput } from '../Tag' ;
12+
13+ export interface CooperationEditorProps {
14+ cooperation ?: Cooperation ;
15+ activityId : number ;
16+ activity ?: Activity ;
17+ }
18+
19+ @observer
20+ export class CooperationEditor extends ObservedComponent < CooperationEditorProps , typeof i18n > {
21+ static contextType = I18nContext ;
22+
23+ cooperationStore = new CooperationModel ( this . props . activityId ) ;
24+ organizationStore = new OrganizationModel ( ) ;
25+ fileStore = fileStore ;
26+
27+ componentDidMount ( ) {
28+ const { cooperation } = this . props ;
29+
30+ if ( cooperation ) this . cooperationStore . currentOne = cooperation ;
31+ }
32+
33+ submitHandler = ( ) => {
34+ const { activityId, cooperation } = this . props ;
35+ const { t } = this . observedContext ;
36+
37+ alert ( cooperation ? t ( 'cooperation_updated_successfully' ) : t ( 'cooperation_created_successfully' ) ) ;
38+
39+ window . location . href = `/activity/${ activityId } /cooperation` ;
40+ } ;
41+
42+ @computed
43+ get fields ( ) : Field < Cooperation > [ ] {
44+ const { t } = this . observedContext ;
45+ const { activity } = this . props ;
46+
47+ return [
48+ {
49+ key : 'partner' ,
50+ renderLabel : t ( 'organization' ) ,
51+ renderInput : renderTagInput ( this . organizationStore ) ,
52+ required : true ,
53+ invalidMessage : t ( 'field_required' ) ,
54+ } ,
55+ {
56+ key : 'level' ,
57+ renderLabel : t ( 'cooperation_level' ) ,
58+ type : 'select' ,
59+ options : activity ?. cooperationLevels ?. map ( ( level : Tag ) => ( {
60+ title : level . name ,
61+ value : String ( level . id ) ,
62+ } ) ) || [ ] ,
63+ required : true ,
64+ invalidMessage : t ( 'field_required' ) ,
65+ } ,
66+ ] ;
67+ }
68+
69+ render ( ) {
70+ const { downloading, uploading } = this . cooperationStore ;
71+
72+ const loading = downloading > 0 || uploading > 0 ;
73+
74+ return (
75+ < >
76+ < RestForm
77+ className = "container-fluid"
78+ translator = { this . observedContext }
79+ store = { this . cooperationStore }
80+ fields = { this . fields }
81+ onSubmit = { this . submitHandler }
82+ />
83+ { loading && < div > Loading...</ div > }
84+ </ >
85+ ) ;
86+ }
87+ }
0 commit comments