Phase 0a Mandantenfähigkeit
This commit is contained in:
1 parent
0748990328
commit
0c044249d9
15 files changed
+982
-193
No files matched your search
@@ -4,9 +4,13 @@ import { useAuth } from '../context/AuthContext'
|
||||
function Navigation() {
|
||||
const location = useLocation()
|
||||
const navigate = useNavigate()
|
||||
const { user, logout } = useAuth()
|
||||
const { user, logout, setActiveClub } = useAuth()
|
||||
|
||||
const handleLogout = async () => {
|
||||
const clubs = user?.clubs || []
|
||||
const selectClubId =
|
||||
user?.active_club_id != null && clubs.some((c) => c.id === user.active_club_id)
|
||||
? user.active_club_id
|
||||
: clubs[0]?.id const handleLogout = async () => {
|
||||
await logout()
|
||||
navigate('/login')
|
||||
}
|
||||
@@ -84,6 +88,25 @@ function Navigation() {
|
||||
Profil
|
||||
</Link>
|
||||
|
||||
{(clubs?.length ?? 0) > 1 && (
|
||||
<label style={{ display: 'flex', alignItems: 'center', gap: '0.35rem', fontSize: '0.8125rem', color: 'var(--text2)' }}>
|
||||
<span>Verein</span>
|
||||
<select
|
||||
className="form-input"
|
||||
style={{ padding: '0.35rem 0.5rem', minWidth: '9rem', fontSize: '0.8125rem' }}
|
||||
value={selectClubId ?? ''}
|
||||
onChange={(e) => {
|
||||
const v = e.target.value
|
||||
if (v) setActiveClub(Number(v))
|
||||
}}
|
||||
>
|
||||
{clubs.map((c) => (
|
||||
<option key={c.id} value={c.id}>{c.name}</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
)}
|
||||
|
||||
{/* User Menu */}
|
||||
<div style={{
|
||||
borderLeft: '1px solid var(--border)',
|
||||
|
||||
@@ -1,11 +1,35 @@
|
||||
import { createContext, useContext, useState, useEffect, useCallback } from 'react'
|
||||
import api from '../utils/api'
|
||||
import { createContext, useContext, useState, useEffect, useCallback, useRef } from 'react'
|
||||
import api, { ACTIVE_CLUB_STORAGE_KEY } from '../utils/api'
|
||||
|
||||
const AuthContext = createContext(null)
|
||||
|
||||
function syncStoredActiveClub(profile) {
|
||||
const clubs = profile?.clubs || []
|
||||
const ids = new Set(clubs.map((c) => String(c.id)))
|
||||
const stored = localStorage.getItem(ACTIVE_CLUB_STORAGE_KEY)
|
||||
if (stored && ids.has(stored)) return
|
||||
|
||||
const ac =
|
||||
profile?.active_club_id != null && profile.active_club_id !== ''
|
||||
? String(profile.active_club_id)
|
||||
: ''
|
||||
if (ac && ids.has(ac)) {
|
||||
localStorage.setItem(ACTIVE_CLUB_STORAGE_KEY, ac)
|
||||
return
|
||||
}
|
||||
if (clubs.length >= 1) {
|
||||
localStorage.setItem(ACTIVE_CLUB_STORAGE_KEY, String(clubs[0].id))
|
||||
}
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [user, setUser] = useState(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
const userRef = useRef(null)
|
||||
|
||||
useEffect(() => {
|
||||
userRef.current = user
|
||||
}, [user])
|
||||
|
||||
const checkAuth = useCallback(async () => {
|
||||
const token = localStorage.getItem('authToken')
|
||||
@@ -16,6 +40,7 @@ export function AuthProvider({ children }) {
|
||||
|
||||
try {
|
||||
const profile = await api.getCurrentProfile()
|
||||
syncStoredActiveClub(profile)
|
||||
setUser(profile)
|
||||
} catch (err) {
|
||||
console.error('Auth check failed:', err)
|
||||
@@ -29,9 +54,23 @@ export function AuthProvider({ children }) {
|
||||
checkAuth()
|
||||
}, [checkAuth])
|
||||
|
||||
const setActiveClub = useCallback(async (clubId) => {
|
||||
const cid = Number(clubId)
|
||||
const uid = userRef.current?.id
|
||||
if (!Number.isFinite(cid) || cid < 1 || !uid) return
|
||||
localStorage.setItem(ACTIVE_CLUB_STORAGE_KEY, String(cid))
|
||||
setUser((prev) => (prev?.id ? { ...prev, active_club_id: cid } : prev))
|
||||
try {
|
||||
await api.updateProfile(uid, { active_club_id: cid })
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
}
|
||||
}, [])
|
||||
|
||||
/** Fallback, falls ohne checkAuth gesetzt wird (Legacy / Token-Injektion) */
|
||||
const login = (payload) => {
|
||||
if (payload?.profile != null) {
|
||||
syncStoredActiveClub(payload.profile)
|
||||
setUser(payload.profile)
|
||||
return
|
||||
}
|
||||
@@ -52,6 +91,7 @@ export function AuthProvider({ children }) {
|
||||
const logout = () => {
|
||||
setUser(null)
|
||||
localStorage.removeItem('authToken')
|
||||
localStorage.removeItem(ACTIVE_CLUB_STORAGE_KEY)
|
||||
}
|
||||
|
||||
const value = {
|
||||
@@ -60,7 +100,8 @@ export function AuthProvider({ children }) {
|
||||
loading,
|
||||
login,
|
||||
logout,
|
||||
checkAuth
|
||||
checkAuth,
|
||||
setActiveClub,
|
||||
}
|
||||
|
||||
return (
|
||||
|
||||
@@ -16,10 +16,26 @@ function ClubsPage() {
|
||||
// Form state
|
||||
const [formData, setFormData] = useState({})
|
||||
|
||||
const isAdmin = user?.role === 'admin' || user?.role === 'superadmin'
|
||||
const isTrainer = user?.role === 'trainer' || isAdmin
|
||||
const canCreateClub = ['admin', 'superadmin', 'trainer', 'user'].includes(user?.role)
|
||||
const isPlatformAdmin = user?.role === 'admin' || user?.role === 'superadmin'
|
||||
const isSuperAdmin = user?.role === 'superadmin'
|
||||
const clubAdminClubIds = new Set(
|
||||
(user?.clubs || [])
|
||||
.filter((c) => (c.roles || []).includes('club_admin'))
|
||||
.map((c) => c.id)
|
||||
)
|
||||
const canManageClub = (clubId) => isPlatformAdmin || clubAdminClubIds.has(clubId)
|
||||
const canCreateClub = isPlatformAdmin
|
||||
const canManageOrgSomewhere = isPlatformAdmin || clubAdminClubIds.size > 0
|
||||
const canCreateTrainingGroup =
|
||||
isPlatformAdmin || (Array.isArray(user?.clubs) && user.clubs.length > 0)
|
||||
|
||||
const canEditGroup = (g) =>
|
||||
isPlatformAdmin ||
|
||||
clubAdminClubIds.has(g.club_id) ||
|
||||
g.trainer_id === user?.id ||
|
||||
(Array.isArray(g.co_trainer_ids) && g.co_trainer_ids.includes(user?.id))
|
||||
|
||||
const canDeleteGroup = (g) => isPlatformAdmin || clubAdminClubIds.has(g.club_id)
|
||||
useEffect(() => {
|
||||
loadData()
|
||||
}, [])
|
||||
@@ -47,7 +63,13 @@ function ClubsPage() {
|
||||
setModalType(type)
|
||||
|
||||
if (type === 'club') {
|
||||
setFormData({ name: '', abbreviation: '', description: '', status: 'active' })
|
||||
setFormData({
|
||||
name: '',
|
||||
abbreviation: '',
|
||||
description: '',
|
||||
status: 'active',
|
||||
primary_admin_profile_id: user?.id ?? '',
|
||||
})
|
||||
} else if (type === 'division') {
|
||||
setFormData({ club_id: '', name: '', focus_area: '' })
|
||||
} else if (type === 'group') {
|
||||
@@ -102,11 +124,19 @@ function ClubsPage() {
|
||||
e.preventDefault()
|
||||
|
||||
try {
|
||||
if (modalType === 'club') {
|
||||
if (modalType === 'club') {
|
||||
if (editing) {
|
||||
await api.updateClub(editing.id, formData)
|
||||
} else {
|
||||
await api.createClub(formData)
|
||||
const payload = {
|
||||
...formData,
|
||||
primary_admin_profile_id: Number(formData.primary_admin_profile_id),
|
||||
}
|
||||
if (!payload.primary_admin_profile_id) {
|
||||
alert('Hauptverwalter (Profil-ID) ist Pflicht.')
|
||||
return
|
||||
}
|
||||
await api.createClub(payload)
|
||||
}
|
||||
} else if (modalType === 'division') {
|
||||
if (editing) {
|
||||
@@ -235,7 +265,7 @@ function ClubsPage() {
|
||||
{club.status}
|
||||
</span>
|
||||
</div>
|
||||
{isAdmin && (
|
||||
{canManageClub(club.id) && (
|
||||
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
@@ -243,6 +273,7 @@ function ClubsPage() {
|
||||
>
|
||||
Bearbeiten
|
||||
</button>
|
||||
{isSuperAdmin && (
|
||||
<button
|
||||
className="btn"
|
||||
style={{
|
||||
@@ -254,6 +285,7 @@ function ClubsPage() {
|
||||
>
|
||||
Löschen
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
@@ -269,7 +301,7 @@ function ClubsPage() {
|
||||
<>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '1rem' }}>
|
||||
<h2>Sparten</h2>
|
||||
{isAdmin && (
|
||||
{canManageOrgSomewhere && (
|
||||
<button className="btn btn-primary" onClick={() => handleCreate('division')}>
|
||||
+ Neue Sparte
|
||||
</button>
|
||||
@@ -306,7 +338,7 @@ function ClubsPage() {
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
{isAdmin && (
|
||||
{canManageClub(division.club_id) && (
|
||||
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
@@ -340,7 +372,7 @@ function ClubsPage() {
|
||||
<>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '1rem' }}>
|
||||
<h2>Trainingsgruppen</h2>
|
||||
{canCreateClub && (
|
||||
{canCreateTrainingGroup && (
|
||||
<button className="btn btn-primary" onClick={() => handleCreate('group')}>
|
||||
+ Neue Gruppe
|
||||
</button>
|
||||
@@ -377,7 +409,7 @@ function ClubsPage() {
|
||||
{group.age_group && <div>👶 {group.age_group}</div>}
|
||||
</div>
|
||||
|
||||
{(isAdmin || group.trainer_id === user?.id) && (
|
||||
{canEditGroup(group) && (
|
||||
<div style={{ display: 'flex', gap: '0.5rem', marginTop: 'auto' }}>
|
||||
<button
|
||||
className="btn btn-secondary"
|
||||
@@ -386,7 +418,7 @@ function ClubsPage() {
|
||||
>
|
||||
Bearbeiten
|
||||
</button>
|
||||
{isAdmin && (
|
||||
{canDeleteGroup(group) && (
|
||||
<button
|
||||
className="btn"
|
||||
style={{
|
||||
@@ -485,6 +517,28 @@ function ClubsPage() {
|
||||
<option value="inactive">Inaktiv</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
{!editing && canCreateClub && (
|
||||
<div className="form-row">
|
||||
<label className="form-label">Hauptverwalter (Profil-ID) *</label>
|
||||
<input
|
||||
type="number"
|
||||
min={1}
|
||||
className="form-input"
|
||||
value={formData.primary_admin_profile_id ?? ''}
|
||||
onChange={(e) =>
|
||||
updateFormField(
|
||||
'primary_admin_profile_id',
|
||||
e.target.value === '' ? '' : parseInt(e.target.value, 10)
|
||||
)
|
||||
}
|
||||
required
|
||||
/>
|
||||
<p style={{ fontSize: '0.8rem', color: 'var(--text3)', marginTop: '0.35rem' }}>
|
||||
Nur Plattform-Administratoren legen Vereine an. Standard ist deine eigene Profil-ID.
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
|
||||
@@ -8,6 +8,17 @@ import { stripHtmlToText } from './htmlUtils'
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL || ''
|
||||
|
||||
/** LocalStorage + Request-Header für Mandanten-Kontext */
|
||||
export const ACTIVE_CLUB_STORAGE_KEY = 'shinkan_active_club_id'
|
||||
|
||||
function mergeActiveClubHeader(headers = {}) {
|
||||
const cid = localStorage.getItem(ACTIVE_CLUB_STORAGE_KEY)
|
||||
if (cid && /^\d+$/.test(String(cid).trim())) {
|
||||
return { ...headers, 'X-Active-Club-Id': String(cid).trim() }
|
||||
}
|
||||
return { ...headers }
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic API request with automatic token injection
|
||||
*/
|
||||
@@ -15,9 +26,9 @@ async function request(endpoint, options = {}) {
|
||||
const token = localStorage.getItem('authToken')
|
||||
const method = (options.method || 'GET').toUpperCase()
|
||||
|
||||
const headers = {
|
||||
const headers = mergeActiveClubHeader({
|
||||
...options.headers,
|
||||
}
|
||||
})
|
||||
// GET ohne Body: kein Content-Type: application/json (manche Proxies/Headers stören sich)
|
||||
if (method !== 'GET' && method !== 'HEAD') {
|
||||
if (!headers['Content-Type'] && !headers['content-type']) {
|
||||
@@ -99,6 +110,11 @@ export async function getCurrentProfile() {
|
||||
return request('/api/profiles/me')
|
||||
}
|
||||
|
||||
/** Liste aller Profile – nur für Plattform-Admins (Vereinsanlage). */
|
||||
export async function listProfiles() {
|
||||
return request('/api/profiles')
|
||||
}
|
||||
|
||||
export async function updateProfile(profileId, data) {
|
||||
return request(`/api/profiles/${profileId}`, {
|
||||
method: 'PUT',
|
||||
@@ -1008,6 +1024,7 @@ export const api = {
|
||||
register,
|
||||
logout,
|
||||
getCurrentProfile,
|
||||
listProfiles,
|
||||
updateProfile,
|
||||
changePassword,
|
||||
verifyEmail,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Shinkan Jinkendo Frontend Version
|
||||
|
||||
export const APP_VERSION = "0.5.1"
|
||||
export const APP_VERSION = "0.8.12"
|
||||
export const BUILD_DATE = "2026-05-05"
|
||||
|
||||
export const PAGE_VERSIONS = {
|
||||
@@ -8,7 +8,7 @@ export const PAGE_VERSIONS = {
|
||||
Dashboard: "1.0.0",
|
||||
AccountSettingsPage: "1.0.0",
|
||||
ExercisesPage: "1.1.0", // Updated: Katalog-Integration
|
||||
ClubsPage: "1.0.0",
|
||||
ClubsPage: "1.1.0",
|
||||
SkillsPage: "1.0.0",
|
||||
TrainingPlanningPage: "1.3.1",
|
||||
TrainingFrameworkProgramsListPage: "1.1.0",
|
||||
|
||||
Reference in new issue
Block a user