user.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import { REST } from 'vuejs/api/rest-axios'
  2. // import { JSONAPI } from 'vuejs/api/json-axios'
  3. import { MA } from 'vuejs/api/ma-axios'
  4. import qs from 'querystring-es3'
  5. import materiauGQL from 'vuejs/api/gql/materiauflaglist.fragment.gql'
  6. export default {
  7. namespaced: true,
  8. // initial state
  9. state: {
  10. uid: null,
  11. // username: '',
  12. mail: '',
  13. csrf_token: null,
  14. logout_token: null,
  15. isloggedin: false,
  16. isAdmin: false,
  17. isAdherent: false,
  18. canSearch: false,
  19. roles: [],
  20. flagcolls: false,
  21. flagcollsLoadedItems: {},
  22. openedCollid: null
  23. },
  24. // getters
  25. getters: {},
  26. // mutations
  27. mutations: {
  28. SetCsrftoken (state, token) {
  29. console.log('SetCsrftoken', token)
  30. state.csrf_token = token
  31. },
  32. setToken (state, data) {
  33. state.uid = data.current_user.uid
  34. // state.username = data.username;
  35. state.mail = data.current_user.mail
  36. state.csrf_token = data.csrf_token
  37. state.isloggedin = true
  38. state.logout_token = data.logout_token
  39. },
  40. setUid (state, uid) {
  41. state.uid = uid
  42. state.isloggedin = true
  43. },
  44. setUser (state, data) {
  45. state.mail = data.mail[0].value
  46. state.uuid = data.uuid[0].value
  47. },
  48. setRoles (state, roles) {
  49. console.log('User setRoles', roles)
  50. state.roles = []
  51. for (let i = 0; i < roles.length; i++) {
  52. state.roles.push(roles[i].target_id)
  53. }
  54. // check if admin
  55. if (
  56. state.roles.indexOf('admin') !== -1 ||
  57. state.roles.indexOf('root') !== -1
  58. ) {
  59. // console.log('is admin');
  60. state.isAdmin = true
  61. }
  62. // check if has access to search
  63. if (state.roles.indexOf('adherent') !== -1) {
  64. // console.log('is admin');
  65. state.canSearch = true
  66. state.isAdherent = true
  67. }
  68. },
  69. setLoggedOut (state) {
  70. console.log('setLoggedOut state', state)
  71. state.uid = null
  72. state.mail = ''
  73. state.csrf_token = null
  74. state.isloggedin = false
  75. state.logout_token = null
  76. if (state.isAdmin) {
  77. // TODO: what if on a page where login is needed (as commerce checkout and cart)
  78. window.location.reload(true)
  79. }
  80. state.asAdmin = false
  81. state.canSearch = false
  82. },
  83. setFlagColls (state, flagcolls) {
  84. console.log('User pre setFlagColls', state.flagcolls)
  85. state.flagcolls = flagcolls
  86. // console.log('User post setFlagColls', state.flagcolls)
  87. },
  88. openFlagColl (state, collid) {
  89. state.openedCollid = collid
  90. },
  91. closeFlagColl (state) {
  92. state.openedCollid = null
  93. },
  94. setLoadedCollItems (state, data) {
  95. console.log('setLoadedCollItems', data)
  96. // if no data, we are just calling the mutation to trigger the component update
  97. if (data) {
  98. state.flagcollsLoadedItems[data.collid] = data.items
  99. }
  100. }
  101. },
  102. // actions
  103. actions: {
  104. userRegister ({ dispatch, commit, state }, credentials) {
  105. return new Promise((resolve) => {
  106. REST.get('/session/token').then(({ token }) => {
  107. commit('SetCsrftoken', token)
  108. REST.post('/user/register?_format=json', credentials, {
  109. 'X-CSRF-Token': state.csrftoken
  110. })
  111. .then(({ data }) => {
  112. console.log('user REST registered', data)
  113. dispatch('userLogin', credentials).then(() => {
  114. resolve()
  115. })
  116. })
  117. .catch(error => {
  118. console.warn('Issue with register', error)
  119. Promise.reject(error)
  120. })
  121. })
  122. })
  123. },
  124. userLogin ({ dispatch, state }, credentials) {
  125. return new Promise((resolve, reject) => {
  126. dispatch('getToken', credentials).then(() => {
  127. dispatch('getUser').then(userdata => {
  128. console.log('User Loggedin')
  129. if (state.isAdmin) {
  130. window.location.reload(true)
  131. }
  132. resolve()
  133. })
  134. })
  135. })
  136. },
  137. getToken ({ dispatch, commit, state }, credentials) {
  138. return REST.post('/user/login?_format=json', credentials)
  139. .then(({ data }) => {
  140. console.log('user REST getToken data', data)
  141. commit('setToken', data)
  142. })
  143. .catch(error => {
  144. console.warn('Issue with getToken', error)
  145. Promise.reject(error)
  146. })
  147. },
  148. getUser ({ dispatch, commit, state }) {
  149. return new Promise((resolve, reject) => {
  150. REST.get('/session/token').then(({ data }) => {
  151. console.log('csrftoken', data)
  152. commit('SetCsrftoken', data)
  153. console.log('state.csrf_token', state.csrf_token)
  154. const params = {
  155. token: state.csrf_token
  156. }
  157. REST.get(`/user/${state.uid}?_format=json`, params)
  158. .then(({ data }) => {
  159. console.log('user REST getUser data', data)
  160. console.log('roles', data.roles)
  161. commit('setUser', data)
  162. if (data.roles) {
  163. commit('setRoles', data.roles)
  164. }
  165. dispatch('getUserFlagColls')
  166. resolve()
  167. })
  168. .catch(error => {
  169. console.warn('Issue with getUser', error)
  170. Promise.reject(error)
  171. })
  172. })
  173. })
  174. },
  175. getUserFlagColls ({ dispatch, commit, state }) {
  176. // flags
  177. // REST.get('/flagging_collection/1?_format=json')
  178. // .then(( data ) => {
  179. // console.log('TEST FLAG REST data', data)
  180. // })
  181. // .catch(error => {
  182. // console.warn('Issue USER TEST FLAG REST', error)
  183. // Promise.reject(error)
  184. // })
  185. return MA.get('materio_flag/user_flagging_collections')
  186. .then(({ data }) => {
  187. console.log('user MA getFlags data', data)
  188. commit('setFlagColls', data)
  189. })
  190. .catch(error => {
  191. console.warn('Issue USER MA getFlags', error)
  192. Promise.reject(error)
  193. })
  194. },
  195. // https://drupal.stackexchange.com/questions/248539/cant-get-flagging-api-to-accept-post-request
  196. createFlagColl ({ dispatch, commit, state }, new_collection_name) {
  197. console.log('user createFlagColl', new_collection_name)
  198. return new Promise((resolve, reject) => {
  199. const params = {
  200. name: new_collection_name
  201. }
  202. MA.post('materio_flag/create_user_flagging_collection', params)
  203. .then(({ data }) => {
  204. console.log('user MA createFlagColl data', data)
  205. if (data.status) {
  206. dispatch('getUserFlagColls').then(() => {
  207. resolve()
  208. })
  209. }
  210. })
  211. .catch(error => {
  212. console.warn('Issue USER MA createFlag', error)
  213. reject(error)
  214. })
  215. })
  216. },
  217. deleteFlagColl ({ dispatch, commit, state }, flagcollid) {
  218. console.log('user deleteFlagColl', flagcollid)
  219. return new Promise((resolve, reject) => {
  220. const params = {
  221. flagcollid: flagcollid
  222. }
  223. MA.post('materio_flag/delete_user_flagging_collection', params)
  224. .then(({ data }) => {
  225. console.log('user MA deleteFlagColl data', data)
  226. dispatch('getUserFlagColls').then(() => {
  227. resolve()
  228. })
  229. })
  230. .catch(error => {
  231. console.warn('Issue USER MA createFlag', error)
  232. reject(error)
  233. })
  234. })
  235. },
  236. flagUnflag ({ dispatch, commit, state }, { action, id, collid }) {
  237. console.log('user flagUnflag', action, id, collid)
  238. return new Promise((resolve, reject) => {
  239. const params = {
  240. flagid: state.flagcolls[collid].flag_id,
  241. id: id,
  242. flagcollid: collid
  243. }
  244. return MA.post(`materio_flag/${action}`, params)
  245. .then(({ data }) => {
  246. console.log('user MA flag', data)
  247. // reload the fulllist of flagcolleciton
  248. dispatch('getUserFlagColls').then(() => {
  249. if (state.flagcolls[collid].items.length) {
  250. dispatch('loadMaterialsGQL', {
  251. ids: state.flagcolls[collid].items,
  252. gqlfragment: materiauGQL,
  253. callBack: 'loadMaterialsCallBack',
  254. callBackArgs: { collid: collid }
  255. }).then(() => {
  256. resolve()
  257. })
  258. // dispatch('loadMaterials', {
  259. // uuids: state.flagcolls[collid].items_uuids,
  260. // imgStyle: ['card_medium_half'],
  261. // callBack: 'loadMaterialsCallBack',
  262. // callBackArgs: { collid: collid }
  263. // }).then(() => {
  264. // resolve()
  265. // })
  266. } else {
  267. commit('setLoadedCollItems', { collid: collid, items: [] })
  268. resolve()
  269. }
  270. })
  271. })
  272. .catch(error => {
  273. console.warn('Issue USER MA flagUnflag', error)
  274. })
  275. })
  276. },
  277. openFlagColl ({ commit, dispatch, state }, collid) {
  278. console.log('user openFlagColl', collid)
  279. commit('openFlagColl', collid)
  280. if (state.flagcolls[collid].items.length) {
  281. if (typeof state.flagcollsLoadedItems[collid] === 'undefined') {
  282. console.log('loading flagcoll items', state.flagcolls[collid])
  283. // if no loadedItems, load them
  284. // loadMaterials is on mixins
  285. // https://github.com/huybuidac/vuex-extensions
  286. dispatch('loadMaterialsGQL', {
  287. ids: state.flagcolls[collid].items,
  288. gqlfragment: materiauGQL,
  289. callBack: 'loadMaterialsCallBack',
  290. callBackArgs: { collid: collid }
  291. })
  292. // dispatch('loadMaterials', {
  293. // uuids: state.flagcolls[collid].items_uuids,
  294. // imgStyle: ['card_medium_half'],
  295. // callBack: 'loadMaterialsCallBack',
  296. // callBackArgs: { collid: collid }
  297. // })
  298. } else {
  299. // call the mutation without data to only trigger the FlagCollection component update
  300. console.log('committing setLoadedCollItems without args')
  301. commit('setLoadedCollItems')
  302. }
  303. } else {
  304. commit('setLoadedCollItems', { collid: collid, items: [] })
  305. }
  306. },
  307. loadMaterialsCallBack ({ commit }, { items, callBackArgs }) {
  308. console.log('user loadMaterialsCallBack', items, callBackArgs)
  309. commit('setLoadedCollItems', { collid: callBackArgs.collid, items: items })
  310. },
  311. closeFlagColl ({ commit, dispatch }) {
  312. console.log('user closeFlagColl')
  313. commit('closeFlagColl')
  314. },
  315. userLogout ({ commit, state }) {
  316. const credentials = qs.stringify({
  317. token: state.csrf_token
  318. })
  319. REST.post('/user/logout', credentials)
  320. .then(resp => {
  321. console.log('userLogout resp', resp)
  322. commit('setLoggedOut')
  323. })
  324. .catch(error => {
  325. console.warn('Issue with logout', error)
  326. Promise.reject(error)
  327. })
  328. }
  329. }
  330. }