177 lines
12 KiB
TypeScript
177 lines
12 KiB
TypeScript
/**
|
|
*
|
|
* @description Settings system state handler
|
|
* * source : https://redux-toolkit.js.org/api/createslice
|
|
*/
|
|
|
|
import { createSlice , PayloadAction , createAsyncThunk } from '@reduxjs/toolkit'
|
|
import axios from 'axios'
|
|
import { fireAlert } from "./alert-slice";
|
|
|
|
const ACTION_NAME = "settings"
|
|
|
|
type IinitialState = {
|
|
value: SettingsState;
|
|
}
|
|
|
|
type SettingsState = {
|
|
authRedirectPage: string;
|
|
notAuthRedirectPage: string,
|
|
appGeneralSettings: {
|
|
appName : string | null,
|
|
appNameEN : string | null,
|
|
gymName : string | null,
|
|
email : string | null,
|
|
phone : string | null,
|
|
address : string | null,
|
|
showLogo : boolean | null,
|
|
logo : string | null,
|
|
currencySymbol : string | null
|
|
},
|
|
isUpdatingGeneralSettings: boolean,
|
|
isLoadingSettings: boolean,
|
|
loadedFirstTime: boolean,
|
|
}
|
|
|
|
const initialState = {
|
|
value: {
|
|
authRedirectPage: '/dashboard',
|
|
notAuthRedirectPage: '/login',
|
|
appGeneralSettings: {
|
|
appName : "انفنتي",
|
|
appNameEN : "Infinity",
|
|
gymName : null,
|
|
email : null,
|
|
phone : null,
|
|
address : null,
|
|
showLogo : true,
|
|
logo : `
|
|
<svg width="85" height="61" viewBox="0 0 85 61" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
<path d="M40.5526 0.120773C34.235 0.950634 28.7204 4.92148 25.9185 10.6502C24.5979 13.354 24 16.0042 24 19.1005C24 20.6175 24.1071 21.5723 24.3123 21.8846C24.5979 22.3129 25.401 22.4378 25.8025 22.1166C26.0791 21.9024 26.1773 21.6526 26.3647 20.6889C27.1856 16.4146 30.4158 12.7383 34.6454 11.2391C36.0642 10.7394 37.0726 10.5788 38.768 10.5788C40.0083 10.5788 40.4545 10.6145 41.2219 10.7751C44.7376 11.5247 47.5217 13.4343 49.467 16.4236C50.3147 17.7264 50.8679 19.1005 51.2249 20.7781C51.4926 22.0274 51.5104 24.2493 51.2695 25.5074C50.3682 30.1832 47.022 33.9667 42.605 35.3052C41.2219 35.7246 40.9185 35.7603 38.0452 35.8495C35.0738 35.9387 34.9399 35.9655 34.6722 36.5187C34.4937 36.9024 34.4937 37.0452 34.6722 37.4289C34.9578 38.0268 34.7971 38.0178 40.3117 37.9821C45.0321 37.9465 45.3355 37.9375 46.3081 37.7412C52.3849 36.5544 57.2927 32.8691 60.0143 27.4438C62.2808 22.9375 62.6466 17.6817 61.0226 12.8989C58.6579 5.92089 52.4206 0.897095 45.1035 0.0761566C44.0149 -0.0398483 41.6502 -0.0219994 40.5526 0.120773Z" fill="#B90000"/>
|
|
<path d="M36.5818 12.8812C35.1095 13.1579 33.4408 13.8985 32.1648 14.8354C30.2195 16.2631 28.8275 18.2173 28.1404 20.4749C27.9173 21.1977 27.8905 21.4029 27.8905 22.7414C27.8816 24.6599 28.0155 25.1507 28.6133 25.3202C28.8275 25.3827 28.9703 25.3649 29.1933 25.2578C29.4521 25.1239 29.5235 24.999 29.818 24.187C30.2106 23.0894 30.6032 22.3577 31.2635 21.5279C33.414 18.7706 37.492 17.5659 41.2665 18.5653C46.1475 19.8503 48.6371 24.1067 47.3879 29.0056C47.2897 29.3625 47.2272 29.6659 47.2451 29.6837C47.2897 29.7283 47.9589 28.8182 48.2713 28.2828C49.6365 25.936 49.9042 22.8574 48.9762 20.1091C47.7894 16.629 44.8715 13.8896 41.4004 13.0062C40.4099 12.7563 37.6169 12.6849 36.5818 12.8812Z" fill="#B90000"/>
|
|
<path d="M25.3742 36.2512C25.1154 36.51 25.0708 36.6171 25.0708 36.974C25.0708 37.3309 25.1154 37.438 25.3742 37.6968L25.6776 38.0002H28.0512C30.7014 38.0002 30.8263 37.9734 31.0851 37.4291C31.2635 37.0454 31.2635 36.9026 31.0851 36.5189C30.8263 35.9746 30.7014 35.9478 28.0512 35.9478H25.6776L25.3742 36.2512Z" fill="#B90000"/>
|
|
<path d="M2.52 44.545V55H1.155V44.545H2.52ZM8.85141 46.63C9.85141 46.63 10.6614 46.935 11.2814 47.545C11.9014 48.145 12.2114 49.015 12.2114 50.155V55H10.8614V50.35C10.8614 49.53 10.6564 48.905 10.2464 48.475C9.83641 48.035 9.27641 47.815 8.56641 47.815C7.84641 47.815 7.27141 48.04 6.84141 48.49C6.42141 48.94 6.21141 49.595 6.21141 50.455V55H4.84641V46.78H6.21141V47.95C6.48141 47.53 6.84641 47.205 7.30641 46.975C7.77641 46.745 8.29141 46.63 8.85141 46.63ZM17.7861 47.905H16.0611V55H14.6961V47.905H13.6311V46.78H14.6961V46.195C14.6961 45.275 14.9311 44.605 15.4011 44.185C15.8811 43.755 16.6461 43.54 17.6961 43.54V44.68C17.0961 44.68 16.6711 44.8 16.4211 45.04C16.1811 45.27 16.0611 45.655 16.0611 46.195V46.78H17.7861V47.905ZM20.0827 45.445C19.8227 45.445 19.6027 45.355 19.4227 45.175C19.2427 44.995 19.1527 44.775 19.1527 44.515C19.1527 44.255 19.2427 44.035 19.4227 43.855C19.6027 43.675 19.8227 43.585 20.0827 43.585C20.3327 43.585 20.5427 43.675 20.7127 43.855C20.8927 44.035 20.9827 44.255 20.9827 44.515C20.9827 44.775 20.8927 44.995 20.7127 45.175C20.5427 45.355 20.3327 45.445 20.0827 45.445ZM20.7427 46.78V55H19.3777V46.78H20.7427ZM27.0741 46.63C28.0741 46.63 28.8841 46.935 29.5041 47.545C30.1241 48.145 30.4341 49.015 30.4341 50.155V55H29.0841V50.35C29.0841 49.53 28.8791 48.905 28.4691 48.475C28.0591 48.035 27.4991 47.815 26.7891 47.815C26.0691 47.815 25.4941 48.04 25.0641 48.49C24.6441 48.94 24.4341 49.595 24.4341 50.455V55H23.0691V46.78H24.4341V47.95C24.7041 47.53 25.0691 47.205 25.5291 46.975C25.9991 46.745 26.5141 46.63 27.0741 46.63ZM33.3688 45.445C33.1088 45.445 32.8888 45.355 32.7088 45.175C32.5288 44.995 32.4388 44.775 32.4388 44.515C32.4388 44.255 32.5288 44.035 32.7088 43.855C32.8888 43.675 33.1088 43.585 33.3688 43.585C33.6188 43.585 33.8288 43.675 33.9988 43.855C34.1788 44.035 34.2688 44.255 34.2688 44.515C34.2688 44.775 34.1788 44.995 33.9988 45.175C33.8288 45.355 33.6188 45.445 33.3688 45.445ZM34.0288 46.78V55H32.6638V46.78H34.0288ZM38.0202 47.905V52.75C38.0202 53.15 38.1052 53.435 38.2752 53.605C38.4452 53.765 38.7402 53.845 39.1602 53.845H40.1652V55H38.9352C38.1752 55 37.6052 54.825 37.2252 54.475C36.8452 54.125 36.6552 53.55 36.6552 52.75V47.905H35.5902V46.78H36.6552V44.71H38.0202V46.78H40.1652V47.905H38.0202ZM48.8991 46.78L43.9491 58.87H42.5391L44.1591 54.91L40.8441 46.78H42.3591L44.9391 53.44L47.4891 46.78H48.8991ZM58.1912 47.575C57.9012 46.965 57.4812 46.495 56.9312 46.165C56.3812 45.825 55.7412 45.655 55.0112 45.655C54.2812 45.655 53.6212 45.825 53.0312 46.165C52.4512 46.495 51.9912 46.975 51.6512 47.605C51.3212 48.225 51.1562 48.945 51.1562 49.765C51.1562 50.585 51.3212 51.305 51.6512 51.925C51.9912 52.545 52.4512 53.025 53.0312 53.365C53.6212 53.695 54.2812 53.86 55.0112 53.86C56.0312 53.86 56.8712 53.555 57.5312 52.945C58.1912 52.335 58.5762 51.51 58.6862 50.47H54.5162V49.36H60.1412V50.41C60.0612 51.27 59.7912 52.06 59.3312 52.78C58.8712 53.49 58.2662 54.055 57.5162 54.475C56.7662 54.885 55.9312 55.09 55.0112 55.09C54.0412 55.09 53.1562 54.865 52.3562 54.415C51.5562 53.955 50.9212 53.32 50.4512 52.51C49.9912 51.7 49.7612 50.785 49.7612 49.765C49.7612 48.745 49.9912 47.83 50.4512 47.02C50.9212 46.2 51.5562 45.565 52.3562 45.115C53.1562 44.655 54.0412 44.425 55.0112 44.425C56.1212 44.425 57.1012 44.7 57.9512 45.25C58.8112 45.8 59.4362 46.575 59.8262 47.575H58.1912ZM69.026 46.78L64.076 58.87H62.666L64.286 54.91L60.971 46.78H62.486L65.066 53.44L67.616 46.78H69.026ZM80.2832 46.63C80.9232 46.63 81.4932 46.765 81.9932 47.035C82.4932 47.295 82.8882 47.69 83.1782 48.22C83.4682 48.75 83.6132 49.395 83.6132 50.155V55H82.2632V50.35C82.2632 49.53 82.0582 48.905 81.6482 48.475C81.2482 48.035 80.7032 47.815 80.0132 47.815C79.3032 47.815 78.7382 48.045 78.3182 48.505C77.8982 48.955 77.6882 49.61 77.6882 50.47V55H76.3382V50.35C76.3382 49.53 76.1332 48.905 75.7232 48.475C75.3232 48.035 74.7782 47.815 74.0882 47.815C73.3782 47.815 72.8132 48.045 72.3932 48.505C71.9732 48.955 71.7632 49.61 71.7632 50.47V55H70.3982V46.78H71.7632V47.965C72.0332 47.535 72.3932 47.205 72.8432 46.975C73.3032 46.745 73.8082 46.63 74.3582 46.63C75.0482 46.63 75.6582 46.785 76.1882 47.095C76.7182 47.405 77.1132 47.86 77.3732 48.46C77.6032 47.88 77.9832 47.43 78.5132 47.11C79.0432 46.79 79.6332 46.63 80.2832 46.63Z" fill="#B90000"/>
|
|
</svg>
|
|
`,
|
|
currencySymbol : null
|
|
},
|
|
isUpdatingGeneralSettings: false,
|
|
isLoadingSettings: false,
|
|
loadedFirstTime: false,
|
|
}
|
|
} as IinitialState
|
|
|
|
// thunks
|
|
// load data ability
|
|
const load = createAsyncThunk(
|
|
'settings/load',
|
|
async (actionPayload : { page : number }, thunkAPI) => {
|
|
try {
|
|
let { data } = await axios.get(`/api/user/actions/${ACTION_NAME}?type=page&page=`+actionPayload.page)
|
|
if(data.success)
|
|
{
|
|
return data
|
|
}
|
|
else
|
|
{
|
|
return { success : false }
|
|
}
|
|
}catch(err) {
|
|
return { success : false }
|
|
}
|
|
}
|
|
)
|
|
// update ability
|
|
const update = createAsyncThunk(
|
|
'settings/update',
|
|
async (actionPayload : {
|
|
appName : string | null | undefined,
|
|
appNameEN : string | null | undefined,
|
|
gymName : string | null | undefined,
|
|
email : string | null | undefined,
|
|
phone : string | null | undefined,
|
|
address : string | null | undefined,
|
|
showLogo : boolean | null | undefined,
|
|
logo : string | null | undefined,
|
|
currencySymbol : string | null | undefined
|
|
}, thunkAPI) => {
|
|
try {
|
|
let { data } = await axios.put(`/api/user/actions/${ACTION_NAME}`, {
|
|
payload: actionPayload
|
|
})
|
|
if(data.success)
|
|
{
|
|
thunkAPI.dispatch(fireAlert({
|
|
success: true,
|
|
message: "actionDoneWithSuccess",
|
|
}))
|
|
return actionPayload
|
|
}
|
|
else
|
|
{
|
|
return { success : false }
|
|
}
|
|
}catch(err) {
|
|
return { success : false }
|
|
}
|
|
}
|
|
)
|
|
|
|
export const settings = createSlice({
|
|
name: "settings",
|
|
initialState,
|
|
reducers: {
|
|
// reset settings to there default status
|
|
setDefaultSettings: () => {
|
|
return initialState
|
|
},
|
|
// change the authRedirectPage state
|
|
setAuthRedirectPage: (state , action: PayloadAction<SettingsState>) => {
|
|
state.value.authRedirectPage = action.payload.authRedirectPage
|
|
},
|
|
// change the notAuthRedirectPage state
|
|
setNotAuthRedirectPage: (state , action: PayloadAction<SettingsState>) => {
|
|
state.value.notAuthRedirectPage = action.payload.notAuthRedirectPage
|
|
}
|
|
},
|
|
extraReducers: (builder) => {
|
|
// load ability
|
|
builder.addCase(load.pending, (state : IinitialState , action) => {
|
|
state.value.isLoadingSettings = true;
|
|
})
|
|
builder.addCase(load.fulfilled, (state : IinitialState , action) => {
|
|
if(action.payload.success)
|
|
{
|
|
// Safely merge incoming settings with existing settings
|
|
// This ensures that if action.payload.data.settings is undefined or null,
|
|
// the existing appGeneralSettings (from initialState) is preserved.
|
|
state.value.appGeneralSettings = {
|
|
...state.value.appGeneralSettings, // Keep existing properties
|
|
...(action.payload.data?.settings || {}) // Merge incoming data, default to empty object if undefined
|
|
};
|
|
state.value.isLoadingSettings = false;
|
|
state.value.loadedFirstTime = true;
|
|
}
|
|
else
|
|
{
|
|
// If loading failed, ensure loading state is false but keep existing data
|
|
state.value.isLoadingSettings = false;
|
|
// You might want to reset loadedFirstTime to false here if the load was truly unsuccessful
|
|
// state.value.loadedFirstTime = false;
|
|
}
|
|
})
|
|
// update ability
|
|
builder.addCase(update.pending, (state : IinitialState , action) => {
|
|
state.value.isUpdatingGeneralSettings = true;
|
|
})
|
|
builder.addCase(update.fulfilled, (state : IinitialState , action) => {
|
|
state.value.isUpdatingGeneralSettings = false;
|
|
state.value.appGeneralSettings = action.payload;
|
|
})
|
|
}
|
|
})
|
|
|
|
// export the functions
|
|
export const { setDefaultSettings , setAuthRedirectPage , setNotAuthRedirectPage } = settings.actions;// export extra reducers
|
|
export { load , update }
|
|
export default settings.reducer; |