user.js 13 KB

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