app/appSlice.js

/**
 * @file
 * File : appSlice.js\
 * It is used by redux as a state management provider\
 * Defines the authorization profile slices
 *
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @name appSlice
 */
import { createSlice } from '@reduxjs/toolkit'

/**
 * AppSlice initial state.
 *
 * @typedef {Object} InitialState
 * @author  Pierre-Yves Léglise <contact@axialdata.net>
 * @property {boolean} modalQrCodeIsOpen - Whether the QR code modal is open.
 * @property {boolean} modalMainIsOpen - Whether the main modal is open.
 * @property {string} interfaceLanguage - Interface language.
 * @property {boolean} confIsLoading - Whether the configuration is loading.
 * @property {boolean} configError - Whether there is an error in the configuration.
 * @property {string} error - Error message.
 * @example
 *  const initialState = reducer(state, action)
 * @returns {Object} Initial state.
 */
const initialState = {
  currentView: 'home',
  usersUpdated: false,
  modalQrCodeIsOpen: false,
  modalMainIsOpen: false,
  interfaceLanguage: 'FR_fr',
  confIsLoading: false,
  configError: false,
  error: '',
}

const appSlice = createSlice({
  /**
   * AppSlice reducer.
   *
   * @function
   * @author  Pierre-Yves Léglise <contact@axialdata.net>
   * @param {Object} state - Current state.
   * @param {Object} action - Action to handle.
   * @example
   *  const newState = reducer(state, action)
   * @returns {Object} New state.
   */
  name: 'app',
  initialState,
  reducers: {
    changeLanguage: (state, action) => {
      state.interfaceLanguage = action.payload
      // state.languageData = action.payload.languageData
    },
    setDefaultLanguage: (state, action) => {
      state.interfaceLanguage = initialState.interfaceLanguage
      // state.languageData = action.payload.languageData
    },
    toggleModalMain: (state) => {
      state.modalMainIsOpen = !state.modalMainIsOpen
    },
    setModalMainIsOpen: (state, action) => {
      state.modalMainIsOpen = action.payload
    },
    toggleModalQrCode: (state) => {
      state.modalQrCodeIsOpen = !state.modalQrCodeIsOpen
    },
    setModalQrCodeIsOpen: (state, action) => {
      state.modalQrCodeIsOpen = action.payload
    },
    setConfLoadingStatus: (state, action) => {
      state.confIsLoading = action.payload
    },
    setAppError: (state, action) => {
      state.error = action.payload
    },
    setConfigError: (state, action) => {
      state.error = action.payload
    },
    setUsersUpdated: (state) => {
      state.usersUpdated = true
    },
    resetUsersUpdated: (state) => {
      state.usersUpdated = false
    },
    setCurrentView: (state, action) => {
      state.currentView = action.payload
    },
  },
})

const { actions, reducer } = appSlice
export const selectedLanguage = (state) => state.app.interfaceLanguage
export const currentView = (state) => state.app.currentView
export const {
  toggleModalMain,
  setModalMainIsOpen,
  toggleModalQrCode,
  setModalQrCodeIsOpen,
  changeLanguage,
  setDefaultLanguage,
  setConfLoadingStatus,
  setAppError,
  setConfigError,
  setUsersUpdated,
  resetUsersUpdated,
  setCurrentView,
} = actions
export default reducer