Skip to content

Instantly share code, notes, and snippets.

@khades
Created July 7, 2019 12:33
Show Gist options
  • Save khades/bb61321270043a622589f11551828d26 to your computer and use it in GitHub Desktop.
Save khades/bb61321270043a622589f11551828d26 to your computer and use it in GitHub Desktop.
import * as React from "react";
import States from "../utils/states";
import * as API from "./api";
import { IChannelNames } from "./types";
export function fetchChannelNameThunk(
channelID: string,
channelNames: IChannelNames,
setChannelNames: React.Dispatch<React.SetStateAction<IChannelNames>>,
getChannelName: (channelID: string) => Promise<string> = API.getChannelName) {
if (channelNames[channelID]) {
return;
}
setChannelNames((localChannelNames: IChannelNames) => Object.assign({}, localChannelNames, { [channelID]: { state: States.LOADING } }));
getChannelName(channelID).then((channelName: string) => {
setChannelNames((localChannelNames: IChannelNames) => Object.assign({}, localChannelNames, {
[channelID]: {
name: channelName,
state: States.READY,
},
}));
}).catch(() => {
setChannelNames((localChannelNames: IChannelNames) => Object.assign({}, localChannelNames, { [channelID]: { state: States.NOTFOUND } }));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment