import {createSlice} from '@reduxjs/toolkit'; import {NO_USER_DATA} from '../initialStates'; /** * A reducer is a pure function with the sole responsibility of updating the state and nothing else. * No side effects are allowed. */ /** * Actions are a way to indicate what just happened / what is going to happen and update the state accordingly. */ /** * Create slice allows us * To initialise State , create Actions and Reducers in one go * Read more here https://redux.js.org/introduction/installation */ const userDataSlice = createSlice({ name: 'userData', initialState: NO_USER_DATA, reducers: { userLoggedIn: (state, action) => { state.user = action.payload; }, userDetailsFetched: (state, action) => { state.profile = action.payload.profile; state.avatar = action.payload.avatar; state.cover = action.payload.cover; }, socialEdited: (state, action) => { const {social, value} = action.payload; switch (social) { case 'Snapchat': state.profile.snapchat = value; break; case 'TikTok': state.profile.tiktok = value; break; } }, profileBadgesUpdated: (state, action) => { state.profile.badges = action.payload.badges; }, profileBadgeRemoved: (state, action) => { state.profile.badges = state.profile.badges.filter( (badge) => badge.name !== action.payload.badge, ); }, profileCompletionStageUpdated: (state, action) => { state.profile.profile_completion_stage = action.payload.stage; }, setSuggestedPeopleLinked: (state, action) => { state.profile.suggested_people_linked = action.payload.suggested_people_linked; }, setIsOnboardedUser: (state, action) => { state.isOnboardedUser = action.payload.isOnboardedUser; }, setNewNotificationReceived: (state, action) => { state.newNotificationReceived = action.payload.newNotificationReceived; }, setReplyPosted: (state, action) => { state.replyPosted = action.payload.replyPosted; }, setNewVersionAvailable: (state, action) => { state.newVersionAvailable = action.payload.newVersionAvailable; }, setSuggestedPeopleImage: (state, action) => { state.suggestedPeopleImage = action.payload.suggestedPeopleImage; }, clearHeaderAndProfileImages: (state) => { state.avatar = ''; state.cover = ''; }, setMomentUploadProgressBar: (state, action) => { state.momentUploadProgressBar = action.payload.momentUploadProgressBar; }, }, }); export const { userLoggedIn, userDetailsFetched, socialEdited, profileCompletionStageUpdated, setSuggestedPeopleLinked, setIsOnboardedUser, setNewVersionAvailable, setNewNotificationReceived, setReplyPosted, setSuggestedPeopleImage, clearHeaderAndProfileImages, profileBadgesUpdated, profileBadgeRemoved, setMomentUploadProgressBar, } = userDataSlice.actions; export const userDataReducer = userDataSlice.reducer;