1+ import {
2+ getPropById ,
3+ addClaimInState ,
4+ editClaimInState ,
5+ deleteClaimInState
6+ } from './../util' ;
7+ import backendApi from './../api/backendApi' ;
8+ import wdSiteApi from './../api/wikidataSiteApi' ;
9+ import {
10+ SET_PROFILE ,
11+ ADD_PROPCOL ,
12+ RESET_PROPCOL ,
13+ SET_DT_PROGRESS ,
14+ SET_BD_PROGRESS ,
15+ SET_LEX_ITEMS ,
16+ SET_LEXITEM_DATA
17+ } from './../constants' ;
18+
19+
20+ export function setBackdrop ( bool ) {
21+ return ( dispatch ) => {
22+ dispatch ( {
23+ type : SET_BD_PROGRESS ,
24+ payload : bool
25+ } )
26+ }
27+ }
28+
29+ export function setProfile ( ) {
30+ return async ( dispatch ) => {
31+ // Checking whether user is authencated on backend or not
32+ backendApi . get ( "/api/profile" )
33+ . then ( ( { data } ) => {
34+ if ( data . logged ) {
35+ dispatch ( {
36+ type : SET_PROFILE ,
37+ payload : data . username
38+ } )
39+ }
40+ } )
41+ . catch ( ( ) => alert ( "Something went wrong with auth." ) )
42+ }
43+ }
44+
45+ export function addPropertyCol ( pId ) {
46+ return async ( dispatch ) => {
47+ const resp = await getPropById ( pId )
48+ if ( resp !== false ) {
49+ dispatch ( {
50+ type : ADD_PROPCOL ,
51+ payload : resp
52+ } ) ;
53+ }
54+ } ;
55+ }
56+
57+ export function removePropertyCol ( pId ) {
58+ return ( dispatch , getState ) => {
59+ const { propCol } = getState ( ) ;
60+ if ( pId !== undefined ) {
61+ dispatch ( {
62+ type : RESET_PROPCOL ,
63+ payload : propCol . filter ( ( item , index ) => index !== pId )
64+ } ) ;
65+ } else {
66+ // if pId is not present then remove every property
67+ dispatch ( {
68+ type : RESET_PROPCOL ,
69+ payload : [ ]
70+ } ) ;
71+ }
72+ } ;
73+ }
74+
75+ export function setDataTableProgress ( option ) {
76+ return ( dispatch ) => {
77+ dispatch ( {
78+ type : SET_DT_PROGRESS ,
79+ payload : option
80+ } )
81+ }
82+ }
83+
84+ export function setLexItems ( item ) {
85+ return ( dispatch ) => {
86+ dispatch ( {
87+ type : SET_LEX_ITEMS ,
88+ payload : item
89+ } )
90+ dispatch ( setLexItemsData ( ) )
91+ }
92+ }
93+
94+ export function setLexItemsData ( ) {
95+ return async ( dispatch , getState ) => {
96+ const { lexItems } = getState ( ) ;
97+ console . log ( lexItems ) ;
98+ const resp = await wdSiteApi . get ( '/api.php?action=wbgetentities&format=json&origin=*&ids=' + lexItems . join ( '|' ) )
99+ dispatch ( {
100+ type : SET_LEXITEM_DATA ,
101+ payload : Object . values ( resp . data . entities )
102+ } )
103+ }
104+ }
105+
106+ export function createClaim ( itemId , pId , type , value ) {
107+ return async ( dispatch , getState ) => {
108+ const resp = await backendApi . post (
109+ '/api/createclaim' ,
110+ {
111+ "entity" : itemId ,
112+ "property" : pId ,
113+ "value" : value ,
114+ "type" : type
115+ } ,
116+ {
117+ crossDomain : true ,
118+ headers : {
119+ 'Accept' : 'application/json' ,
120+ 'Content-Type' : 'application/json'
121+ }
122+ }
123+ )
124+
125+ dispatch ( setBackdrop ( false ) )
126+
127+ if ( resp . data [ "success" ] ) {
128+ let { lexItemsData } = getState ( )
129+
130+ // Dispatching action to set new data
131+ dispatch ( {
132+ type : SET_LEXITEM_DATA ,
133+ payload : addClaimInState ( lexItemsData , itemId , pId , resp . data . claim )
134+ } )
135+ return Promise . resolve ( {
136+ "status" : 1
137+ } )
138+ } else {
139+ alert ( "Failed to add the item :(" )
140+ return Promise . resolve ( {
141+ "status" : 0
142+ } )
143+ }
144+ }
145+ }
146+
147+ export function editClaim ( id , p , newValue ) {
148+ return async ( dispatch , getState ) => {
149+ const resp = await backendApi . post (
150+ '/api/editclaim' ,
151+ {
152+ "claimId" : id ,
153+ "claimType" : p . type ,
154+ "value" : typeof ( newValue ) === "string" ? newValue : newValue . id
155+ } ,
156+ {
157+ crossDomain : true ,
158+ headers : {
159+ 'Accept' : 'application/json' ,
160+ 'Content-Type' : 'application/json'
161+ }
162+ }
163+ )
164+
165+ dispatch ( setBackdrop ( false ) )
166+
167+ if ( resp . data [ "success" ] ) {
168+ let { lexItemsData } = getState ( )
169+
170+ // Dispatching action to set new data
171+ dispatch ( {
172+ type : SET_LEXITEM_DATA ,
173+ payload : editClaimInState ( lexItemsData , id , p , newValue )
174+ } )
175+ return Promise . resolve ( {
176+ "status" : 1 ,
177+ "value" : typeof ( newValue ) === "string" ? newValue : newValue . id
178+ } )
179+ } else {
180+ alert ( "Failed to edit the item :(" )
181+ return Promise . resolve ( {
182+ "status" : 0
183+ } )
184+ }
185+ }
186+ }
187+
188+ export function deleteClaim ( id , p ) {
189+ return async ( dispatch , getState ) => {
190+ const resp = await backendApi . post (
191+ '/api/deleteitem' ,
192+ {
193+ "itemId" : id
194+ } ,
195+ {
196+ crossDomain : true ,
197+ headers : {
198+ 'Accept' : 'application/json' ,
199+ 'Content-Type' : 'application/json'
200+ }
201+ }
202+ )
203+
204+ dispatch ( setBackdrop ( false ) )
205+
206+ if ( resp . data [ "success" ] ) {
207+ let { lexItemsData } = getState ( )
208+
209+ // Dispatching action to set new data
210+ dispatch ( {
211+ type : SET_LEXITEM_DATA ,
212+ payload : deleteClaimInState ( lexItemsData , id , p )
213+ } )
214+ return Promise . resolve ( {
215+ "status" : 1
216+ } )
217+ } else {
218+ alert ( "Failed to delete the item :(" )
219+ return Promise . resolve ( {
220+ "status" : 0
221+ } )
222+ }
223+ }
224+ }
0 commit comments