( controls: FeedControls, change: FeedControlChange, )
| 77 | }; |
| 78 | |
| 79 | const applyChange = ( |
| 80 | controls: FeedControls, |
| 81 | change: FeedControlChange, |
| 82 | ): FeedControls => { |
| 83 | switch (change.kind) { |
| 84 | case ChangeKind.Topic: { |
| 85 | const topics = controls.topics.filter( |
| 86 | ({ keyword }) => keyword !== change.keyword, |
| 87 | ); |
| 88 | const stored = toStored(change, FeedTopicMode.Off); |
| 89 | return { |
| 90 | ...controls, |
| 91 | topics: stored |
| 92 | ? [...topics, { keyword: change.keyword, ...stored }] |
| 93 | : topics, |
| 94 | }; |
| 95 | } |
| 96 | case ChangeKind.Source: { |
| 97 | const sources = controls.sources.filter( |
| 98 | ({ source }) => source.id !== change.sourceId, |
| 99 | ); |
| 100 | const source = controls.sources.find( |
| 101 | (item) => item.source.id === change.sourceId, |
| 102 | )?.source ?? { id: change.sourceId }; |
| 103 | const stored = toStored(change); |
| 104 | return { |
| 105 | ...controls, |
| 106 | sources: stored ? [...sources, { source, ...stored }] : sources, |
| 107 | }; |
| 108 | } |
| 109 | case ChangeKind.Author: { |
| 110 | const authors = controls.authors.filter( |
| 111 | ({ user }) => user.id !== change.userId, |
| 112 | ); |
| 113 | const user = controls.authors.find( |
| 114 | (item) => item.user.id === change.userId, |
| 115 | )?.user ?? { id: change.userId }; |
| 116 | const stored = toStored(change); |
| 117 | return { |
| 118 | ...controls, |
| 119 | authors: stored ? [...authors, { user, ...stored }] : authors, |
| 120 | }; |
| 121 | } |
| 122 | case ChangeKind.Learning: |
| 123 | return { ...controls, learning: change.enabled }; |
| 124 | default: |
| 125 | return { ...controls, fillIns: change.enabled }; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | const sendChange = (change: FeedControlChange): Promise<unknown> => { |
| 130 | switch (change.kind) { |
no test coverage detected