Skip to content

Commit

Permalink
#2490 Refactor how the theme picker works, turn it into a pinia store
Browse files Browse the repository at this point in the history
  • Loading branch information
dwalizer committed Jun 21, 2024
1 parent 1c2fa20 commit ea1ce97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
6 changes: 6 additions & 0 deletions dashboard-prime/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { invoke, until } from '@vueuse/core'
import DashboardFooter from '@/components/header/DashboardFooter.vue'
import { useUserAgreementInterceptor } from '@/interceptors/UseUserAgreementInterceptor.js'
import PkiAppBootstrap from '@/components/access/PkiAppBootstrap.vue'
import {usePrimeVue} from "primevue/config";
const authState = useAuthState()
const appInfoState = useAppInfoState()
Expand All @@ -49,6 +50,7 @@ const accessState = useAccessState()
const errorHandling = useErrorHandling()
const userAgreementInterceptor = useUserAgreementInterceptor()
const route = useRoute()
const PrimeVue = usePrimeVue()
const customGlobalValidators = useCustomGlobalValidators()
const globalNavGuards = useGlobalNavGuards()
Expand Down Expand Up @@ -76,6 +78,10 @@ watch(() => authState.userInfo, async (newUserInfo) => {
}
})
watch(() => themeHelper.currentTheme, (newTheme, oldTheme) => {
PrimeVue.changeTheme(oldTheme.value, newTheme.value, 'theme-link')
})
const iframeInit = useIframeInit()
onBeforeMount(() => {
iframeInit.handleHandshake()
Expand Down
28 changes: 22 additions & 6 deletions dashboard-prime/src/components/header/UseThemesHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,29 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useStorage } from '@vueuse/core'
import { ref } from 'vue';
import { defineStore } from 'pinia'
import SettingsService from "@/components/settings/SettingsService.js";

export const useThemesHelper = () => {
export const useThemesHelper = defineStore('useThemesHelper', () => {

const themeOptions =[
{ icon: 'fas fa-sun', name: 'Light', value: 'skills-light-green' },
{ icon: 'fas fa-moon', name: 'Dark', value: 'skills-dark-green' },
{ name: 'Light', value: 'skills-light-green' },
{ name: 'Dark', value: 'skills-dark-green' },
];
const currentTheme = useStorage('currentTheme', themeOptions[0])
const currentTheme = ref(themeOptions[0]);

const setCurrentTheme = (newTheme) => {
currentTheme.value = newTheme;
}

const setDarkMode = () => {
setCurrentTheme(themeOptions[1]);
}

const setLightMode = () => {
setCurrentTheme(themeOptions[0]);
}

const loadTheme = () => {
SettingsService.getUserSettings().then((response) => {
Expand All @@ -46,8 +59,11 @@ export const useThemesHelper = () => {

return {
currentTheme,
setCurrentTheme,
setDarkMode,
setLightMode,
themeOptions,
configureDefaultThemeFileInHeadTag,
loadTheme
}
}
});
9 changes: 5 additions & 4 deletions dashboard-prime/src/components/settings/Preferences.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ import SubPageHeader from '@/components/utils/pages/SubPageHeader.vue'
import SettingsService from '@/components/settings/SettingsService.js'
import { useAppConfig } from '@/common-components/stores/UseAppConfig.js'
import { useColors } from '@/skills-display/components/utilities/UseColors.js'
import {usePrimeVue} from "primevue/config";
import {useThemesHelper} from "@/components/header/UseThemesHelper.js";
const PrimeVue = usePrimeVue()
const themeHelper = useThemesHelper()
const appConfig = useAppConfig();
const colors = useColors()
Expand Down Expand Up @@ -121,8 +119,11 @@ function saveUserSettings(dirtyChanges) {
// $store.commit('storeUser', userInfo);
}
if (key === 'enableDarkMode') {
const selectedThemes = value.value ? [themeHelper.themeOptions[0].value, themeHelper.themeOptions[1].value] : [themeHelper.themeOptions[1].value, themeHelper.themeOptions[0].value];
PrimeVue.changeTheme(selectedThemes[0], selectedThemes[1], 'theme-link')
if(value.value) {
themeHelper.setDarkMode();
} else {
themeHelper.setLightMode();
}
}
});
})
Expand Down

0 comments on commit ea1ce97

Please sign in to comment.