-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement editing channel subscription settings
- Loading branch information
Showing
3 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
Parlance.ClientApp/src/pages/Account/Notifications/Channel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {SubscriptionChannelName} from "@/interfaces/unsubscribe"; | ||
import ListPageBlock from "@/components/ListPageBlock"; | ||
import {VerticalLayout} from "@/components/Layouts"; | ||
import PageHeading from "@/components/PageHeading"; | ||
import React, {Dispatch} from "react"; | ||
import {useTranslation} from "react-i18next"; | ||
import {notificationChannelText, NotificationChannelType} from "@/components/notifications/Events"; | ||
import SelectableList from "@/components/SelectableList"; | ||
import Fetch from "@/helpers/Fetch"; | ||
import Modal from "@/components/Modal"; | ||
import ErrorModal from "@/components/modals/ErrorModal"; | ||
|
||
|
||
export interface ChannelSubscriptionState { | ||
channel: SubscriptionChannelName; | ||
subscriptionData: string; | ||
enabled: boolean; | ||
} | ||
|
||
interface ChannelSubscriptionsInitAction { | ||
action: "init" | ||
data: ChannelSubscriptionState[] | ||
} | ||
|
||
interface ChannelSubscriptionsSetAction { | ||
action: "set" | ||
channel: string | ||
subscriptionData: string | ||
enabled: boolean | ||
} | ||
|
||
export type ChannelSubscriptionsActions = ChannelSubscriptionsInitAction | ChannelSubscriptionsSetAction; | ||
|
||
export function Channel({channel, channels, dispatchChannels}: { | ||
channel: SubscriptionChannelName, | ||
channels: ChannelSubscriptionState[], | ||
dispatchChannels: Dispatch<ChannelSubscriptionsActions> | ||
}) { | ||
const {t} = useTranslation(); | ||
|
||
const updateChannel = async (subscriptionData: string, enabled: boolean) => { | ||
dispatchChannels({ | ||
action: "set", | ||
channel: channel, | ||
subscriptionData: subscriptionData, | ||
enabled: enabled | ||
}); | ||
|
||
try { | ||
await Fetch.post("/api/notifications/channels", { | ||
channel: channel, | ||
subscriptionData: subscriptionData, | ||
enabled: enabled | ||
}); | ||
} catch (e) { | ||
Modal.mount(<ErrorModal error={e} onContinue={() => window.location.reload()} okButtonText={t("RELOAD")} />) | ||
} | ||
} | ||
|
||
return <div> | ||
<ListPageBlock> | ||
<VerticalLayout> | ||
<PageHeading level={3}>{t(notificationChannelText(channel, NotificationChannelType.Name))}</PageHeading> | ||
<SelectableList items={channels.filter(x => x.channel == channel).map(x => { | ||
return { | ||
contents: StateContents(x), | ||
on: x.enabled, | ||
onClick: () => updateChannel(x.subscriptionData, !x.enabled) | ||
} | ||
})} /> | ||
</VerticalLayout> | ||
</ListPageBlock> | ||
</div> | ||
} | ||
|
||
function StateContents(subscription: ChannelSubscriptionState) { | ||
const data = JSON.parse(subscription.subscriptionData); | ||
|
||
switch (subscription.channel) { | ||
case "TranslationFreeze": | ||
return data.Project; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters