user.js 10 KB

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