user.js 13 KB

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