Initial commit

This commit is contained in:
Julien Nahum
2022-09-20 21:59:52 +02:00
commit f8e6cd4dd6
479 changed files with 77078 additions and 0 deletions

23
resources/js/store/index.js vendored Normal file
View File

@@ -0,0 +1,23 @@
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// Load store modules dynamically.
const requireContext = require.context('./modules', true, /.*\.js$/)
const modules = requireContext.keys()
.map(file =>
[file.replace(/(^.\/)|(\.js$)/g, ''), requireContext(file)]
)
.reduce((modules, [name, module]) => {
if (module.namespaced === undefined) {
module.namespaced = true
}
return { ...modules, [name]: module }
}, {})
export default new Vuex.Store({
modules
})

104
resources/js/store/modules/auth.js vendored Normal file
View File

@@ -0,0 +1,104 @@
import axios from 'axios'
import Cookies from 'js-cookie'
import * as types from '../mutation-types'
// state
export const state = {
user: null,
token: Cookies.get('token'),
// For admin impersonation
admin_token: Cookies.get('admin_token') ?? null
}
// getters
export const getters = {
user: state => state.user,
token: state => state.token,
check: state => state.user !== null,
isImpersonating: state => state.admin_token !== null
}
// mutations
export const mutations = {
[types.SAVE_TOKEN] (state, { token, remember }) {
state.token = token
Cookies.set('token', token, { expires: remember ? 365 : null })
},
[types.FETCH_USER_SUCCESS] (state, { user }) {
state.user = user
},
[types.FETCH_USER_FAILURE] (state) {
state.token = null
Cookies.remove('token')
},
[types.LOGOUT] (state) {
state.user = null
state.token = null
Cookies.remove('token')
},
[types.UPDATE_USER] (state, { user }) {
state.user = user
},
// Stores admin token temporarily for impersonation
startImpersonating (state) {
state.admin_token = state.token
Cookies.set('admin_token', state.token, { expires: 365 })
},
// Stores admin token temporarily for impersonation
stopImpersonating (state) {
state.token = state.admin_token
state.admin_token = null
Cookies.set('token', state.token, { expires: 365 })
Cookies.remove('admin_token')
}
}
// actions
export const actions = {
saveToken ({ commit, dispatch }, payload) {
commit(types.SAVE_TOKEN, payload)
},
async fetchUser ({ commit }) {
try {
const { data } = await axios.get('/api/user')
commit(types.FETCH_USER_SUCCESS, { user: data })
return data
} catch (e) {
commit(types.FETCH_USER_FAILURE)
}
},
updateUser ({ commit }, payload) {
commit(types.UPDATE_USER, payload)
},
async logout ({ commit }) {
try {
await axios.post('/api/logout')
} catch (e) { }
commit(types.LOGOUT)
},
async fetchOauthUrl (ctx, { provider }) {
const { data } = await axios.post(`/api/oauth/${provider}`)
return data.url
},
// Reverse admin impersonation
stopImpersonating ({ commit, dispatch }, payload) {
commit('stopImpersonating')
return dispatch('fetchUser')
}
}

View File

@@ -0,0 +1,35 @@
import Vue from 'vue'
export const namespaced = true
// state
export const state = {
content: []
}
// getters
export const getters = {
getById: (state) => (id) => {
if (state.content.length === 0) return null
return state.content.find(item => item.id === id)
}
}
// mutations
export const mutations = {
set (state, items) {
state.content = items
},
addOrUpdate (state, item) {
state.content = state.content.filter((val) => val.id !== item.id)
state.content.push(item)
},
remove (state, item) {
state.content = state.content.filter((val) => val.id !== item.id)
}
}
// actions
export const actions = {
}

49
resources/js/store/modules/lang.js vendored Normal file
View File

@@ -0,0 +1,49 @@
import Cookies from 'js-cookie'
import * as types from '../mutation-types'
const { locale, locales } = window.config
// state
export const state = {
locale: getLocale(locales, locale),
locales: locales
}
// getters
export const getters = {
locale: state => state.locale,
locales: state => state.locales
}
// mutations
export const mutations = {
[types.SET_LOCALE] (state, { locale }) {
state.locale = locale
}
}
// actions
export const actions = {
setLocale ({ commit }, { locale }) {
commit(types.SET_LOCALE, { locale })
Cookies.set('locale', locale, { expires: 365 })
}
}
/**
* @param {String[]} locales
* @param {String} fallback
* @return {String}
*/
function getLocale (locales, fallback) {
const locale = Cookies.get('locale')
if (Object.prototype.hasOwnProperty.call(locales, locale)) {
return locale
} else if (locale) {
Cookies.remove('locale')
}
return fallback
}

View File

@@ -0,0 +1,27 @@
import Vue from 'vue'
export const namespaced = true
// state
export const state = {
content: null
}
// getters
export const getters = {
}
// mutations
export const mutations = {
set (state, error) {
state.content = error
},
clear (state) {
state.content = null
}
}
// actions
export const actions = {
}

View File

@@ -0,0 +1,67 @@
import axios from 'axios'
export const formsEndpoint = '/api/open/workspaces/{workspaceId}/forms'
export const namespaced = true
// state
export const state = {
content: [],
loading: false
}
// getters
export const getters = {
getById: (state) => (id) => {
if (state.content.length === 0) return null
return state.content.find(item => item.id === id)
},
getBySlug: (state) => (slug) => {
if (state.content.length === 0) return null
return state.content.find(item => item.slug === slug)
},
getAllTags: (state) => {
if (state.content.length === 0) return []
let allTags = []
state.content.forEach(form => {
if(form.tags && form.tags.length > 0){
allTags = allTags.concat(form.tags)
}
})
return allTags.filter((item, i, ar) => ar.indexOf(item) === i)
}
}
// mutations
export const mutations = {
set (state, items) {
state.content = items
},
addOrUpdate (state, item) {
state.content = state.content.filter((val) => val.id !== item.id)
state.content.push(item)
},
remove (state, item) {
state.content = state.content.filter((val) => val.id !== item.id)
},
startLoading (state) {
state.loading = true
},
stopLoading (state) {
state.loading = false
}
}
// actions
export const actions = {
resetState (context) {
context.commit('set', [])
context.commit('stopLoading')
},
load (context, workspaceId) {
context.commit('startLoading')
return axios.get(formsEndpoint.replace('{workspaceId}', workspaceId)).then((response) => {
context.commit('set', response.data)
context.commit('stopLoading')
})
}
}

View File

@@ -0,0 +1,13 @@
export const namespaced = true
// state
export const state = {
content: null
}
// mutations
export const mutations = {
set (state, form) {
state.content = form
}
}

View File

@@ -0,0 +1,100 @@
import Vue from 'vue'
import axios from 'axios'
export const namespaced = true
export const workspaceEndpoint = '/api/open/workspaces/'
const localStorageCurrentWorkspaceKey = 'currentWorkspace'
// state
export const state = {
content: [],
currentId: null,
loading: false
}
// getters
export const getters = {
getById: (state) => (id) => {
if (state.content.length === 0) return null
return state.content.find(item => item.id === id)
},
getCurrent: (state) => () => {
if (state.content.length === 0 || state.currentId === null) return null
return state.content.find(item => item.id === state.currentId)
}
}
// mutations
export const mutations = {
set (state, items) {
state.content = items
if (state.currentId == null && state.content.length > 0) {
// If one only, set it
if (state.content.length === 1) {
state.currentId = items[0].id
localStorage.setItem(localStorageCurrentWorkspaceKey, state.currentId)
} else if (localStorage.getItem(localStorageCurrentWorkspaceKey) && state.content.find(item => item.id === parseInt(localStorage.getItem(localStorageCurrentWorkspaceKey)))) {
// Check local storage for current workspace, or take first
state.currentId = parseInt(localStorage.getItem(localStorageCurrentWorkspaceKey))
localStorage.setItem(localStorageCurrentWorkspaceKey, state.currentId)
} else {
// Else, take first
state.currentId = items[0].id
localStorage.setItem(localStorageCurrentWorkspaceKey, state.currentId)
}
} else {
localStorage.removeItem(localStorageCurrentWorkspaceKey)
}
},
setCurrentId (state, id) {
state.currentId = id
localStorage.setItem(localStorageCurrentWorkspaceKey, id)
},
addOrUpdate (state, item) {
state.content = state.content.filter((val) => val.id !== item.id)
state.content.push(item)
if (state.currentId == null) {
state.currentId = item.id
localStorage.setItem(localStorageCurrentWorkspaceKey, state.currentId)
}
},
remove (state, itemId) {
state.content = state.content.filter((val) => val.id !== itemId)
},
startLoading () {
state.loading = true
},
stopLoading () {
state.loading = false
}
}
// actions
export const actions = {
resetState (context) {
context.commit('set', [])
context.commit('stopLoading')
},
load (context) {
context.commit('set', [])
context.commit('startLoading')
return axios.get(workspaceEndpoint).then((response) => {
context.commit('set', response.data)
context.commit('stopLoading')
})
},
loadIfEmpty ({ context, dispatch, state }) {
if (state.content.length === 0) {
return dispatch('load')
}
return Promise.resolve()
},
delete ({ commit, dispatch, state }, id) {
commit('startLoading')
return axios.delete(workspaceEndpoint + id).then((response) => {
commit('remove', response.data.workspace_id)
commit('stopLoading')
})
}
}

10
resources/js/store/mutation-types.js vendored Normal file
View File

@@ -0,0 +1,10 @@
// auth.js
export const LOGOUT = 'LOGOUT'
export const SAVE_TOKEN = 'SAVE_TOKEN'
export const FETCH_USER = 'FETCH_USER'
export const FETCH_USER_SUCCESS = 'FETCH_USER_SUCCESS'
export const FETCH_USER_FAILURE = 'FETCH_USER_FAILURE'
export const UPDATE_USER = 'UPDATE_USER'
// lang.js
export const SET_LOCALE = 'SET_LOCALE'