Skip to content

Commit 8d6e1f4

Browse files
committed
refactor code logic ,
1 parent fa5971b commit 8d6e1f4

1 file changed

Lines changed: 39 additions & 43 deletions

File tree

libs/store/src/lib/clans/clanSettingChannel.slice.ts

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -148,61 +148,57 @@ export const settingClanChannelSlice = createSlice({
148148
reducers: {
149149
addChannelFromSocket: (state, action) => {
150150
const channel = action.payload;
151-
if (channel?.id) {
152-
if (channel.parent_id && channel.parent_id !== '0') {
151+
if (!channel?.id) return;
152+
if (channel.parent_id && channel.parent_id !== '0') {
153+
if (!state.threadsByChannel[channel.parent_id]) {
154+
state.threadsByChannel[channel.parent_id] = [];
155+
}
156+
const existingThread = state.threadsByChannel[channel.parent_id].find((t) => t.id === channel.id);
157+
if (!existingThread) {
158+
state.threadsByChannel[channel.parent_id].push(channel);
153159
state.threadCount += 1;
154-
if (!state.threadsByChannel[channel.parent_id]) {
155-
state.threadsByChannel[channel.parent_id] = [];
156-
}
157-
const existingThread = state.threadsByChannel[channel.parent_id].find((t) => t.id === channel.id);
158-
if (!existingThread) {
159-
state.threadsByChannel[channel.parent_id].push(channel);
160-
}
161-
} else {
162-
channelSettingAdapter.addOne(state, channel);
163-
state.channelCount += 1;
164160
}
161+
return;
165162
}
163+
channelSettingAdapter.addOne(state, channel);
164+
state.channelCount += 1;
166165
},
167166
removeChannelFromSocket: (state, action) => {
168167
const channelId = action.payload;
169-
const channel = state.entities[channelId];
170-
171-
if (channel) {
168+
if (state.entities[channelId]) {
172169
channelSettingAdapter.removeOne(state, channelId);
173170
state.channelCount = Math.max(0, state.channelCount - 1);
174-
} else {
175-
for (const parentId in state.threadsByChannel) {
176-
const threads = state.threadsByChannel[parentId];
177-
const threadIndex = threads.findIndex((t) => t.id === channelId);
178-
if (threadIndex !== -1) {
179-
state.threadsByChannel[parentId] = threads.filter((t) => t.id !== channelId);
180-
state.threadCount = Math.max(0, state.threadCount - 1);
181-
break;
182-
}
183-
}
171+
return;
184172
}
173+
Object.keys(state.threadsByChannel).some((parentId) => {
174+
const threads = state.threadsByChannel[parentId];
175+
const threadExists = threads.some((t) => t.id === channelId);
176+
if (threadExists) {
177+
state.threadsByChannel[parentId] = threads.filter((t) => t.id !== channelId);
178+
state.threadCount = Math.max(0, state.threadCount - 1);
179+
return true;
180+
}
181+
return false;
182+
});
185183
},
186184
updateChannelFromSocket: (state, action) => {
187185
const channel = action.payload;
188-
if (channel?.id) {
189-
if (state.entities[channel.id]) {
190-
channelSettingAdapter.updateOne(state, {
191-
id: channel.id,
192-
changes: channel
193-
});
194-
} else {
195-
for (const parentId in state.threadsByChannel) {
196-
const threads = state.threadsByChannel[parentId];
197-
const threadIndex = threads.findIndex((t) => t.id === channel.id);
198-
if (threadIndex !== -1) {
199-
state.threadsByChannel[parentId][threadIndex] = {
200-
...threads[threadIndex],
201-
...channel
202-
};
203-
break;
204-
}
205-
}
186+
if (!channel?.id) return;
187+
if (state.entities[channel.id]) {
188+
channelSettingAdapter.updateOne(state, {
189+
id: channel.id,
190+
changes: channel
191+
});
192+
return;
193+
}
194+
if (channel.parent_id && state.threadsByChannel[channel.parent_id]) {
195+
const threads = state.threadsByChannel[channel.parent_id];
196+
const threadIndex = threads.findIndex((t) => t.id === channel.id);
197+
if (threadIndex !== -1) {
198+
state.threadsByChannel[channel.parent_id][threadIndex] = {
199+
...threads[threadIndex],
200+
...channel
201+
};
206202
}
207203
}
208204
}

0 commit comments

Comments
 (0)